From 90ae95536fc51110717c13b435ed2da09f4392a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Fri, 3 Mar 2023 21:18:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86db=E7=BC=93=E5=AD=98=E6=94=B9=E6=88=90?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E8=87=AA=E5=B8=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZR.Admin.WebApi/Extensions/SqlSugarCache.cs | 15 ++++++++++----- ZR.Common/Cache/CacheHelper.cs | 12 ++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/ZR.Admin.WebApi/Extensions/SqlSugarCache.cs b/ZR.Admin.WebApi/Extensions/SqlSugarCache.cs index 8b9595b..8354e63 100644 --- a/ZR.Admin.WebApi/Extensions/SqlSugarCache.cs +++ b/ZR.Admin.WebApi/Extensions/SqlSugarCache.cs @@ -1,4 +1,5 @@ -using ZR.Common.Cache; +using ZR.Common; +using ZR.Common.Cache; namespace ZR.Admin.WebApi.Extensions { @@ -6,22 +7,26 @@ namespace ZR.Admin.WebApi.Extensions { public void Add(string key, V value) { - RedisServer.Cache.Set(key, value, 3600 + RedisHelper.RandomExpired(5, 30)); + //RedisServer.Cache.Set(key, value, 3600 + RedisHelper.RandomExpired(5, 30)); + CacheHelper.SetCache(key, value); } public void Add(string key, V value, int cacheDurationInSeconds) { - RedisServer.Cache.Set(key, value, cacheDurationInSeconds); + //RedisServer.Cache.Set(key, value, cacheDurationInSeconds); + CacheHelper.SetCaches(key, value, cacheDurationInSeconds); } public bool ContainsKey(string key) { - return RedisServer.Cache.Exists(key); + //return RedisServer.Cache.Exists(key); + return CacheHelper.Exists(key); } public V Get(string key) { - return RedisServer.Cache.Get(key); + //return RedisServer.Cache.Get(key); + return (V)CacheHelper.Get(key); } public IEnumerable GetAllKey() diff --git a/ZR.Common/Cache/CacheHelper.cs b/ZR.Common/Cache/CacheHelper.cs index 0fc6625..3d82255 100644 --- a/ZR.Common/Cache/CacheHelper.cs +++ b/ZR.Common/Cache/CacheHelper.cs @@ -106,6 +106,18 @@ namespace ZR.Common { Cache.Remove(key); } + + /// + /// 验证缓存项是否存在 + /// + /// 缓存Key + /// + public static bool Exists(string key) + { + if (key == null) + throw new ArgumentNullException(nameof(key)); + return Cache.TryGetValue(key, out _); + } } }