namespace OSS.Tools.Cache
{
///
/// 缓存委托方法扩展处理
///
public static class FuncCacheExtension
{
#region 缓存获取
///
/// 获取缓存数据,如果没有则添加
///
///
/// key
/// 获取原始数据方法
/// 滚动过期时长,访问后自动延长
/// 缓存击穿保护秒数,默认值10。
/// 来源名称
///
public static Task WithCacheAsync(this Func> getFunc, string cacheKey,
TimeSpan slidingExpiration, int hitProtectedSeconds = 10, string sourceName = "")
{
return CacheHelper.GetOrSetAsync(cacheKey, getFunc, new CacheTimeOptions() { sliding_expiration = slidingExpiration }, null, hitProtectedSeconds, sourceName);
}
///
/// 获取缓存数据【同时添加缓存击穿保护】,如果没有则添加
///
///
/// key
/// 获取原始数据方法
/// 滚动过期时长,访问后自动延长
/// 设置缓存前验证原始数据,false - 不满足设置缓存条件(会触发验证缓存击穿保护), true - 满足设置缓存条件
/// 缓存击穿保护秒数,默认值10。
/// 来源名称
///
public static Task WithCacheAsync(this Func> getFunc, string cacheKey, TimeSpan slidingExpiration,
Func? beforeSettingChecker, int hitProtectedSeconds = 10,
string sourceName = "")
{
return CacheHelper.GetOrSetAsync(cacheKey, getFunc, new CacheTimeOptions() { sliding_expiration = slidingExpiration }, beforeSettingChecker, hitProtectedSeconds, sourceName);
}
///
/// 获取缓存数据【同时添加缓存击穿保护】,如果没有则添加
///
///
/// key
/// 获取原始数据方法
/// 固定过期时长,设置后到时过期
/// 缓存击穿保护秒数,默认值10。
/// 来源名称
///
public static Task WithAbsoluteCacheAsync(this Func> getFunc, string cacheKey, TimeSpan absoluteExpiration, int hitProtectedSeconds = 10, string sourceName = "")
{
return CacheHelper.GetOrSetAsync(cacheKey, getFunc, new CacheTimeOptions() { absolute_expiration_relative_to_now = absoluteExpiration }, null
, hitProtectedSeconds, sourceName);
}
///
/// 获取缓存数据【同时添加缓存击穿保护】,如果没有则添加
///
///
/// key
/// 获取原始数据方法
/// 固定过期时长,设置后到时过期
/// 设置缓存前验证原始数据,false - 不满足设置缓存条件(会触发验证缓存击穿保护), true - 满足设置缓存条件
/// 缓存击穿保护秒数,默认值10。
/// 来源名称
///
public static Task WithAbsoluteCacheAsync(this Func> getFunc, string cacheKey, TimeSpan absoluteExpiration,
Func? beforeSettingChecker, int hitProtectedSeconds = 10, string sourceName = "")
{
return CacheHelper.GetOrSetAsync(cacheKey, getFunc,
new CacheTimeOptions()
{
absolute_expiration_relative_to_now = absoluteExpiration
},
beforeSettingChecker, hitProtectedSeconds, sourceName);
}
///
/// 获取缓存数据,如果没有则添加
///
/// 数据类型
///
/// 获取原始数据方法
/// 缓存时间信息
/// 设置缓存前验证原始数据,false - 不满足设置缓存条件(会触发验证缓存击穿保护), true - 满足设置缓存条件
/// 缓存击穿保护秒数,默认值10。
/// 来源名称
///
public static Task WithCacheAsync(this Func> getFunc, string cacheKey, CacheTimeOptions cacheTimeOpt,
Func? beforeSettingChecker = null, int hitProtectedSeconds = 10, string sourceName = "")
{
return CacheHelper.GetOrSetAsync(cacheKey, getFunc, cacheTimeOpt, beforeSettingChecker, hitProtectedSeconds, sourceName);
}
#endregion
}
}