feat(DefaultReadConfigQuery): add DefaultReadConfigQuery with caching and AutoMapper support
- Implemented DefaultReadConfigQuery and its handler for reading default configuration. - Added memory caching using IMemoryCache to improve performance. - Integrated AutoMapper to map Config entities to ConfigDto. - Included extension method for easier MediatR query invocation. - Added error handling for missing configuration records.
This commit is contained in:
parent
e57e9e1834
commit
dee424e7db
@ -0,0 +1,87 @@
|
||||
using AutoMapper;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Exceptions;
|
||||
using EnvelopeGenerator.Application.Common.Dto;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace EnvelopeGenerator.Application.Configs;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class DefaultReadConfigQuery : IRequest<ConfigDto>
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static readonly Guid MemoryCacheKey = Guid.NewGuid();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static class DefaultReadConfigQueryExtensions
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="cancel"></param>
|
||||
/// <returns></returns>
|
||||
public static Task<ConfigDto> ReadDefaultConfigAsync(this ISender sender, CancellationToken cancel = default)
|
||||
=> sender.Send(new DefaultReadConfigQuery(), cancel);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class DefaultReadConfigQueryHandler : IRequestHandler<DefaultReadConfigQuery, ConfigDto>
|
||||
{
|
||||
private readonly IRepository<Config> _repo;
|
||||
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
private readonly IMemoryCache _cache;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="repo"></param>
|
||||
/// <param name="mapper"></param>
|
||||
/// <param name="cache"></param>
|
||||
public DefaultReadConfigQueryHandler(IRepository<Config> repo, IMapper mapper, IMemoryCache cache)
|
||||
{
|
||||
_repo = repo;
|
||||
_mapper = mapper;
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancel"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ConfigDto> Handle(DefaultReadConfigQuery request, CancellationToken cancel)
|
||||
{
|
||||
var config = await _cache.GetOrCreateAsync(DefaultReadConfigQuery.MemoryCacheKey, async entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5);
|
||||
var configs = await _repo.GetAllAsync(cancel);
|
||||
var defaultConfig = configs.FirstOrDefault();
|
||||
var defaultConfigDto = _mapper.Map<ConfigDto>(defaultConfig);
|
||||
return defaultConfigDto;
|
||||
});
|
||||
|
||||
if(config is null)
|
||||
{
|
||||
_cache.Remove(DefaultReadConfigQuery.MemoryCacheKey);
|
||||
throw new NotFoundException("No configuration record is found.");
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user