using AngleSharp.Dom; using EnvelopeGenerator.Application.Configurations; using EnvelopeGenerator.Application.Contracts; using EnvelopeGenerator.Application.Extensions; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Options; namespace EnvelopeGenerator.Application.Services { public class EnvelopeReceiverCache : IEnvelopeReceiverCache { private readonly EnvelopeReceiverCacheParams _cacheParams; private readonly DistributedCacheEntryOptions _codeCacheOptions; private readonly IDistributedCache _cache; public EnvelopeReceiverCache(IOptions cacheParamOptions, IDistributedCache cache) { _cacheParams = cacheParamOptions.Value; _codeCacheOptions = new() { AbsoluteExpirationRelativeToNow = cacheParamOptions.Value.CodeCacheValidityPeriod }; _cache = cache; } public async Task GetSmsCodeAsync(string envelopeReceiverId) { var code_key = string.Format(_cacheParams.CodeCacheKeyFormat, envelopeReceiverId); return await _cache.GetStringAsync(code_key); } public async Task SetSmsCodeAsync(string envelopeReceiverId, string code) { // set key var code_key = string.Format(_cacheParams.CodeCacheKeyFormat, envelopeReceiverId); await _cache.SetStringAsync(code_key, code, _codeCacheOptions); // set expiration var code_expiration_key = string.Format(_cacheParams.CodeExpirationCacheKeyFormat, envelopeReceiverId); var expiration = DateTime.Now + _cacheParams.CodeCacheValidityPeriod; await _cache.SetDateTimeAsync(code_expiration_key, expiration, _codeCacheOptions); return expiration; } public async Task GetSmsCodeExpirationAsync(string envelopeReceiverId) { var code_expiration_key = string.Format(_cacheParams.CodeExpirationCacheKeyFormat, envelopeReceiverId); return await _cache.GetDateTimeAsync(code_expiration_key); } } }