Files
EnvelopeGenerator/EnvelopeGenerator.Application/Services/ConfigService.cs
TekH 64e0a4f749 Centralize cache key definitions in CacheKey class
Refactored cache key usage by introducing a new static CacheKey class in the Application.Common namespace. Replaced the private DefaultConfigCacheId in ConfigService with CacheKey.DefaultConfig. Updated using statements accordingly. This change improves maintainability by centralizing cache key management and sets the stage for future cache key consolidation.
2026-03-06 11:41:47 +01:00

81 lines
2.8 KiB
C#

using AutoMapper;
using DigitalData.Core.Application;
using DigitalData.Core.Abstraction.Application.DTO;
using EnvelopeGenerator.Domain.Entities;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using EnvelopeGenerator.Application.Common.Dto;
using EnvelopeGenerator.Application.Common.Interfaces.Repositories;
using EnvelopeGenerator.Application.Common.Interfaces.Services;
using EnvelopeGenerator.Application.Common;
namespace EnvelopeGenerator.Application.Services;
/// <summary>
///
/// </summary>
[Obsolete("Use MediatR")]
public class ConfigService : ReadService<IConfigRepository, ConfigDto, Config, int>, IConfigService
{
private readonly IMemoryCache _cache;
private readonly ILogger<ConfigService> _logger;
/// <summary>
///
/// </summary>
/// <param name="repository"></param>
/// <param name="mapper"></param>
/// <param name="memoryCache"></param>
/// <param name="logger"></param>
public ConfigService(IConfigRepository repository, IMapper mapper, IMemoryCache memoryCache, ILogger<ConfigService> logger) : base(repository, mapper)
{
_cache = memoryCache;
_logger = logger;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public async Task<DataResult<ConfigDto>> ReadFirstAsync()
{
var config = await _repository.ReadFirstAsync();
return config is null
? Result.Fail<ConfigDto>().Notice(LogLevel.Error, Flag.DataIntegrityIssue, "There is no configuration in DB.")
: Result.Success(_mapper.Map<ConfigDto>(config));
}
/// <summary>
/// Reads the default configuration asynchronously.
/// </summary>
/// <remarks>
/// The configuration is cached in memory upon the first retrieval. If the configuration is updated,
/// the application needs to be restarted for the changes to take effect as the memory cache will not be updated automatically.
/// </remarks>
/// <returns>
/// A task that represents the asynchronous read operation. The task result contains the default configuration as a <see cref="ConfigDto"/>.
/// </returns>
/// <exception cref="InvalidOperationException">
/// Thrown when the default configuration cannot be found.
/// </exception>
public async Task<ConfigDto> ReadDefaultAsync()
{
var config = await _cache.GetOrCreateAsync(CacheKey.DefaultConfig, _ => ReadFirstAsync().ThenAsync(
Success: config => config,
Fail: (mssg, ntc) =>
{
_logger.LogNotice(ntc);
throw new InvalidOperationException("Default configuration cannot find.");
})
);
return config!;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public async Task<string> ReadDefaultSignatureHost() => (await ReadDefaultAsync()).SignatureHost;
}