diff --git a/Falcon.SugarApi/ApiDefinistions/ApiControllerBase.cs b/Falcon.SugarApi/ApiDefinistions/ApiControllerBase.cs index 67a3a96..e254fc2 100644 --- a/Falcon.SugarApi/ApiDefinistions/ApiControllerBase.cs +++ b/Falcon.SugarApi/ApiDefinistions/ApiControllerBase.cs @@ -259,7 +259,6 @@ namespace Falcon.SugarApi.ApiDefinistions } return this.Cache.GetString(key); } - /// /// 设置缓存 /// @@ -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 }); } - /// /// 获取缓存的对象 /// @@ -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 }); + } + /// + /// 尝试获取key指定的缓冲数据,如果不存在通过cacheDataBuilder方法获取缓冲数据,获取缓冲数据后存入分布式缓存,并返回数据 + /// + /// 缓存的键 + /// + /// 存储过期时间 + /// 缓冲数据 + protected string? GetCache(string key,Func? 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; + } + /// + /// 尝试获取key指定的缓冲数据,如果不存在通过cacheDataBuilder方法获取缓冲数据,获取缓冲数据后存入分布式缓存,并返回数据 + /// + /// + /// + /// + /// + /// + protected T? GetCache(string key,Func? cacheDataBuilder = null,TimeSpan? span = null) { + T? result = this.GetCache(key); + if(result != null) { + return result; + } + if(cacheDataBuilder != null) { + result = cacheDataBuilder.Invoke(); + } + if(result != null) { + this.SetCache(key,result,span); + } + return result; } #endregion }