优化DB缓存
This commit is contained in:
commit
ee4b7c17b2
@ -43,6 +43,7 @@ namespace ZR.Admin.WebApi.Extensions
|
|||||||
//...增加其他数据库
|
//...增加其他数据库
|
||||||
};
|
};
|
||||||
SugarIocServices.AddSqlSugar(iocList);
|
SugarIocServices.AddSqlSugar(iocList);
|
||||||
|
ICacheService cache = new SqlSugarCache();
|
||||||
SugarIocServices.ConfigurationSugar(db =>
|
SugarIocServices.ConfigurationSugar(db =>
|
||||||
{
|
{
|
||||||
//db0数据过滤
|
//db0数据过滤
|
||||||
@ -50,12 +51,12 @@ namespace ZR.Admin.WebApi.Extensions
|
|||||||
|
|
||||||
iocList.ForEach(iocConfig =>
|
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;
|
var config = db.GetConnection(iocConfig.ConfigId).CurrentConnectionConfig;
|
||||||
|
|
||||||
@ -76,6 +77,15 @@ namespace ZR.Admin.WebApi.Extensions
|
|||||||
Console.ForegroundColor = ConsoleColor.Red;
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
logger.Error(e, $"执行SQL出错:{e.Message}");
|
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>
|
/// <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);
|
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>
|
/// <summary>
|
||||||
/// 获取所有缓存键
|
/// 获取所有缓存键
|
||||||
@ -118,13 +130,18 @@ namespace ZR.Common
|
|||||||
public static List<string> GetCacheKeys()
|
public static List<string> GetCacheKeys()
|
||||||
{
|
{
|
||||||
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
|
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>();
|
var keys = new List<string>();
|
||||||
if (entries is not IDictionary cacheItems) return keys;
|
if (entries is not IDictionary cacheItems) return keys;
|
||||||
foreach (DictionaryEntry cacheItem in cacheItems)
|
foreach (DictionaryEntry cacheItem in cacheItems)
|
||||||
{
|
{
|
||||||
keys.Add(cacheItem.Key.ToString());
|
keys.Add(cacheItem.Key.ToString());
|
||||||
Console.WriteLine(cacheItem.Key);
|
//Console.WriteLine("缓存key=" +cacheItem.Key);
|
||||||
}
|
}
|
||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,25 +40,25 @@ namespace ZR.Service.System
|
|||||||
public List<SysDictData> SelectDictDataByType(string dictType)
|
public List<SysDictData> SelectDictDataByType(string dictType)
|
||||||
{
|
{
|
||||||
string CK = $"SelectDictDataByType_{dictType}";
|
string CK = $"SelectDictDataByType_{dictType}";
|
||||||
if (CacheHelper.GetCache(CK) is not List<SysDictData> list)
|
|
||||||
{
|
var list = Queryable()
|
||||||
list = Queryable().Where(f => f.Status == "0" && f.DictType == dictType)
|
.WithCache(CK, 60 * 10)
|
||||||
.OrderBy(it => it.DictSort)
|
.Where(f => f.Status == "0" && f.DictType == dictType)
|
||||||
.ToList();
|
.OrderBy(it => it.DictSort)
|
||||||
CacheHelper.SetCache(CK, list, 30);
|
.ToList();
|
||||||
}
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
public List<SysDictData> SelectDictDataByTypes(string[] dictTypes)
|
public List<SysDictData> SelectDictDataByTypes(string[] dictTypes)
|
||||||
{
|
{
|
||||||
string CK = $"SelectDictDataByTypes_{dictTypes}";
|
string CK = $"SelectDictDataByTypes_{dictTypes}";
|
||||||
if (CacheHelper.GetCache(CK) is not List<SysDictData> list)
|
|
||||||
{
|
var list = Queryable()
|
||||||
list = Queryable().Where(f => f.Status == "0" && dictTypes.Contains(f.DictType))
|
.WithCache(CK, 60 * 30)
|
||||||
.OrderBy(it => it.DictSort)
|
.Where(f => f.Status == "0" && dictTypes.Contains(f.DictType))
|
||||||
.ToList();
|
.OrderBy(it => it.DictSort)
|
||||||
//CacheHelper.SetCache(CK, list, 30);
|
.ToList();
|
||||||
}
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user