diff --git a/EnvelopeGenerator.Infrastructure/Repositories/EmailTemplateRepository.cs b/EnvelopeGenerator.Infrastructure/Repositories/EmailTemplateRepository.cs index 1735311d..dc80e97b 100644 --- a/EnvelopeGenerator.Infrastructure/Repositories/EmailTemplateRepository.cs +++ b/EnvelopeGenerator.Infrastructure/Repositories/EmailTemplateRepository.cs @@ -3,16 +3,33 @@ using DigitalData.UserManager.Infrastructure.Repositories; using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Infrastructure.Contracts; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Caching.Memory; using static EnvelopeGenerator.Common.Constants; namespace EnvelopeGenerator.Infrastructure.Repositories { public class EmailTemplateRepository : CRUDRepository, IEmailTemplateRepository { - public EmailTemplateRepository(EGDbContext dbContext) : base(dbContext) + private readonly IMemoryCache _cache; + + public EmailTemplateRepository(EGDbContext dbContext, IMemoryCache cache) : base(dbContext) { + _cache = cache; } - public async Task ReadByNameAsync(EmailTemplateType type) => await _dbSet.Where(t => t.Name == type.ToString()).FirstOrDefaultAsync(); + private readonly Guid key_guid = Guid.NewGuid(); + + /// + /// Retrieves an email template based on its name. + /// Utilizes in-memory caching to improve performance for the limited number of email templates available. + /// If the template is not found in the cache, it queries the database and stores the result in the cache. + /// + /// The type of the email template, which corresponds to its name. + /// + /// A task that represents the asynchronous operation. The task result contains the email template + /// if found, otherwise null. + /// + public async Task ReadByNameAsync(EmailTemplateType type) => await _cache.GetOrCreateAsync($"{type}{key_guid}", async _ + => await _dbSet.Where(t => t.Name == type.ToString()).FirstOrDefaultAsync()); } } \ No newline at end of file