using JSMR.Application.Common.Caching; using Microsoft.Extensions.Caching.Memory; namespace JSMR.Infrastructure.Caching; public class MemoryCacheAdapter(IMemoryCache memoryCache) : ICache { public ValueTask GetAsync(string key, CancellationToken cancellationToken = default) { memoryCache.TryGetValue(key, out T? value); return ValueTask.FromResult(value); } public ValueTask SetAsync(string key, T value, CacheEntryOptions options, CancellationToken cancellationToken = default) { MemoryCacheEntryOptions memoryCacheOptions = new(); if (options.AbsoluteExpirationRelativeToNow != null) memoryCacheOptions.SetAbsoluteExpiration(options.AbsoluteExpirationRelativeToNow.Value); if (options.AbsoluteExpiration != null) memoryCacheOptions.SetAbsoluteExpiration(options.AbsoluteExpiration.Value); if (options.SlidingExpiration != null) memoryCacheOptions.SetSlidingExpiration(options.SlidingExpiration.Value); memoryCache.Set(key, value, memoryCacheOptions); return ValueTask.CompletedTask; } }