Commit ebe3cc727bc43e3e78663fbebfd13903f7ec7962

Authored by YunaiV
1 parent aef833fb

【功能新增】全局:多租户缓存,增加忽略的 Spring Cache 配置

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 { @@ -39,4 +39,11 @@ public class TenantProperties {
39 */ 39 */
40 private Set<String> ignoreTables = Collections.emptySet(); 40 private Set<String> ignoreTables = Collections.emptySet();
41 41
  42 + /**
  43 + * 需要忽略多租户的 Spring Cache 缓存
  44 + *
  45 + * 即默认所有缓存都开启多租户的功能,所以记得添加对应的 tenant_id 字段哟
  46 + */
  47 + private Set<String> ignoreCaches = Collections.emptySet();
  48 +
42 } 49 }
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantAutoConfiguration.java
@@ -120,13 +120,14 @@ public class YudaoTenantAutoConfiguration { @@ -120,13 +120,14 @@ public class YudaoTenantAutoConfiguration {
120 @Primary // 引入租户时,tenantRedisCacheManager 为主 Bean 120 @Primary // 引入租户时,tenantRedisCacheManager 为主 Bean
121 public RedisCacheManager tenantRedisCacheManager(RedisTemplate<String, Object> redisTemplate, 121 public RedisCacheManager tenantRedisCacheManager(RedisTemplate<String, Object> redisTemplate,
122 RedisCacheConfiguration redisCacheConfiguration, 122 RedisCacheConfiguration redisCacheConfiguration,
123 - YudaoCacheProperties yudaoCacheProperties) { 123 + YudaoCacheProperties yudaoCacheProperties,
  124 + TenantProperties tenantProperties) {
124 // 创建 RedisCacheWriter 对象 125 // 创建 RedisCacheWriter 对象
125 RedisConnectionFactory connectionFactory = Objects.requireNonNull(redisTemplate.getConnectionFactory()); 126 RedisConnectionFactory connectionFactory = Objects.requireNonNull(redisTemplate.getConnectionFactory());
126 RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory, 127 RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory,
127 BatchStrategies.scan(yudaoCacheProperties.getRedisScanBatchSize())); 128 BatchStrategies.scan(yudaoCacheProperties.getRedisScanBatchSize()));
128 // 创建 TenantRedisCacheManager 对象 129 // 创建 TenantRedisCacheManager 对象
129 - return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration); 130 + return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration, tenantProperties.getIgnoreCaches());
130 } 131 }
131 132
132 } 133 }
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/redis/TenantRedisCacheManager.java
1 package cn.iocoder.yudao.framework.tenant.core.redis; 1 package cn.iocoder.yudao.framework.tenant.core.redis;
2 2
  3 +import cn.hutool.core.collection.CollUtil;
3 import cn.iocoder.yudao.framework.redis.core.TimeoutRedisCacheManager; 4 import cn.iocoder.yudao.framework.redis.core.TimeoutRedisCacheManager;
4 import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; 5 import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
5 import lombok.extern.slf4j.Slf4j; 6 import lombok.extern.slf4j.Slf4j;
@@ -8,6 +9,8 @@ import org.springframework.data.redis.cache.RedisCacheConfiguration; @@ -8,6 +9,8 @@ import org.springframework.data.redis.cache.RedisCacheConfiguration;
8 import org.springframework.data.redis.cache.RedisCacheManager; 9 import org.springframework.data.redis.cache.RedisCacheManager;
9 import org.springframework.data.redis.cache.RedisCacheWriter; 10 import org.springframework.data.redis.cache.RedisCacheWriter;
10 11
  12 +import java.util.Set;
  13 +
11 /** 14 /**
12 * 多租户的 {@link RedisCacheManager} 实现类 15 * 多租户的 {@link RedisCacheManager} 实现类
13 * 16 *
@@ -18,16 +21,21 @@ import org.springframework.data.redis.cache.RedisCacheWriter; @@ -18,16 +21,21 @@ import org.springframework.data.redis.cache.RedisCacheWriter;
18 @Slf4j 21 @Slf4j
19 public class TenantRedisCacheManager extends TimeoutRedisCacheManager { 22 public class TenantRedisCacheManager extends TimeoutRedisCacheManager {
20 23
  24 + private final Set<String> ignoreCaches;
  25 +
21 public TenantRedisCacheManager(RedisCacheWriter cacheWriter, 26 public TenantRedisCacheManager(RedisCacheWriter cacheWriter,
22 - RedisCacheConfiguration defaultCacheConfiguration) { 27 + RedisCacheConfiguration defaultCacheConfiguration,
  28 + Set<String> ignoreCaches) {
23 super(cacheWriter, defaultCacheConfiguration); 29 super(cacheWriter, defaultCacheConfiguration);
  30 + this.ignoreCaches = ignoreCaches;
24 } 31 }
25 32
26 @Override 33 @Override
27 public Cache getCache(String name) { 34 public Cache getCache(String name) {
28 // 如果开启多租户,则 name 拼接租户后缀 35 // 如果开启多租户,则 name 拼接租户后缀
29 if (!TenantContextHolder.isIgnore() 36 if (!TenantContextHolder.isIgnore()
30 - && TenantContextHolder.getTenantId() != null) { 37 + && TenantContextHolder.getTenantId() != null
  38 + && !CollUtil.contains(ignoreCaches, name)) {
31 name = name + ":" + TenantContextHolder.getTenantId(); 39 name = name + ":" + TenantContextHolder.getTenantId();
32 } 40 }
33 41
yudao-server/src/main/resources/application.yaml
@@ -286,6 +286,13 @@ yudao: @@ -286,6 +286,13 @@ yudao:
286 - rep_demo_jianpiao 286 - rep_demo_jianpiao
287 - tmp_report_data_1 287 - tmp_report_data_1
288 - tmp_report_data_income 288 - tmp_report_data_income
  289 + ignore-caches:
  290 + - permission_menu_ids
  291 + - oauth_client
  292 + - notify_template
  293 + - mail_account
  294 + - mail_template
  295 + - sms_template
289 sms-code: # 短信验证码相关的配置项 296 sms-code: # 短信验证码相关的配置项
290 expire-times: 10m 297 expire-times: 10m
291 send-frequency: 1m 298 send-frequency: 1m