From 1c95e12aeadf5f473994d50b2b6d9631656809ee Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 23 Jul 2024 23:26:09 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=8A=9F=E8=83=BD=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E3=80=91=E5=85=A8=E5=B1=80=EF=BC=9A=E5=A4=9A=E7=A7=9F=E6=88=B7?= =?UTF-8?q?=E7=BC=93=E5=AD=98=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=E7=9A=84=20Spring=20Cache=20=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/tenant/config/TenantProperties.java | 7 +++++++ .../tenant/config/YudaoTenantAutoConfiguration.java | 5 +++-- .../tenant/core/redis/TenantRedisCacheManager.java | 12 ++++++++++-- .../src/main/resources/application.yaml | 7 +++++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/TenantProperties.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/TenantProperties.java index 7cb813ca9..b0c0217f0 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/TenantProperties.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/TenantProperties.java @@ -39,4 +39,11 @@ public class TenantProperties { */ private Set ignoreTables = Collections.emptySet(); + /** + * 需要忽略多租户的 Spring Cache 缓存 + * + * 即默认所有缓存都开启多租户的功能,所以记得添加对应的 tenant_id 字段哟 + */ + private Set ignoreCaches = Collections.emptySet(); + } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantAutoConfiguration.java index e2cbbeb95..cd3b3659e 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantAutoConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantAutoConfiguration.java @@ -133,12 +133,13 @@ public class YudaoTenantAutoConfiguration { @Primary // 引入租户时,tenantRedisCacheManager 为主 Bean public RedisCacheManager tenantRedisCacheManager(RedisTemplate redisTemplate, RedisCacheConfiguration redisCacheConfiguration, - YudaoCacheProperties yudaoCacheProperties) { + YudaoCacheProperties yudaoCacheProperties, + TenantProperties tenantProperties) { // 创建 RedisCacheWriter 对象 RedisConnectionFactory connectionFactory = Objects.requireNonNull(redisTemplate.getConnectionFactory()); RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory, BatchStrategies.scan(yudaoCacheProperties.getRedisScanBatchSize())); // 创建 TenantRedisCacheManager 对象 - return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration); + return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration, tenantProperties.getIgnoreCaches()); } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/redis/TenantRedisCacheManager.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/redis/TenantRedisCacheManager.java index 240b9209f..68a2a3e91 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/redis/TenantRedisCacheManager.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/redis/TenantRedisCacheManager.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.framework.tenant.core.redis; +import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.framework.redis.core.TimeoutRedisCacheManager; import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; import lombok.extern.slf4j.Slf4j; @@ -8,6 +9,8 @@ import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheWriter; +import java.util.Set; + /** * 多租户的 {@link RedisCacheManager} 实现类 * @@ -18,16 +21,21 @@ import org.springframework.data.redis.cache.RedisCacheWriter; @Slf4j public class TenantRedisCacheManager extends TimeoutRedisCacheManager { + private final Set ignoreCaches; + public TenantRedisCacheManager(RedisCacheWriter cacheWriter, - RedisCacheConfiguration defaultCacheConfiguration) { + RedisCacheConfiguration defaultCacheConfiguration, + Set ignoreCaches) { super(cacheWriter, defaultCacheConfiguration); + this.ignoreCaches = ignoreCaches; } @Override public Cache getCache(String name) { // 如果开启多租户,则 name 拼接租户后缀 if (!TenantContextHolder.isIgnore() - && TenantContextHolder.getTenantId() != null) { + && TenantContextHolder.getTenantId() != null + && !CollUtil.contains(ignoreCaches, name)) { name = name + ":" + TenantContextHolder.getTenantId(); } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/resources/application.yaml b/yudao-module-system/yudao-module-system-biz/src/main/resources/application.yaml index 04753d148..533c34162 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/resources/application.yaml +++ b/yudao-module-system/yudao-module-system-biz/src/main/resources/application.yaml @@ -190,6 +190,13 @@ yudao: - system_mail_template - system_mail_log - system_notify_template + ignore-caches: + - permission_menu_ids + - oauth_client + - notify_template + - mail_account + - mail_template + - sms_template sms-code: # 短信验证码相关的配置项 expire-times: 10m send-frequency: 1m