From 28d8571d085f3574ef61bbffa7883727137bc1b3 Mon Sep 17 00:00:00 2001 From: Parker Date: Wed, 12 May 2021 00:24:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E4=BC=AA=E7=A9=BF?= =?UTF-8?q?=E9=80=8F=E8=BF=87=E6=BB=A4=E5=99=A8=EF=BC=8C=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E7=9B=B4=E6=8E=A5=E9=94=81=E6=AD=BB=EF=BC=8C?= =?UTF-8?q?=E7=BB=99=E4=BA=883=E6=AC=A1=20=E7=A9=BF=E9=80=8Fnil=E6=9C=BA?= =?UTF-8?q?=E4=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/opsli/core/cache/local/CacheUtil.java | 28 +++++++++++++------ .../java/org/opsli/core/utils/UserUtil.java | 21 +++++++------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/cache/local/CacheUtil.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/cache/local/CacheUtil.java index 59e7c0b..6932924 100644 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/cache/local/CacheUtil.java +++ b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/cache/local/CacheUtil.java @@ -77,6 +77,8 @@ public class CacheUtil { public static final String JSON_KEY = "data"; /** 空状态 key 前缀 */ private static final String NIL_FLAG_PREFIX = "nil"; + /** 空状态 生效阈值 */ + private final static long NIL_FLAG_THRESHOLD = 3; /** 热点数据前缀 */ private static String PREFIX_NAME; @@ -642,12 +644,15 @@ public class CacheUtil { // 判断 工具类是否初始化完成 ThrowExceptionUtil.isThrowException(!IS_INIT, CoreMsg.OTHER_EXCEPTION_UTILS_INIT); + // 处理缓存 key + String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key); try { - // 处理缓存 key - String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key); // 存入Redis - return redisPlugin.put(cacheKey, 1, TTL_NIL_DATA_TIME); + Long increment = redisPlugin.increment(cacheKey); + // 设置失效时间 + redisPlugin.expire(cacheKey, TTL_NIL_DATA_TIME); + return increment != null; }catch (Exception e){ log.error(e.getMessage(),e); } @@ -666,9 +671,9 @@ public class CacheUtil { ThrowExceptionUtil.isThrowException(!IS_INIT, CoreMsg.OTHER_EXCEPTION_UTILS_INIT); + // 处理缓存 key + String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key); try { - // 处理缓存 key - String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key); // 删除Redis return redisPlugin.del(cacheKey); }catch (Exception e){ @@ -689,12 +694,17 @@ public class CacheUtil { // 判断 工具类是否初始化完成 ThrowExceptionUtil.isThrowException(!IS_INIT, CoreMsg.OTHER_EXCEPTION_UTILS_INIT); + // 处理缓存 key + String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key); try { - // 处理缓存 key - String cacheKey = CacheUtil.handleKey(NIL_FLAG_PREFIX + ":" + key); - // 判断Redis 是否 包含当前Nil值 - return redisPlugin.get(cacheKey) != null; + Object nilObj = redisPlugin.get(cacheKey); + if(nilObj == null){ + return false; + } + + Long nilNum = Convert.toLong(nilObj, 0L); + return NIL_FLAG_THRESHOLD < nilNum; }catch (Exception e){ log.error(e.getMessage(),e); } diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/UserUtil.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/UserUtil.java index d29d62c..698b0d8 100644 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/UserUtil.java +++ b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/UserUtil.java @@ -16,6 +16,7 @@ package org.opsli.core.utils; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.collection.ListUtil; import cn.hutool.core.convert.Convert; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -79,7 +80,7 @@ public class UserUtil { // 判断 工具类是否初始化完成 ThrowExceptionUtil.isThrowException(!IS_INIT, CoreMsg.OTHER_EXCEPTION_UTILS_INIT); - + String token = TokenThreadLocal.get(); // 如果还是没获取到token 则抛出异常 @@ -255,7 +256,7 @@ public class UserUtil { // 防止缓存穿透判断 boolean hasNilFlag = CacheUtil.hasNilFlag(cacheKey); if(hasNilFlag){ - return null; + return ListUtil.empty(); } try { @@ -263,7 +264,7 @@ public class UserUtil { if(!DistributedLockUtil.lock(cacheKey)){ // 无法申领分布式锁 log.error(CoreMsg.REDIS_EXCEPTION_LOCK.getMessage()); - return null; + return ListUtil.empty(); } // 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求 @@ -290,7 +291,7 @@ public class UserUtil { if(CollUtil.isEmpty(roles)){ // 设置空变量 用于防止穿透判断 CacheUtil.putNilFlag(cacheKey); - return null; + return ListUtil.empty(); } return roles; @@ -323,7 +324,7 @@ public class UserUtil { // 防止缓存穿透判断 boolean hasNilFlag = CacheUtil.hasNilFlag(cacheKey); if(hasNilFlag){ - return null; + return ListUtil.empty(); } try { @@ -331,7 +332,7 @@ public class UserUtil { if(!DistributedLockUtil.lock(cacheKey)){ // 无法申领分布式锁 log.error(CoreMsg.REDIS_EXCEPTION_LOCK.getMessage()); - return null; + return ListUtil.empty(); } // 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求 @@ -358,7 +359,7 @@ public class UserUtil { if(CollUtil.isEmpty(permissions)){ // 设置空变量 用于防止穿透判断 CacheUtil.putNilFlag(cacheKey); - return null; + return ListUtil.empty(); } return permissions; @@ -390,7 +391,7 @@ public class UserUtil { // 防止缓存穿透判断 boolean hasNilFlag = CacheUtil.hasNilFlag(cacheKey); if(hasNilFlag){ - return null; + return ListUtil.empty(); } @@ -399,7 +400,7 @@ public class UserUtil { if(!DistributedLockUtil.lock(cacheKey)){ // 无法申领分布式锁 log.error(CoreMsg.REDIS_EXCEPTION_LOCK.getMessage()); - return null; + return ListUtil.empty(); } // 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求 @@ -426,7 +427,7 @@ public class UserUtil { if(CollUtil.isEmpty(menus)){ // 设置空变量 用于防止穿透判断 CacheUtil.putNilFlag(cacheKey); - return null; + return ListUtil.empty(); } return menus;