123456789101112131415161718192021222324252627282930313233 |
- using Microsoft.Extensions.Caching.Distributed;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO.Compression;
- namespace SMP.Common.Extensions
- {
- public static class CacheExtensions
- {
- public static Task SetAsync(this IDistributedCache cache, string key, object data, DistributedCacheEntryOptions options, CancellationToken token = default)
- {
- string josnValue = Newtonsoft.Json.JsonConvert.SerializeObject(data);
- var bytes = UTF8Encoding.UTF8.GetBytes(josnValue);
- return cache.SetAsync(key, bytes, options, token);
- }
- public static async Task<T?> GetAsync<T>(this IDistributedCache cache, string key, CancellationToken token = default)
- {
- var cacheV = await cache.GetAsync(key, token);
- if (cacheV == null)
- {
- return default;
- }
-
- string orijson = System.Text.UTF8Encoding.UTF8.GetString(cacheV);
- var a = await Task<T>.Run(() => Newtonsoft.Json.JsonConvert.DeserializeObject<T>(orijson));
- return a;
- }
- }
- }
|