using Microsoft.Extensions.Caching.Distributed; using Newtonsoft.Json; using System.Threading; using System.Threading.Tasks; namespace NDB.Extensions.Caching { internal static class DistributedCachingExtensions { public static async Task SetAsync(this IDistributedCache distributedCache, string key, T value, DistributedCacheEntryOptions options, CancellationToken token = default) where T : class { var v = JsonConvert.SerializeObject(value); await distributedCache.SetStringAsync(key, v, options, token); } public static async Task GetAsync(this IDistributedCache distributedCache, string key, CancellationToken token = default) where T : class { var result = await distributedCache.GetStringAsync(key, token); return result == null ? null : JsonConvert.DeserializeObject(result); } } }