diff --git a/ZR.Admin.WebApi/Extensions/DbExtension.cs b/ZR.Admin.WebApi/Extensions/DbExtension.cs index 2540410..4025055 100644 --- a/ZR.Admin.WebApi/Extensions/DbExtension.cs +++ b/ZR.Admin.WebApi/Extensions/DbExtension.cs @@ -43,14 +43,22 @@ namespace ZR.Admin.WebApi.Extensions //...增加其他数据库 }; SugarIocServices.AddSqlSugar(iocList); + ICacheService cache = new SqlSugarCache(); SugarIocServices.ConfigurationSugar(db => { //db0数据过滤 FilterData(0); - + db.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings() + { + IsAutoRemoveDataCache = true + }; + db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices() + { + DataInfoCacheService = cache + }; iocList.ForEach(iocConfig => { - SetSugarAop(db, iocConfig); + SetSugarAop(db, iocConfig); }); }); } @@ -58,7 +66,7 @@ namespace ZR.Admin.WebApi.Extensions private static void SetSugarAop(SqlSugarClient db, IocConfig iocConfig) { var config = db.GetConnection(iocConfig.ConfigId).CurrentConnectionConfig; - + string configId = config.ConfigId; db.GetConnectionScope(configId).Aop.OnLogExecuting = (sql, pars) => { diff --git a/ZR.Admin.WebApi/Extensions/SqlSugarCache.cs b/ZR.Admin.WebApi/Extensions/SqlSugarCache.cs new file mode 100644 index 0000000..8b9595b --- /dev/null +++ b/ZR.Admin.WebApi/Extensions/SqlSugarCache.cs @@ -0,0 +1,52 @@ +using ZR.Common.Cache; + +namespace ZR.Admin.WebApi.Extensions +{ + public class SqlSugarCache : SqlSugar.ICacheService + { + public void Add(string key, V value) + { + RedisServer.Cache.Set(key, value, 3600 + RedisHelper.RandomExpired(5, 30)); + } + + public void Add(string key, V value, int cacheDurationInSeconds) + { + RedisServer.Cache.Set(key, value, cacheDurationInSeconds); + } + + public bool ContainsKey(string key) + { + return RedisServer.Cache.Exists(key); + } + + public V Get(string key) + { + return RedisServer.Cache.Get(key); + } + + public IEnumerable GetAllKey() + { + return RedisServer.Cache.Keys("*"); + } + + public V GetOrCreate(string cacheKey, Func create, int cacheDurationInSeconds = int.MaxValue) + { + if (RedisServer.Cache.Exists(cacheKey)) + { + return Get(cacheKey); + } + else + { + var restul = create(); + + Add(cacheKey, restul); + return restul; + } + } + + public void Remove(string key) + { + RedisServer.Cache.Del(key); + } + } +} \ No newline at end of file