netmash/Netmash.Extensions.Caching/DistributedCachingExtension...

23 lines
918 B
C#

using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;
using System.Threading;
using System.Threading.Tasks;
namespace Netmash.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);
}
}
}