ApiControllerBase增加缓冲方法支持
This commit is contained in:
parent
459dcf8548
commit
c8db46053f
|
@ -259,7 +259,6 @@ namespace Falcon.SugarApi.ApiDefinistions
|
|||
}
|
||||
return this.Cache.GetString(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置缓存
|
||||
/// </summary>
|
||||
|
@ -270,7 +269,7 @@ namespace Falcon.SugarApi.ApiDefinistions
|
|||
if(this.Cache == null) {
|
||||
return;
|
||||
}
|
||||
if(key.IsNullOrEmpty() || val.IsNotNullOrEmpty()) {
|
||||
if(key.IsNullOrEmpty() || val.IsNullOrEmpty()) {
|
||||
return;
|
||||
}
|
||||
if(span == null) {
|
||||
|
@ -278,7 +277,6 @@ namespace Falcon.SugarApi.ApiDefinistions
|
|||
}
|
||||
this.Cache.SetString(key,val,new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = span });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存的对象
|
||||
/// </summary>
|
||||
|
@ -310,7 +308,49 @@ namespace Falcon.SugarApi.ApiDefinistions
|
|||
return;
|
||||
}
|
||||
var str = JsonSerializer.Serialize(val);
|
||||
this.Cache.SetString(key,str,new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = span });
|
||||
this.SetCache(key,str,span);
|
||||
//this.Cache.SetString(key,str,new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = span });
|
||||
}
|
||||
/// <summary>
|
||||
/// 尝试获取key指定的缓冲数据,如果不存在通过cacheDataBuilder方法获取缓冲数据,获取缓冲数据后存入分布式缓存,并返回数据
|
||||
/// </summary>
|
||||
/// <param name="key">缓存的键</param>
|
||||
/// <param name="cacheDataBuilder"></param>
|
||||
/// <param name="span">存储过期时间</param>
|
||||
/// <returns>缓冲数据</returns>
|
||||
protected string? GetCache(string key,Func<string>? cacheDataBuilder = null,TimeSpan? span = null) {
|
||||
string? str = this.GetCache(key);
|
||||
if(str.IsNotNullOrEmpty()) {
|
||||
return str;
|
||||
}
|
||||
if(cacheDataBuilder != null) {
|
||||
str = cacheDataBuilder?.Invoke();
|
||||
}
|
||||
if(str.IsNotNullOrEmpty()) {
|
||||
this.SetCache(key,str,span);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
/// <summary>
|
||||
/// 尝试获取key指定的缓冲数据,如果不存在通过cacheDataBuilder方法获取缓冲数据,获取缓冲数据后存入分布式缓存,并返回数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="cacheDataBuilder"></param>
|
||||
/// <param name="span"></param>
|
||||
/// <returns></returns>
|
||||
protected T? GetCache<T>(string key,Func<T>? cacheDataBuilder = null,TimeSpan? span = null) {
|
||||
T? result = this.GetCache<T>(key);
|
||||
if(result != null) {
|
||||
return result;
|
||||
}
|
||||
if(cacheDataBuilder != null) {
|
||||
result = cacheDataBuilder.Invoke();
|
||||
}
|
||||
if(result != null) {
|
||||
this.SetCache(key,result,span);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user