#region Copyright (C) 2016 Kevin (OSS开源系列) 公众号:OSSCore
/***************************************************************************
* 文件功能描述:全局插件 - 缓存插件辅助类
*
* 创建人: Kevin
* 创建人Email:1985088337@qq.com
*
*
*****************************************************************************/
#endregion
using System;
using System.Threading.Tasks;
namespace OSS.Tools.Cache
{
///
/// 缓存的辅助类
///
public static class CacheHelper
{
private static readonly DefaultToolCache defaultCache = new DefaultToolCache();
///
/// 缓存模块提供者
///
public static Func CacheProvider { get; set; }
///
/// 通过模块名称获取
///
///
///
public static IToolCache GetCache(string cacheModule)
{
if (string.IsNullOrEmpty(cacheModule))
cacheModule = "default";
return CacheProvider?.Invoke(cacheModule) ?? defaultCache;
}
///
/// 添加时间段过期缓存
/// 如果存在则更新
///
/// 添加缓存对象类型
/// 添加对象的key
/// 值
/// 相对过期的TimeSpan
/// 模块名称
/// 是否添加成功
public static bool Set(string key, T obj, TimeSpan slidingExpiration,
string moduleName = "default")
{
return GetCache(moduleName).Set(key, obj, slidingExpiration);
}
///
/// 添加固定过期时间缓存,如果存在则更新
///
/// 添加缓存对象类型
/// 添加对象的key
/// 值
/// 绝对过期时间,不为空则按照绝对过期时间计算
/// 模块名称
/// 是否添加成功
public static bool Set(string key, T obj, DateTime absoluteExpiration,
string moduleName = "default")
{
return GetCache(moduleName).Set(key, obj, absoluteExpiration);
}
///
/// 获取缓存对象
///
/// 获取缓存对象类型
/// key
/// 模块名称
/// 获取指定key对应的值
public static T Get(string key, string moduleName = "default")
{
return GetCache(moduleName).Get(key);
}
///
/// 获取缓存数据,如果没有则添加
///
///
/// key
/// 没有数据时,通过此方法获取原始数据
/// 绝对过期时间
/// 模块名称
///
public static Task Get(string cacheKey, Func> getFunc, DateTime absoluteExpiration, string moduleName = "default")
{
return Get(cacheKey, getFunc, TimeSpan.Zero, absoluteExpiration,null, moduleName);
}
///
/// 获取缓存数据,如果没有则添加
///
///
/// key
/// 没有数据时,通过此方法获取原始数据
/// 过期时长,访问后自动延长
/// 模块名称
///
public static Task Get(string cacheKey, Func> getFunc, TimeSpan slidingExpiration, string moduleName = "default")
{
return Get(cacheKey, getFunc, slidingExpiration,null, null, moduleName);
}
///
/// 获取缓存数据【同时添加缓存击穿保护】,如果没有则添加
///
///
/// key
/// 没有数据时,通过此方法获取原始数据
/// 绝对过期时间
/// 保护到期时间
/// 模块名称
///
public static Task ProtectedGet(string cacheKey, Func> getFunc,
DateTime absoluteExpiration, DateTime hitProtectExpiration, string moduleName = "default")
{
return Get(cacheKey, getFunc,TimeSpan.Zero, absoluteExpiration,hitProtectExpiration, moduleName);
}
///
/// 获取缓存数据【同时添加缓存击穿保护】,如果没有则添加
///
///
/// key
/// 没有数据时,通过此方法获取原始数据
/// 过期时长,访问后自动延长
/// 保护到期时间
/// 模块名称
///
public static Task ProtectedGet(string cacheKey, Func> getFunc,
TimeSpan slidingExpiration, DateTime hitProtectExpiration, string moduleName = "default")
{
return Get(cacheKey, getFunc, slidingExpiration, null, hitProtectExpiration, moduleName);
}
private static async Task Get(string cacheKey, Func> createFunc
, TimeSpan slidingExpiration, DateTime? absoluteExpiration,
DateTime? hitProtectExpiration, string moduleName)
{
var obj = GetCache(moduleName).Get>(cacheKey);
if (!obj.Equals(null))
{
if (obj.IsNull)
return default(RType);
return obj.Data;
}
var data = await createFunc.Invoke();
if (data==null)
{
if (hitProtectExpiration.HasValue)
{
var safeData = new HitProtectCahce();
Set(cacheKey, safeData, hitProtectExpiration.Value);
}
}
else
{
var safeData = new HitProtectCahce(data);
if (absoluteExpiration.HasValue)
Set(cacheKey, safeData, absoluteExpiration.Value);
else
Set(cacheKey, safeData, slidingExpiration);
}
return data;
}
///
/// 移除缓存对象
///
///
/// 模块名称
/// 是否成功
public static bool Remove(string key, string moduleName = "default")
{
return GetCache(moduleName).Remove(key);
}
}
internal struct HitProtectCahce
{
public HitProtectCahce(TT data)
{
IsNull = false;
Data = data;
}
public HitProtectCahce(bool isNull=true)
{
IsNull = isNull;
Data = default(TT);
}
public TT Data { get; set; }
public bool IsNull { get; set; }
}
}