using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; using Volo.Abp.Caching; namespace Win.Sfs.Shared.CacheBase { public static class CachingExtensions { private static DistributedCacheEntryOptions CreateDistributedCacheEntryOptions(int minutes) { var options = new DistributedCacheEntryOptions(); if (minutes != CacheStrategyConst.NEVER) { options.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(minutes); } return options; } /// /// 获取或添加缓存 /// /// /// /// /// /// /// public static async Task GetOrAddAsync( this IDistributedCache cache, string key, Func> factory, int minutes) where TCacheItem : class { TCacheItem cacheItem; var result = await cache.GetAsync(key); if (result == null) { cacheItem = await factory.Invoke(); await cache.SetAsync(key, cacheItem, minutes); } else { cacheItem = result; } return cacheItem; } // public static async Task GetOrAddAsync(this IDistributedCache cache, string key, TCacheItem cacheItem, int minutes) // { // // var result = await cache.GetStringAsync(GetKey(typeof(TCacheItem), key)); // if (string.IsNullOrEmpty(result)) // { // var options = new DistributedCacheEntryOptions(); // if (minutes != CacheStrategyConst.NEVER) // { // options.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(minutes); // } // await cache.SetStringAsync(GetKey(typeof(TCacheItem), key), cacheItem.ToJson(), options); // } // else // { // cacheItem = result.FromJson(); // } // return cacheItem; // } public static async Task> GetManyAsync( this IDistributedCache cache, IEnumerable keys) where TCacheItem : class { var cacheItems = await cache.GetManyAsync(keys, null, true); return cacheItems.Select(p => p.Value); } public static async Task SetAsync( this IDistributedCache cache, string key, TCacheItem cacheItem, int minutes) where TCacheItem : class { var options = CreateDistributedCacheEntryOptions(minutes); await cache.SetAsync(key, cacheItem, options, null, true); } public static async Task SetManyAsync( this IDistributedCache cache, IEnumerable> cacheItems, int minutes) where TCacheItem : class { var options = CreateDistributedCacheEntryOptions(minutes); await cache.SetManyAsync(cacheItems, options); } public static async Task DeleteAsync( this IDistributedCache cache, string key) where TCacheItem : class { var result = await cache.GetAsync(key); if (result != null) { await cache.RemoveAsync(key); } } private static string GetKey(Type type, string key) { return $"{type.Name}:{key}"; } } }