Parcourir la source

😎优化缓存服务

zuohuaijun il y a 1 an
Parent
commit
bb44c4fc1a
1 fichiers modifiés avec 138 ajouts et 37 suppressions
  1. 138 37
      Admin.NET/Admin.NET.Core/Service/Cache/SysCacheService.cs

+ 138 - 37
Admin.NET/Admin.NET.Core/Service/Cache/SysCacheService.cs

@@ -4,7 +4,8 @@
 //
 // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
 
-using NewLife.Caching.Models;
+using NewLife.Reflection;
+using Newtonsoft.Json;
 
 namespace Admin.NET.Core.Service;
 
@@ -31,6 +32,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
     /// <param name="msExpire">锁过期时间,超过该时间没有主动是放则自动是放,必须整数秒,单位毫秒</param>
     /// <param name="throwOnFailure">失败时是否抛出异常,如不抛出异常,可通过判断返回null得知申请锁失败</param>
     /// <returns></returns>
+    [DisplayName("申请分布式锁")]
     public IDisposable? BeginCacheLock(string key, int msTimeout = 500, int msExpire = 10000, bool throwOnFailure = true)
     {
         try
@@ -82,6 +84,97 @@ public class SysCacheService : IDynamicApiController, ISingleton
         return _cacheProvider.Cache.Set($"{_cacheOptions.Prefix}{key}", value, expire);
     }
 
+    public async Task<TR> AdGetAsync<TR>(String cacheName, Func<Task<TR>> del, TimeSpan? expiry = default(TimeSpan?)) where TR : class
+    {
+        return await AdGetAsync<TR>(cacheName, del, new object[] { }, expiry);
+    }
+
+    public async Task<TR> AdGetAsync<TR, T1>(String cacheName, Func<T1, Task<TR>> del, T1 t1, TimeSpan? expiry = default(TimeSpan?)) where TR : class
+    {
+        return await AdGetAsync<TR>(cacheName, del, new object[] { t1 }, expiry);
+    }
+
+    public async Task<TR> AdGetAsync<TR, T1, T2>(String cacheName, Func<T1, T2, Task<TR>> del, T1 t1, T2 t2, TimeSpan? expiry = default(TimeSpan?)) where TR : class
+    {
+        return await AdGetAsync<TR>(cacheName, del, new object[] { t1, t2 }, expiry);
+    }
+
+    public async Task<TR> AdGetAsync<TR, T1, T2, T3>(String cacheName, Func<T1, T2, T3, Task<TR>> del, T1 t1, T2 t2, T3 t3, TimeSpan? expiry = default(TimeSpan?)) where TR : class
+    {
+        return await AdGetAsync<TR>(cacheName, del, new object[] { t1, t2, t3 }, expiry);
+    }
+
+    private async Task<T> AdGetAsync<T>(string cacheName, Delegate del, Object[] obs, TimeSpan? expiry) where T : class
+    {
+        var key = Key(cacheName, obs);
+        // 使用分布式锁
+        using (_cacheProvider.Cache.AcquireLock($@"lock:AdGetAsync:{cacheName}", 1000))
+        {
+            var value = Get<T>(key);
+            value ??= await ((dynamic)del).DynamicInvokeAsync(obs);
+            Set(key, value);
+            return value;
+        }
+    }
+
+    public T Get<T>(String cacheName, object t1)
+    {
+        return Get<T>(cacheName, new object[] { t1 });
+    }
+
+    public T Get<T>(String cacheName, object t1, object t2)
+    {
+        return Get<T>(cacheName, new object[] { t1, t2 });
+    }
+
+    public T Get<T>(String cacheName, object t1, object t2, object t3)
+    {
+        return Get<T>(cacheName, new object[] { t1, t2, t3 });
+    }
+
+    private T Get<T>(String cacheName, Object[] obs)
+    {
+        var key = cacheName + ":" + obs.Aggregate(string.Empty, (current, o) => current + $"<{o}>");
+        return Get<T>(key);
+    }
+
+    private static string Key(string cacheName, object[] obs)
+    {
+        foreach (var obj in obs)
+        {
+            if (obj is TimeSpan)
+            {
+                throw new Exception("缓存参数类型不能能是:TimeSpan类型");
+            }
+        }
+        StringBuilder sb = new StringBuilder(cacheName + ":");
+        foreach (var a in obs)
+        {
+            sb.Append($@"<{KeySingle(a)}>");
+        }
+        return sb.ToString();
+    }
+
+    public static string KeySingle(object t)
+    {
+        if (t.GetType().IsClass && !t.GetType().IsPrimitive)
+        {
+            return JsonConvert.SerializeObject(t);
+        }
+        return t?.ToString();
+    }
+
+    /// <summary>
+    /// 获取缓存的剩余生存时间
+    /// </summary>
+    /// <param name="key"></param>
+    /// <returns></returns>
+    [NonAction]
+    public TimeSpan GetExpire(string key)
+    {
+        return _cacheProvider.Cache.GetExpire(key);
+    }
+
     /// <summary>
     /// 获取缓存
     /// </summary>
