优化DB缓存
This commit is contained in:
commit
ee4b7c17b2
@ -43,6 +43,7 @@ namespace ZR.Admin.WebApi.Extensions
|
||||
//...增加其他数据库
|
||||
};
|
||||
SugarIocServices.AddSqlSugar(iocList);
|
||||
ICacheService cache = new SqlSugarCache();
|
||||
SugarIocServices.ConfigurationSugar(db =>
|
||||
{
|
||||
//db0数据过滤
|
||||
@ -50,12 +51,12 @@ namespace ZR.Admin.WebApi.Extensions
|
||||
|
||||
iocList.ForEach(iocConfig =>
|
||||
{
|
||||
SetSugarAop(db, iocConfig);
|
||||
SetSugarAop(db, iocConfig, cache);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private static void SetSugarAop(SqlSugarClient db, IocConfig iocConfig)
|
||||
private static void SetSugarAop(SqlSugarClient db, IocConfig iocConfig, ICacheService cache)
|
||||
{
|
||||
var config = db.GetConnection(iocConfig.ConfigId).CurrentConnectionConfig;
|
||||
|
||||
@ -76,6 +77,15 @@ namespace ZR.Admin.WebApi.Extensions
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
logger.Error(e, $"执行SQL出错:{e.Message}");
|
||||
};
|
||||
|
||||
db.GetConnectionScope(configId).CurrentConnectionConfig.MoreSettings = new ConnMoreSettings()
|
||||
{
|
||||
IsAutoRemoveDataCache = true
|
||||
};
|
||||
db.GetConnectionScope(configId).CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
|
||||
{
|
||||
DataInfoCacheService = cache
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
66
ZR.Admin.WebApi/Extensions/SqlSugarCache.cs
Normal file
66
ZR.Admin.WebApi/Extensions/SqlSugarCache.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using ZR.Common;
|
||||
|
||||
namespace ZR.Admin.WebApi.Extensions
|
||||
{
|
||||
public class SqlSugarCache : SqlSugar.ICacheService
|
||||
{
|
||||
public void Add<V>(string key, V value)
|
||||
{
|
||||
//RedisServer.Cache.Set(key, value, 3600 + RedisHelper.RandomExpired(5, 30));
|
||||
CacheHelper.SetCache(key, value);
|
||||
}
|
||||
|
||||
public void Add<V>(string key, V value, int cacheDurationInSeconds)
|
||||
{
|
||||
//RedisServer.Cache.Set(key, value, cacheDurationInSeconds);
|
||||
CacheHelper.SetCaches(key, value, cacheDurationInSeconds);
|
||||
}
|
||||
|
||||
public bool ContainsKey<V>(string key)
|
||||
{
|
||||
//return RedisServer.Cache.Exists(key);
|
||||
return CacheHelper.Exists(key);
|
||||
}
|
||||
|
||||
public V Get<V>(string key)
|
||||
{
|
||||
//return RedisServer.Cache.Get<V>(key);
|
||||
return (V)CacheHelper.Get(key);
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetAllKey<V>()
|
||||
{
|
||||
//return RedisServer.Cache.Keys("*");
|
||||
return CacheHelper.GetCacheKeys();
|
||||
}
|
||||
|
||||
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
|
||||
{
|
||||
if (ContainsKey<V>(cacheKey))
|
||||
{
|
||||
var result = Get<V>(cacheKey);
|
||||
if (result == null)
|
||||
{
|
||||
return create();
|
||||
}
|
||||
else
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var restul = create();
|
||||
|
||||
Add(cacheKey, restul, cacheDurationInSeconds);
|
||||
return restul;
|
||||
}
|
||||
}
|
||||
|
||||
public void Remove<V>(string key)
|
||||
{
|
||||
//RedisServer.Cache.Del(key);
|
||||
CacheHelper.Remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -110,6 +110,18 @@ namespace ZR.Common
|
||||
Cache.Remove(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证缓存项是否存在
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public static bool Exists(string key)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
return Cache.TryGetValue(key, out _);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有缓存键
|
||||
@ -118,13 +130,18 @@ namespace ZR.Common
|
||||
public static List<string> GetCacheKeys()
|
||||
{
|
||||
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
|
||||
var entries = Cache.GetType().GetField("_entries", flags).GetValue(Cache);
|
||||
//var entries = Cache.GetType().GetField("_entries", flags).GetValue(Cache);
|
||||
|
||||
//.net7需要这样写
|
||||
var coherentState = Cache.GetType().GetField("_coherentState", flags).GetValue(Cache);
|
||||
var entries = coherentState.GetType().GetField("_entries", flags).GetValue(coherentState);
|
||||
|
||||
var keys = new List<string>();
|
||||
if (entries is not IDictionary cacheItems) return keys;
|
||||
foreach (DictionaryEntry cacheItem in cacheItems)
|
||||
{
|
||||
keys.Add(cacheItem.Key.ToString());
|
||||
Console.WriteLine(cacheItem.Key);
|
||||
//Console.WriteLine("缓存key=" +cacheItem.Key);
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
@ -40,25 +40,25 @@ namespace ZR.Service.System
|
||||
public List<SysDictData> SelectDictDataByType(string dictType)
|
||||
{
|
||||
string CK = $"SelectDictDataByType_{dictType}";
|
||||
if (CacheHelper.GetCache(CK) is not List<SysDictData> list)
|
||||
{
|
||||
list = Queryable().Where(f => f.Status == "0" && f.DictType == dictType)
|
||||
|
||||
var list = Queryable()
|
||||
.WithCache(CK, 60 * 10)
|
||||
.Where(f => f.Status == "0" && f.DictType == dictType)
|
||||
.OrderBy(it => it.DictSort)
|
||||
.ToList();
|
||||
CacheHelper.SetCache(CK, list, 30);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
public List<SysDictData> SelectDictDataByTypes(string[] dictTypes)
|
||||
{
|
||||
string CK = $"SelectDictDataByTypes_{dictTypes}";
|
||||
if (CacheHelper.GetCache(CK) is not List<SysDictData> list)
|
||||
{
|
||||
list = Queryable().Where(f => f.Status == "0" && dictTypes.Contains(f.DictType))
|
||||
|
||||
var list = Queryable()
|
||||
.WithCache(CK, 60 * 30)
|
||||
.Where(f => f.Status == "0" && dictTypes.Contains(f.DictType))
|
||||
.OrderBy(it => it.DictSort)
|
||||
.ToList();
|
||||
//CacheHelper.SetCache(CK, list, 30);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
/// <summary>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user