😎优化获取缓存各种类型的值
This commit is contained in:
parent
ea917ec061
commit
3346150bdd
@ -56,7 +56,7 @@
|
||||
<PackageReference Include="SSH.NET" Version="2025.0.0" />
|
||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.6.3" />
|
||||
<PackageReference Include="System.Net.Http" Version="4.3.4" />
|
||||
<PackageReference Include="TencentCloudSDK.Sms" Version="3.0.1240" />
|
||||
<PackageReference Include="TencentCloudSDK.Sms" Version="3.0.1241" />
|
||||
<PackageReference Include="UAParser" Version="3.1.47" />
|
||||
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -245,12 +245,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
|
||||
/// <param name="nullExpire">空值缓存时间(默认永久)</param>
|
||||
/// </summary>
|
||||
[NonAction]
|
||||
public async Task<List<T>> GetListAsync<T>(
|
||||
IEnumerable<long> ids,
|
||||
Func<List<long>, Task<List<T>>> loadFromDb, // 改为异步委托
|
||||
bool cacheNull = true,
|
||||
TimeSpan? nullExpire = null
|
||||
) where T : class
|
||||
public async Task<List<T>> GetListAsync<T>(IEnumerable<long> ids, Func<List<long>, Task<List<T>>> loadFromDb, bool cacheNull = true, TimeSpan? nullExpire = null) where T : class
|
||||
{
|
||||
var idList = ids.Distinct().ToList();
|
||||
if (idList.Count == 0) return new List<T>();
|
||||
@ -314,12 +309,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
|
||||
/// <param name="cacheNull">是否缓存空值(防穿透)</param>
|
||||
/// <param name="nullExpire">空值缓存时间(默认永久)</param>
|
||||
[NonAction]
|
||||
public List<T> GetList<T>(
|
||||
IEnumerable<long> ids,
|
||||
Func<List<long>, List<T>> loadFromDb,
|
||||
bool cacheNull = true,
|
||||
TimeSpan? nullExpire = null
|
||||
) where T : class
|
||||
public List<T> GetList<T>(IEnumerable<long> ids, Func<List<long>, List<T>> loadFromDb, bool cacheNull = true, TimeSpan? nullExpire = null) where T : class
|
||||
{
|
||||
var idList = ids.Distinct().ToList();
|
||||
if (idList.Count == 0) return new List<T>();
|
||||
@ -489,13 +479,70 @@ public class SysCacheService : IDynamicApiController, ISingleton
|
||||
[DisplayName("获取缓存值")]
|
||||
public object GetValue(string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key)) return null;
|
||||
|
||||
// 若Key经过URL编码则进行解码
|
||||
if (Regex.IsMatch(key, @"%[0-9a-fA-F]{2}"))
|
||||
key = HttpUtility.UrlDecode(key);
|
||||
|
||||
return _cacheProvider.Cache == Cache.Default
|
||||
? _cacheProvider.Cache.Get<object>($"{_cacheOptions.Prefix}{key}")
|
||||
: _cacheProvider.Cache.Get<string>($"{_cacheOptions.Prefix}{key}");
|
||||
var fullKey = $"{_cacheOptions.Prefix}{key}";
|
||||
|
||||
if (_cacheProvider.Cache == Cache.Default)
|
||||
return _cacheProvider.Cache.Get<object>(fullKey);
|
||||
|
||||
if (_cacheProvider.Cache is FullRedis redisCache)
|
||||
{
|
||||
if (!redisCache.ContainsKey(fullKey))
|
||||
return null;
|
||||
try
|
||||
{
|
||||
var keyType = redisCache.TYPE(fullKey)?.ToLower();
|
||||
switch (keyType)
|
||||
{
|
||||
case "string":
|
||||
return redisCache.Get<string>(fullKey);
|
||||
|
||||
case "list":
|
||||
var list = redisCache.GetList<string>(fullKey);
|
||||
return list?.ToList();
|
||||
|
||||
case "hash":
|
||||
var hash = redisCache.GetDictionary<string>(fullKey);
|
||||
return hash?.ToDictionary(k => k.Key, v => v.Value);
|
||||
|
||||
case "set":
|
||||
var set = redisCache.GetSet<string>(fullKey);
|
||||
return set?.ToArray();
|
||||
|
||||
case "zset":
|
||||
var sortedSet = redisCache.GetSortedSet<string>(fullKey);
|
||||
return sortedSet?.Range(0, -1)?.ToList();
|
||||
|
||||
case "none":
|
||||
return null;
|
||||
|
||||
default:
|
||||
// 未知类型或特殊类型
|
||||
return new Dictionary<string, object>
|
||||
{
|
||||
{ "key", key },
|
||||
{ "type", keyType ?? "unknown" },
|
||||
{ "message", "无法使用标准方式获取此类型数据" }
|
||||
};
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new Dictionary<string, object>
|
||||
{
|
||||
{ "key", key },
|
||||
{ "error", ex.Message },
|
||||
{ "type", "exception" }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return _cacheProvider.Cache.Get<object>(fullKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
"name": "admin.net.pro",
|
||||
"type": "module",
|
||||
"version": "2.4.33",
|
||||
"lastBuildTime": "2025.05.18",
|
||||
"lastBuildTime": "2025.05.19",
|
||||
"description": "Admin.NET 站在巨人肩膀上的 .NET 通用权限开发框架",
|
||||
"author": "zuohuaijun",
|
||||
"license": "MIT",
|
||||
@ -81,7 +81,7 @@
|
||||
"vue3-flag-icons": "^0.0.3",
|
||||
"vue3-tree-org": "^4.2.2",
|
||||
"vxe-pc-ui": "^4.6.11",
|
||||
"vxe-table": "^4.13.30",
|
||||
"vxe-table": "^4.13.31",
|
||||
"xe-utils": "^3.7.4",
|
||||
"xlsx-js-style": "^1.2.0"
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user