@@ -197,9 +290,9 @@ public class SysCacheService : IDynamicApiController, ISingleton
     /// <param name="key"></param>
     /// <returns></returns>
     [NonAction]
-    public RedisHash<string, T> GetHashMap<T>(string key)
+    public IDictionary<String, T> GetHashMap<T>(string key)
     {
-        return _cacheProvider.Cache.GetDictionary<T>(key) as RedisHash<string, T>;
+        return _cacheProvider.Cache.GetDictionary<T>(key);
     }
 
     /// <summary>
@@ -213,7 +306,11 @@ public class SysCacheService : IDynamicApiController, ISingleton
     public bool HashSet<T>(string key, Dictionary<string, T> dic)
     {
         var hash = GetHashMap<T>(key);
-        return hash.HMSet(dic);
+        foreach (var v in dic)
+        {
+            hash.Add(v);
+        }
+        return true;
     }
 
     /// <summary>
@@ -241,8 +338,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
     public List<T> HashGet<T>(string key, params string[] fields)
     {
         var hash = GetHashMap<T>(key);
-        var result = hash.HMGet(fields);
-        return result.ToList();
+        return hash.Where(t => fields.Any(c => t.Key == c)).Select(t => t.Value).ToList();
     }
 
     /// <summary>
@@ -256,8 +352,12 @@ public class SysCacheService : IDynamicApiController, ISingleton
     public T HashGetOne<T>(string key, string field)
     {
         var hash = GetHashMap<T>(key);
-        var result = hash.HMGet(new string[] { field });
-        return result[0];
+        var value = hash.GetValue(field);
+        if (value == null)
+        {
+            return default(T);
+        }
+        return (T)hash.GetValue(field);
     }
 
     /// <summary>
@@ -270,7 +370,7 @@ public class SysCacheService : IDynamicApiController, ISingleton
     public IDictionary<string, T> HashGetAll<T>(string key)
     {
         var hash = GetHashMap<T>(key);
-        return hash.GetAll();
+        return hash;
     }
 
     /// <summary>
@@ -284,35 +384,36 @@ public class SysCacheService : IDynamicApiController, ISingleton
     public int HashDel<T>(string key, params string[] fields)
     {
         var hash = GetHashMap<T>(key);
-        return hash.HDel(fields);
+        fields.ToList().ForEach(t => hash.Remove(t));
+        return fields.Length;
     }
 
-    /// <summary>
-    /// 搜索HASH
-    /// </summary>
-    /// <typeparam name="T"></typeparam>
-    /// <param name="key"></param>
-    /// <param name="searchModel"></param>
-    /// <returns></returns>
-    [NonAction]
-    public List<KeyValuePair<string, T>> HashSearch<T>(string key, SearchModel searchModel)
-    {
-        var hash = GetHashMap<T>(key);
-        return hash.Search(searchModel).ToList();
-    }
+    ///// <summary>
+    ///// 搜索HASH
+    ///// </summary>
+    ///// <typeparam name="T"></typeparam>
+    ///// <param name="key"></param>
+    ///// <param name="searchModel"></param>
+    ///// <returns></returns>
+    //[NonAction]
+    //public List<KeyValuePair<string, T>> HashSearch<T>(string key, SearchModel searchModel)
+    //{
+    //    var hash = GetHashMap<T>(key);
+    //    return hash.Search(searchModel).ToList();
+    //}
 
-    /// <summary>
-    /// 搜索HASH
-    /// </summary>
-    /// <typeparam name="T"></typeparam>
-    /// <param name="key"></param>
-    /// <param name="pattern"></param>
-    /// <param name="count"></param>
-    /// <returns></returns>
-    [NonAction]
-    public List<KeyValuePair<string, T>> HashSearch<T>(string key, string pattern, int count)
-    {
-        var hash = GetHashMap<T>(key);
-        return hash.Search(pattern, count).ToList();
-    }
+    ///// <summary>
+    ///// 搜索HASH
+    ///// </summary>
+    ///// <typeparam name="T"></typeparam>
+    ///// <param name="key"></param>
+    ///// <param name="pattern"></param>
+    ///// <param name="count"></param>
+    ///// <returns></returns>
+    //[NonAction]
+    //public List<KeyValuePair<string, T>> HashSearch<T>(string key, string pattern, int count)
+    //{
+    //    var hash = GetHashMap<T>(key);
+    //    return hash.Search(pattern, count).ToList();
+    //}
 }