Add in-memory caching to ReadDefaultConfigQueryHandler

Injected IMemoryCache into ReadDefaultConfigQueryHandler and updated the Handle method to cache the default configuration for 30 minutes. This reduces database queries and improves performance for frequently accessed configuration data.
This commit is contained in:
2026-03-06 11:54:50 +01:00
parent 64e0a4f749
commit 41e0d4691b

View File

@@ -1,10 +1,13 @@
using AutoMapper; using AutoMapper;
using DigitalData.Core.Abstraction.Application.Repository; using DigitalData.Core.Abstraction.Application.Repository;
using DigitalData.Core.Exceptions; using DigitalData.Core.Exceptions;
using EnvelopeGenerator.Application.Common;
using EnvelopeGenerator.Application.Common.Dto; using EnvelopeGenerator.Application.Common.Dto;
using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Domain.Entities;
using MediatR; using MediatR;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
namespace EnvelopeGenerator.Application.Configuration.Queries; namespace EnvelopeGenerator.Application.Configuration.Queries;
@@ -28,15 +31,19 @@ public class ReadDefaultConfigQueryHandler : IRequestHandler<ReadDefaultConfigQu
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly IMemoryCache _cache;
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="repo"></param> /// <param name="repo"></param>
/// <param name="mapper"></param> /// <param name="mapper"></param>
public ReadDefaultConfigQueryHandler(IRepository<Config> repo, IMapper mapper) /// <param name="cache"></param>
public ReadDefaultConfigQueryHandler(IRepository<Config> repo, IMapper mapper, IMemoryCache cache)
{ {
_repo = repo; _repo = repo;
_mapper = mapper; _mapper = mapper;
_cache = cache;
} }
/// <summary> /// <summary>
@@ -48,10 +55,15 @@ public class ReadDefaultConfigQueryHandler : IRequestHandler<ReadDefaultConfigQu
/// <exception cref="InvalidOperationException"></exception> /// <exception cref="InvalidOperationException"></exception>
public async Task<ConfigDto> Handle(ReadDefaultConfigQuery request, CancellationToken cancel) public async Task<ConfigDto> Handle(ReadDefaultConfigQuery request, CancellationToken cancel)
{ {
var config = request.EnforceSingleResult var config = await _cache.GetOrCreateAsync(CacheKey.DefaultConfig, entry =>
? await _repo.Query.SingleOrDefaultAsync(cancel) {
: await _repo.Query.FirstOrDefaultAsync(cancel) entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
return request.EnforceSingleResult
? _repo.Query.SingleOrDefaultAsync(cancel)
: _repo.Query.FirstOrDefaultAsync(cancel)
?? throw new NotFoundException("Default configuration could not be found. Ensure at least one configuration record exists in the database."); ?? throw new NotFoundException("Default configuration could not be found. Ensure at least one configuration record exists in the database.");
});
return _mapper.Map<ConfigDto>(config); return _mapper.Map<ConfigDto>(config);
} }
} }