netmash/NDB.Extensions.Caching/DistributedCachingExtension...

23 lines
914 B
C#

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<T>(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<T> GetAsync<T>(this IDistributedCache distributedCache, string key, CancellationToken token = default) where T : class
{
var result = await distributedCache.GetStringAsync(key, token);
return result == null ? null : JsonConvert.DeserializeObject<T>(result);
}
}
}