diff --git a/Falcon.SugarApi/DatabaseDefinitions/Cache/HttpRuntimeCache.cs b/Falcon.SugarApi/DatabaseDefinitions/Cache/HttpRuntimeCache.cs
new file mode 100644
index 0000000..d1923db
--- /dev/null
+++ b/Falcon.SugarApi/DatabaseDefinitions/Cache/HttpRuntimeCache.cs
@@ -0,0 +1,52 @@
+using SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.Runtime.Caching;
+
+namespace Falcon.SugarApi.DatabaseDefinitions.Cache
+{
+ ///
+ /// HttpRuntimeCache 缓存
+ ///
+ public class HttpRuntimeCache : ICacheService
+ {
+ public void Add(string key, V value) {
+ ObjectCache cache = MemoryCache.Default;
+ cache[key] = value;
+ }
+ public void Add(string key, V value, int cacheDurationInSeconds) {
+ MemoryCache.Default.Add(key, value, new CacheItemPolicy() {
+ AbsoluteExpiration = DateTime.Now.AddSeconds(cacheDurationInSeconds)
+ });
+ }
+ public bool ContainsKey(string key) {
+ return MemoryCache.Default.Contains(key);
+ }
+ public V Get(string key) {
+ return (V)MemoryCache.Default.Get(key);
+ }
+ public IEnumerable GetAllKey() {
+ var keys = new List();
+ foreach (var cacheItem in MemoryCache.Default) {
+ keys.Add(cacheItem.Key);
+ }
+ return keys;
+ }
+ public V GetOrCreate(string cacheKey, Func create, int cacheDurationInSeconds = int.MaxValue) {
+ var cacheManager = MemoryCache.Default;
+ if (cacheManager.Contains(cacheKey)) {
+ return (V)cacheManager[cacheKey];
+ }
+ else {
+ var result = create();
+ cacheManager.Add(cacheKey, result, new CacheItemPolicy() {
+ AbsoluteExpiration = DateTime.Now.AddSeconds(cacheDurationInSeconds)
+ });
+ return result;
+ }
+ }
+ public void Remove(string key) {
+ MemoryCache.Default.Remove(key);
+ }
+ }
+}
diff --git a/Falcon.SugarApi/DatabaseDefinitions/Cache/SqlSugarRedisCache.cs b/Falcon.SugarApi/DatabaseDefinitions/Cache/SqlSugarRedisCache.cs
new file mode 100644
index 0000000..e065b9a
--- /dev/null
+++ b/Falcon.SugarApi/DatabaseDefinitions/Cache/SqlSugarRedisCache.cs
@@ -0,0 +1,83 @@
+using SqlSugar;
+using SugarRedis;
+using System;
+using System.Collections.Generic;
+
+namespace Falcon.SugarApi.DatabaseDefinitions.Cache
+{
+
+ ///
+ /// SqlSugar使用Redis缓冲。必须以单例实现
+ ///
+ public class SqlSugarRedisCache : ICacheService
+ {
+
+ //NUGET安装 SugarRedis (也可以自个实现)
+ //注意:SugarRedis 不要扔到构造函数里面, 一定要单例模式
+
+
+ ///
+ /// 数据缓冲服务
+ ///
+ public static SugarRedisClient Service { get; private set; } = null;
+
+ ///
+ /// 通过配置实现Redis缓冲,必须单例实现.
+ /// 默认:127.0.0.1:6379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=0
+ ///
+ public SqlSugarRedisCache() {
+ Service ??= new SugarRedisClient();
+ }
+
+ ///
+ /// 通过配置实现Redis缓冲,必须单例实现
+ ///
+ /// Redis链接字符串
+ public SqlSugarRedisCache(string redisConnectionString) {
+ Service ??= redisConnectionString.IsNullOrEmpty() ? new SugarRedisClient()
+ : new SugarRedisClient(redisConnectionString);
+ }
+
+ public void Add(string key, V value) {
+ Service.Set(key, value);
+ }
+
+ public void Add(string key, V value, int cacheDurationInSeconds) {
+ Service.Set(key, value, cacheDurationInSeconds);
+ }
+
+ public bool ContainsKey(string key) {
+ return Service.Exists(key);
+ }
+
+ public V Get(string key) {
+ return Service.Get(key);
+ }
+
+ public IEnumerable GetAllKey() {
+
+ return Service.SearchCacheRegex("SqlSugarDataCache.*");
+ }
+
+ public V GetOrCreate(string cacheKey, Func create, int cacheDurationInSeconds = int.MaxValue) {
+ if (ContainsKey(cacheKey)) {
+ var result = Get(cacheKey);
+ if (result == null) {
+ return create();
+ }
+ else {
+ return result;
+ }
+ }
+ else {
+ var result = create();
+ Add(cacheKey, result, cacheDurationInSeconds);
+ return result;
+ }
+ }
+
+ public void Remove(string key) {
+ Service.Remove(key);
+ }
+ }
+}
diff --git a/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs
index d7a5f84..e8b0523 100644
--- a/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs
+++ b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs
@@ -1,4 +1,5 @@
-using SqlSugar;
+using Falcon.SugarApi.DatabaseDefinitions.Cache;
+using SqlSugar;
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
@@ -15,6 +16,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// 是否使用log
///
public bool Log { get; set; }
+
///
/// 实例化SugarDb链接配置
///
@@ -47,5 +49,36 @@ namespace Falcon.SugarApi.DatabaseDefinitions
//}
};
}
+
+ private static ICacheService? CacheService = null;
+
+ ///
+ /// 通过配置实现Redis缓冲,必须单例实现.
+ /// 默认:127.0.0.1:6379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=0
+ ///
+ public void AddRedisCache() {
+ this.ConfigureExternalServices ??= new ConfigureExternalServices { };
+ this.ConfigureExternalServices.DataInfoCacheService = new SqlSugarRedisCache();
+ }
+
+ ///
+ /// 通过配置实现Redis缓冲,必须单例实现.
+ ///
+ public void AddRedisCache(string connectionString) {
+ if (connectionString.IsNullOrEmpty()) {
+ AddRedisCache();
+ return;
+ }
+ this.ConfigureExternalServices ??= new ConfigureExternalServices { };
+ this.ConfigureExternalServices.DataInfoCacheService = new SqlSugarRedisCache(connectionString);
+ }
+
+ ///
+ /// HttpRuntimeCache 缓存
+ ///
+ public void AddHttpRuntimeCache() {
+ this.ConfigureExternalServices ??= new ConfigureExternalServices { };
+ this.ConfigureExternalServices.DataInfoCacheService = new HttpRuntimeCache();
+ }
}
}
diff --git a/Falcon.SugarApi/Falcon.SugarApi.csproj b/Falcon.SugarApi/Falcon.SugarApi.csproj
index 3445cb5..a88ddc1 100644
--- a/Falcon.SugarApi/Falcon.SugarApi.csproj
+++ b/Falcon.SugarApi/Falcon.SugarApi.csproj
@@ -13,7 +13,9 @@
+
+