using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.StackExchangeRedis; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Volo.Abp.Caching; using Volo.Abp.DependencyInjection; namespace Syc.Abp.Caching.Redis { public partial class RedisCacheProvide : IRedisCacheProvide, ITransientDependency { private readonly IServiceProvider serviceProvider; private readonly AbpDistributedCacheOptions abpDistributedCacheOptions; private readonly RedisCacheOptions redisCacheOptions; public RedisCacheProvide(IServiceProvider serviceProvider,IOptions options,IOptions redisCachOptions) { this.serviceProvider = serviceProvider; this.abpDistributedCacheOptions = options.Value; this.redisCacheOptions = redisCachOptions.Value; } private IDistributedCache> GetDistributedCache() => serviceProvider.GetService>>(); #region ICacheProvide 实现 /// /// 获取 /// /// /// /// /// public async Task GetAsync(string key) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); var cache = GetDistributedCache(); var result = await cache.GetAsync(key); return result is null ? default : result.Value ?? default; } /// /// 获取缓存过期配置 /// /// 绝对过期 /// 相对过期 /// 滑动过期 /// private DistributedCacheEntryOptions GetDistributedCacheEntryOptions(int? absExpiration = null, int? relativeExpiration = null, int? slidExpiration = null) { DateTimeOffset? absoluteExpiration = null; if (absExpiration.HasValue) absoluteExpiration = DateTimeOffset.Now.AddSeconds(absExpiration.Value); TimeSpan? absoluteExpirationRelativeToNow = null; if (relativeExpiration.HasValue) absoluteExpirationRelativeToNow = TimeSpan.FromSeconds(relativeExpiration.Value); TimeSpan? slidingExpiration = null; if (slidExpiration.HasValue) slidingExpiration = TimeSpan.FromSeconds(slidExpiration.Value); return new DistributedCacheEntryOptions() { AbsoluteExpiration = absoluteExpiration, AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow, SlidingExpiration = slidingExpiration, }; } /// /// 获取 /// /// /// /// /// /// /// public async Task GetAsync(string key, Func func, int relativeExpiration) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); var result = await GetAsync(key); if (result is null) result = func(); await SaveAsync(key, result, relativeExpiration); return result; } /// /// 删除key /// /// /// /// public async Task RemoveAsync(string key) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); var cache = serviceProvider.GetService(); await cache.RemoveAsync(key); } /// /// 添加/保存 /// /// /// /// /// /// /// public async Task SaveAsync(string key, T value, int? relativeExpiration = null) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); var option = GetDistributedCacheEntryOptions(relativeExpiration: relativeExpiration); var cache = GetDistributedCache(); await cache.SetAsync(key, new CacheItem(value, option), option); } #endregion } }