using DD.Persistence.Repositories; using Microsoft.Extensions.Caching.Memory; namespace DD.Persistence.API.Services; public class ChangeLogService { private readonly IMemoryCache memoryCache; private readonly IChangeLogCommitRepository commitRepository; private readonly IChangeLogRepository repository; private readonly TimeSpan? AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(60); public ChangeLogService( IMemoryCache memoryCache, IChangeLogCommitRepository commitRepository, IChangeLogRepository repository) { this.memoryCache = memoryCache; this.commitRepository = commitRepository; this.repository = repository; } private async Task GetOrCreateAsync(Guid idUser, string comment, CancellationToken token) { var key = (idUser, comment); var commitId = await memoryCache.GetOrCreateAsync(key, async (cacheEntry) => { cacheEntry.AbsoluteExpirationRelativeToNow = AbsoluteExpirationRelativeToNow; var commitId = await commitRepository.Add(idUser, comment, token); return commitId; }); return commitId; } public async Task AddRange( Guid idUser, string comment, CancellationToken token) { var cimmitId = await GetOrCreateAsync(idUser, comment, token); repository.AddRange(new); } }