137 lines
5.2 KiB
C#
137 lines
5.2 KiB
C#
|
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<AbpDistributedCacheOptions> options,IOptions<RedisCacheOptions> redisCachOptions)
|
|||
|
{
|
|||
|
this.serviceProvider = serviceProvider;
|
|||
|
this.abpDistributedCacheOptions = options.Value;
|
|||
|
this.redisCacheOptions = redisCachOptions.Value;
|
|||
|
}
|
|||
|
|
|||
|
private IDistributedCache<CacheItem<T>> GetDistributedCache<T>()
|
|||
|
=> serviceProvider.GetService<IDistributedCache<CacheItem<T>>>();
|
|||
|
|
|||
|
#region ICacheProvide 实现
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 获取
|
|||
|
/// </summary>
|
|||
|
/// <typeparam name="T"></typeparam>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <returns></returns>
|
|||
|
/// <exception cref="ArgumentNullException"></exception>
|
|||
|
public async Task<T> GetAsync<T>(string key)
|
|||
|
{
|
|||
|
if (string.IsNullOrWhiteSpace(key))
|
|||
|
throw new ArgumentNullException(nameof(key));
|
|||
|
|
|||
|
var cache = GetDistributedCache<T>();
|
|||
|
var result = await cache.GetAsync(key);
|
|||
|
|
|||
|
return result is null ? default : result.Value ?? default;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 获取缓存过期配置
|
|||
|
/// </summary>
|
|||
|
/// <param name="absExpiration">绝对过期</param>
|
|||
|
/// <param name="relativeExpiration">相对过期</param>
|
|||
|
/// <param name="slidExpiration">滑动过期</param>
|
|||
|
/// <returns></returns>
|
|||
|
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,
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 获取
|
|||
|
/// </summary>
|
|||
|
/// <typeparam name="T"></typeparam>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="func"></param>
|
|||
|
/// <param name="relativeExpiration"></param>
|
|||
|
/// <returns></returns>
|
|||
|
/// <exception cref="ArgumentNullException"></exception>
|
|||
|
public async Task<T> GetAsync<T>(string key, Func<T> func, int relativeExpiration)
|
|||
|
{
|
|||
|
if (string.IsNullOrWhiteSpace(key))
|
|||
|
throw new ArgumentNullException(nameof(key));
|
|||
|
|
|||
|
var result = await GetAsync<T>(key);
|
|||
|
if (result is null)
|
|||
|
result = func();
|
|||
|
await SaveAsync(key, result, relativeExpiration);
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 删除key
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <returns></returns>
|
|||
|
/// <exception cref="ArgumentNullException"></exception>
|
|||
|
public async Task RemoveAsync(string key)
|
|||
|
{
|
|||
|
if (string.IsNullOrWhiteSpace(key))
|
|||
|
throw new ArgumentNullException(nameof(key));
|
|||
|
var cache = serviceProvider.GetService<IDistributedCache>();
|
|||
|
await cache.RemoveAsync(key);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 添加/保存
|
|||
|
/// </summary>
|
|||
|
/// <typeparam name="T"></typeparam>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="value"></param>
|
|||
|
/// <param name="relativeExpiration"></param>
|
|||
|
/// <returns></returns>
|
|||
|
/// <exception cref="ArgumentNullException"></exception>
|
|||
|
public async Task SaveAsync<T>(string key, T value, int? relativeExpiration = null)
|
|||
|
{
|
|||
|
if (string.IsNullOrWhiteSpace(key))
|
|||
|
throw new ArgumentNullException(nameof(key));
|
|||
|
var option = GetDistributedCacheEntryOptions(relativeExpiration: relativeExpiration);
|
|||
|
var cache = GetDistributedCache<T>();
|
|||
|
await cache.SetAsync(key, new CacheItem<T>(value, option), option);
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|