修复更改

This commit is contained in:
lqc 2025-03-10 02:55:15 +08:00
parent 97e438c552
commit 807e4b1c07

View File

@ -146,7 +146,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
private static string KeySingle(object t)
{
return t.GetType().IsClass && !t.GetType().IsPrimitive ? JsonConvert.SerializeObject(t) : t.ToString();
return t.GetType().IsClass && !t.GetType().IsPrimitive ? JSON.Serialize(t) : t.ToString();
}
/// <summary>
@ -155,7 +155,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
/// <param name="key"></param>
/// <returns></returns>
[NonAction]
public TimeSpan GetExpire(string key)
public static TimeSpan GetExpire(string key)
{
return _cacheProvider.Cache.GetExpire(key);
}
@ -505,7 +505,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
/// <param name="expire">过期时间,单位秒</param>
/// <returns></returns>
[NonAction]
public T GetOrAdd<T>(string key, Func<string, T> callback, int expire = -1)
public T GetOrAdd<T>(string key, Func<string, T> callback, int expire = -1)
{
if (string.IsNullOrWhiteSpace(key)) return default;
return _cacheProvider.Cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire);
@ -518,7 +518,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
/// <param name="key"></param>
/// <returns></returns>
[NonAction]
public IDictionary<String, T> GetHashMap<T>(string key)
public static IDictionary<String, T> GetHashMap<T>(string key)
{
return _cacheProvider.Cache.GetDictionary<T>(key);
}
@ -531,7 +531,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
/// <param name="dic"></param>
/// <returns></returns>
[NonAction]
public bool HashSet<T>(string key, Dictionary<string, T> dic)
public static bool HashSet<T>(string key, Dictionary<string, T> dic)
{
var hash = GetHashMap<T>(key);
foreach (var v in dic)
@ -549,7 +549,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
/// <param name="hashKey"></param>
/// <param name="value"></param>
[NonAction]
public void HashAdd<T>(string key, string hashKey, T value)
public static void HashAdd<T>(string key, string hashKey, T value)
{
var hash = GetHashMap<T>(key);
hash.Add(hashKey, value);
@ -563,7 +563,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
/// <param name="hashKey"></param>
/// <param name="value"></param>
[NonAction]
public void HashAddOrUpdate<T>(string key, string hashKey, T value)
public static void HashAddOrUpdate<T>(string key, string hashKey, T value)
{
var hash = GetHashMap<T>(key);
if (hash.ContainsKey(hashKey))
@ -580,7 +580,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
/// <param name="fields"></param>
/// <returns></returns>
[NonAction]
public List<T> HashGet<T>(string key, params string[] fields)
public static List<T> HashGet<T>(string key, params string[] fields)
{
var hash = GetHashMap<T>(key);
return hash.Where(t => fields.Any(c => t.Key == c)).Select(t => t.Value).ToList();
@ -594,20 +594,20 @@ public class SysCacheService : IDynamicApiController, ISingleton
/// <param name="field"></param>
/// <returns></returns>
[NonAction]
public T HashGetOne<T>(string key, string field)
public static T HashGetOne<T>(string key, string field)
{
var hash = GetHashMap<T>(key);
return hash.TryGetValue(field, out T value) ? value : default;
}
// 新增方法:获取哈希表所有键
public List<string> HashGetAllKeys(string key)
public static List<string> HashGetAllKeys(string key)
{
var hash = GetHashMap<string>(key); // 假设值为任意类型
return hash.Keys.ToList();
}
// 增强的哈希设置方法(带过期时间)
public bool HashSet<T>(string key, Dictionary<string, T> items, TimeSpan? expiry = null)
public static bool HashSet<T>(string key, Dictionary<string, T> items, TimeSpan? expiry = null)
{
var hash = GetHashMap<T>(key);
foreach (var item in items)
@ -622,7 +622,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
return true;
}
// 异步批量设置哈希,目前没有,先保留扩展
public async Task<bool> HashSetAsync<T>(string key, Dictionary<string, T> items, TimeSpan? expiry = null)
public static async Task<bool> HashSetAsync<T>(string key, Dictionary<string, T> items, TimeSpan? expiry = null)
{
var hash = GetHashMap<T>(key);
foreach (var item in items)
@ -645,7 +645,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
/// <param name="key"></param>
/// <returns></returns>
[NonAction]
public IDictionary<string, T> HashGetAll<T>(string key)
public static IDictionary<string, T> HashGetAll<T>(string key)
{
var hash = GetHashMap<T>(key);
return hash;
@ -659,7 +659,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
/// <param name="fields"></param>
/// <returns></returns>
[NonAction]
public int HashDel<T>(string key, params string[] fields)
public static int HashDel<T>(string key, params string[] fields)
{
var hash = GetHashMap<T>(key);
fields.ToList().ForEach(t => hash.Remove(t));