using PostFundManagement.Application.Abstractions; using PostFundManagement.Application.Abstractions.Services; using PostFundManagement.Application.Services.Shared; using static PostFundManagement.Domain.Extensions.Constants; namespace PostFundManagement.Application.Services.Tools; public sealed class ToolCacheService(ToolService service, IDistributedCache cache, IFeatureManager featureManager) : ServiceCacheBase, IToolService { public async Task> EncodeLong(long id, CancellationToken cancellationToken) => await Result.Try((Func>>)(async () => { if (!await featureManager.IsEnabledAsync(ServiceCachingFeature)) return await service.EncodeLong(id, cancellationToken); var cacheKey = HashService.ToMd5Hash($"encodelonghash-{id}").Value; var cached = await cache.GetStringAsync(cacheKey, cancellationToken); if(string.IsNullOrWhiteSpace(cached)) { var result = (await service.EncodeLong(id).ConfigureAwait(false)); if (result.IsFailed) return result; await cache.SetStringAsync(cacheKey, result.Value, cacheOptions, cancellationToken); return result.Value; } return Result.Ok(cached); })); }