From 50b81ff077f7638b6dc236bf31ba1ed3424c0103 Mon Sep 17 00:00:00 2001 From: zuohuaijun Date: Sat, 29 Jun 2024 10:08:37 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=8E=E8=B0=83=E6=95=B4newlife.redis?= =?UTF-8?q?=E7=94=A8=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/Cache/SysCacheService.cs | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/Admin.NET/Admin.NET.Core/Service/Cache/SysCacheService.cs b/Admin.NET/Admin.NET.Core/Service/Cache/SysCacheService.cs index ea6dd916..85e16895 100644 --- a/Admin.NET/Admin.NET.Core/Service/Cache/SysCacheService.cs +++ b/Admin.NET/Admin.NET.Core/Service/Cache/SysCacheService.cs @@ -14,12 +14,12 @@ namespace Admin.NET.Core.Service; [ApiDescriptionSettings(Order = 400)] public class SysCacheService : IDynamicApiController, ISingleton { - private readonly ICache _cache; + private static ICacheProvider _cacheProvider = App.GetService(); private readonly CacheOptions _cacheOptions; - public SysCacheService(ICache cache, IOptions cacheOptions) + public SysCacheService(ICacheProvider cacheProvider, IOptions cacheOptions) { - _cache = cache; + _cacheProvider = cacheProvider; _cacheOptions = cacheOptions.Value; } @@ -30,9 +30,9 @@ public class SysCacheService : IDynamicApiController, ISingleton [DisplayName("获取缓存键名集合")] public List GetKeyList() { - return _cache == Cache.Default - ? _cache.Keys.Where(u => u.StartsWith(_cacheOptions.Prefix)).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList() - : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList(); + return _cacheProvider.Cache == Cache.Default + ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith(_cacheOptions.Prefix)).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList() + : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList(); } /// @@ -45,7 +45,7 @@ public class SysCacheService : IDynamicApiController, ISingleton public bool Set(string key, object value) { if (string.IsNullOrWhiteSpace(key)) return false; - return _cache.Set($"{_cacheOptions.Prefix}{key}", value); + return _cacheProvider.Cache.Set($"{_cacheOptions.Prefix}{key}", value); } /// @@ -59,7 +59,7 @@ public class SysCacheService : IDynamicApiController, ISingleton public bool Set(string key, object value, TimeSpan expire) { if (string.IsNullOrWhiteSpace(key)) return false; - return _cache.Set($"{_cacheOptions.Prefix}{key}", value, expire); + return _cacheProvider.Cache.Set($"{_cacheOptions.Prefix}{key}", value, expire); } /// @@ -71,7 +71,7 @@ public class SysCacheService : IDynamicApiController, ISingleton [NonAction] public T Get(string key) { - return _cache.Get($"{_cacheOptions.Prefix}{key}"); + return _cacheProvider.Cache.Get($"{_cacheOptions.Prefix}{key}"); } /// @@ -83,7 +83,7 @@ public class SysCacheService : IDynamicApiController, ISingleton [DisplayName("删除缓存")] public int Remove(string key) { - return _cache.Remove($"{_cacheOptions.Prefix}{key}"); + return _cacheProvider.Cache.Remove($"{_cacheOptions.Prefix}{key}"); } /// @@ -94,7 +94,7 @@ public class SysCacheService : IDynamicApiController, ISingleton [NonAction] public bool ExistKey(string key) { - return _cache.ContainsKey($"{_cacheOptions.Prefix}{key}"); + return _cacheProvider.Cache.ContainsKey($"{_cacheOptions.Prefix}{key}"); } /// @@ -106,10 +106,10 @@ public class SysCacheService : IDynamicApiController, ISingleton [DisplayName("根据键名前缀删除缓存")] public int RemoveByPrefixKey(string prefixKey) { - var delKeys = _cache == Cache.Default - ? _cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).ToArray() - : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).ToArray(); - return _cache.Remove(delKeys); + var delKeys = _cacheProvider.Cache == Cache.Default + ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).ToArray() + : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).ToArray(); + return _cacheProvider.Cache.Remove(delKeys); } /// @@ -120,9 +120,9 @@ public class SysCacheService : IDynamicApiController, ISingleton [DisplayName("根据键名前缀获取键名集合")] public List GetKeysByPrefixKey(string prefixKey) { - return _cache == Cache.Default - ? _cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).Select(u => u[_cacheOptions.Prefix.Length..]).ToList() - : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).ToList(); + return _cacheProvider.Cache == Cache.Default + ? _cacheProvider.Cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).Select(u => u[_cacheOptions.Prefix.Length..]).ToList() + : ((FullRedis)_cacheProvider.Cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).ToList(); } /// @@ -133,9 +133,9 @@ public class SysCacheService : IDynamicApiController, ISingleton [DisplayName("获取缓存值")] public object GetValue(string key) { - return _cache == Cache.Default - ? _cache.Get($"{_cacheOptions.Prefix}{key}") - : _cache.Get($"{_cacheOptions.Prefix}{key}"); + return _cacheProvider.Cache == Cache.Default + ? _cacheProvider.Cache.Get($"{_cacheOptions.Prefix}{key}") + : _cacheProvider.Cache.Get($"{_cacheOptions.Prefix}{key}"); } /// @@ -150,7 +150,7 @@ public class SysCacheService : IDynamicApiController, ISingleton public T GetOrAdd(string key, Func callback, int expire = -1) { if (string.IsNullOrWhiteSpace(key)) return default; - return _cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire); + return _cacheProvider.Cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire); } /// @@ -162,7 +162,7 @@ public class SysCacheService : IDynamicApiController, ISingleton [NonAction] public RedisHash GetHashMap(string key) { - return _cache.GetDictionary(key) as RedisHash; + return _cacheProvider.Cache.GetDictionary(key) as RedisHash; } ///