From d83525d71c6c4edad8a17590e389fd15b39031d5 Mon Sep 17 00:00:00 2001 From: "Emil.Zhang" Date: Tue, 30 Aug 2022 16:30:37 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=BC=93=E5=AD=98=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=8F=AF=E9=85=8D=E7=BD=AE=E6=A0=87=E8=AF=86):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 针对二次开发项目可能部署在同一个服务器上导致缓存冲突的问题,给所有的缓存Key改原先写在代码里的配置为引用CacheKey配置,并给每个缓存Key增加PROJECT作为头,用以区分不同的项目缓存 --- README.md | 2 +- .../main/java/me/zhengjie/utils/CacheKey.java | 99 +++++++++++++++---- .../security/config/bean/LoginProperties.java | 3 +- .../config/bean/SecurityProperties.java | 9 ++ .../system/service/impl/DataServiceImpl.java | 5 +- .../system/service/impl/DeptServiceImpl.java | 8 +- 6 files changed, 101 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 62eb60cd..76340cff 100644 --- a/README.md +++ b/README.md @@ -105,4 +105,4 @@ 项目的发展离不开你的支持,请作者喝杯咖啡吧☕ [Donate](https://eladmin.vip/donation/) #### 反馈交流 -- QQ交流群:一群:891137268 、二群:947578238、三群:659622532 \ No newline at end of file +- QQ交流群:一群:891137268 、二群:947578238、三群:659622532 diff --git a/eladmin-common/src/main/java/me/zhengjie/utils/CacheKey.java b/eladmin-common/src/main/java/me/zhengjie/utils/CacheKey.java index 7485713f..2ade6e94 100644 --- a/eladmin-common/src/main/java/me/zhengjie/utils/CacheKey.java +++ b/eladmin-common/src/main/java/me/zhengjie/utils/CacheKey.java @@ -16,6 +16,10 @@ package me.zhengjie.utils; /** + * 实际上缓存中的记录的Key为 PROJECT + xxx_KEY + :: + ID|NAME + : + 数据 + *

+ * 即最终保存的key为 eladmin-user::id:1 + * * @author: liaojinlong * @date: 2020/6/11 15:49 * @apiNote: 关于缓存的Key集合 @@ -23,36 +27,97 @@ package me.zhengjie.utils; public interface CacheKey { /** - * 用户 + * 项目名称 */ - String USER_ID = "user::id:"; + String PROJECT = "eladmin-"; /** - * 数据 + * 用户部分 */ - String DATA_USER = "data::user:"; + String USER_KEY = "user"; /** - * 菜单 + * 目录部分 */ - String MENU_ID = "menu::id:"; - String MENU_USER = "menu::user:"; + String MENU_KEY = "menu"; /** - * 角色授权 + * 角色部分 */ - String ROLE_AUTH = "role::auth:"; + String ROLE_KEY = "role"; /** - * 角色信息 + * 部门部分 */ - String ROLE_ID = "role::id:"; - /** - * 部门 - */ - String DEPT_ID = "dept::id:"; + String DEPT_KEY = "dept"; /** * 岗位 */ - String JOB_ID = "job::id:"; + String JOB_KEY = "job"; + /** + * 数据部分 + */ + String DATA_KEY = "data"; + /** + * 鉴权部分 + */ + String AUTH_KEY = "auth"; /** * 数据字典 */ - String DICT_NAME = "dict::name:"; + String DICT_KEY = "dict"; + /** + * 支付宝 + */ + String ALI_PAY = "aliPay"; + /** + * 邮箱 + */ + String EMAIL = "email"; + /** + * 七牛云 + */ + String QI_NIU = "qiNiu"; + /** + * 通用ID部分 + */ + String ID = "id"; + /** + * 通用名称部分 + */ + String NAME = "name"; + /** + * 配置 + */ + String CONFIG = "config"; + + /** + * 依据传入的 key 拼接为 PROJECT + xx_KEY + * 例如 baiKe-user + * + * @param key xx_KEY + * @return / + */ + static String projectAndKey(String key) { + return String.format( + "%1$s%2$s", + PROJECT, + key + ); + } + + /** + * 一般用于手动操作Redis时,拼接存入的Key + *

+ * 依据传入的 key 和 target 拼接为 PROJECT + xx_KEY + target:: + *

+ * 例如 返回 baiKe-user::id:,后面自行拼接区分的标识,例如ID值 + * + * @param key xx_KEY + * @param target target + * @return PROJECT-key::target: + */ + static String keyAndTarget(String key, String target) { + return String.format( + "%1$s::%2$s:", + projectAndKey(key), + target + ); + } } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/security/config/bean/LoginProperties.java b/eladmin-system/src/main/java/me/zhengjie/modules/security/config/bean/LoginProperties.java index 164c0073..a2b1886f 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/security/config/bean/LoginProperties.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/security/config/bean/LoginProperties.java @@ -19,6 +19,7 @@ import com.wf.captcha.*; import com.wf.captcha.base.Captcha; import lombok.Data; import me.zhengjie.exception.BadConfigurationException; +import me.zhengjie.utils.CacheKey; import me.zhengjie.utils.StringUtils; import java.awt.*; import java.util.Objects; @@ -39,7 +40,7 @@ public class LoginProperties { private LoginCode loginCode; - public static final String cacheKey = "USER-LOGIN-DATA"; + public static final String cacheKey = CacheKey.PROJECT + "USER-LOGIN-DATA"; public boolean isSingleLogin() { return singleLogin; diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/security/config/bean/SecurityProperties.java b/eladmin-system/src/main/java/me/zhengjie/modules/security/config/bean/SecurityProperties.java index 16ec3cf0..e051d0af 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/security/config/bean/SecurityProperties.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/security/config/bean/SecurityProperties.java @@ -16,6 +16,7 @@ package me.zhengjie.modules.security.config.bean; import lombok.Data; +import me.zhengjie.utils.CacheKey; /** * Jwt参数配置 @@ -66,6 +67,14 @@ public class SecurityProperties { */ private Long renew; + public void setOnlineKey(String onlineKey) { + this.onlineKey = CacheKey.PROJECT + onlineKey; + } + + public void setCodeKey(String codeKey) { + this.codeKey = CacheKey.PROJECT + codeKey; + } + public String getTokenStartWith() { return tokenStartWith + " "; } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DataServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DataServiceImpl.java index 88f208e6..1f6319d9 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DataServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DataServiceImpl.java @@ -22,6 +22,7 @@ import me.zhengjie.modules.system.service.DeptService; import me.zhengjie.modules.system.service.RoleService; import me.zhengjie.modules.system.service.dto.RoleSmallDto; import me.zhengjie.modules.system.service.dto.UserDto; +import me.zhengjie.utils.CacheKey; import me.zhengjie.utils.enums.DataScopeEnum; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.Cacheable; @@ -36,7 +37,7 @@ import java.util.*; **/ @Service @RequiredArgsConstructor -@CacheConfig(cacheNames = "data") +@CacheConfig(cacheNames = CacheKey.PROJECT + CacheKey.DATA_KEY) public class DataServiceImpl implements DataService { private final RoleService roleService; @@ -48,7 +49,7 @@ public class DataServiceImpl implements DataService { * @return / */ @Override - @Cacheable(key = "'user:' + #p0.id") + @Cacheable(key = "'" + CacheKey.USER_KEY + ":' + #user.id") public List getDeptIds(UserDto user) { // 用于存储部门id Set deptIds = new HashSet<>(); diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java index a02bd699..7f0c65ad 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java @@ -47,7 +47,7 @@ import java.util.stream.Collectors; */ @Service @RequiredArgsConstructor -@CacheConfig(cacheNames = "dept") +@CacheConfig(cacheNames = CacheKey.PROJECT + CacheKey.DEPT_KEY) public class DeptServiceImpl implements DeptService { private final DeptRepository deptRepository; @@ -88,7 +88,7 @@ public class DeptServiceImpl implements DeptService { } @Override - @Cacheable(key = "'id:' + #p0") + @Cacheable(key = "'" + CacheKey.ID + ":' + #id") public DeptDto findById(Long id) { Dept dept = deptRepository.findById(id).orElseGet(Dept::new); ValidationUtil.isNull(dept.getId(),"Dept","id",id); @@ -277,7 +277,7 @@ public class DeptServiceImpl implements DeptService { public void delCaches(Long id){ List users = userRepository.findByRoleDeptId(id); // 删除数据权限 - redisUtils.delByKeys(CacheKey.DATA_USER, users.stream().map(User::getId).collect(Collectors.toSet())); - redisUtils.del(CacheKey.DEPT_ID + id); + redisUtils.delByKeys(CacheKey.keyAndTarget(CacheKey.DATA_KEY, CacheKey.USER_KEY), users.stream().map(User::getId).collect(Collectors.toSet())); + redisUtils.del(CacheKey.keyAndTarget(CacheKey.DEPT_KEY, CacheKey.ID) + id); } }