From c8db46053fc3eb066498068cd2b55a0c9dfc8c2a Mon Sep 17 00:00:00 2001
From: FalconFly <12919280+falconfly@user.noreply.gitee.com>
Date: Mon, 14 Aug 2023 15:41:52 +0800
Subject: [PATCH] =?UTF-8?q?ApiControllerBase=E5=A2=9E=E5=8A=A0=E7=BC=93?=
=?UTF-8?q?=E5=86=B2=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../ApiDefinistions/ApiControllerBase.cs | 48 +++++++++++++++++--
1 file changed, 44 insertions(+), 4 deletions(-)
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
}