using Microsoft.Extensions.Caching.Distributed; using System; using System.Text; namespace Infrastructure.Extensions { public static class DistributedCacheExtensions { public static void Set(this IDistributedCache cache, object key, T value) { if (cache is null) { throw new ArgumentNullException(nameof(cache)); } if (key is null) { throw new ArgumentNullException(nameof(key)); } cache.SetString(key.ToString(), value.ToJson(), new DistributedCacheEntryOptions { AbsoluteExpiration = DateTimeOffset.UtcNow.AddHours(1) }); } public static T Get(this IDistributedCache cache, object key) { if (cache is null) { throw new ArgumentNullException(nameof(cache)); } if (key is null) { throw new ArgumentNullException(nameof(key)); } var value = cache.GetString(key.ToString()); if (value != null) { return value.FromJson(); } return default; } public static void Remove(this IDistributedCache cache, object key) { if (cache is null) { throw new ArgumentNullException(nameof(cache)); } if (key is null) { throw new ArgumentNullException(nameof(key)); } cache.Remove(key.ToString()); } } }