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

23 lines
918 B
C#
Raw Normal View History

2021-04-25 03:50:14 +03:00
using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;
using System.Threading;
using System.Threading.Tasks;
2023-01-12 01:01:36 +02:00
namespace Netmash.Extensions.Caching
2021-04-25 03:50:14 +03:00
{
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);
}
}
}