【功能新增】全局:多租户缓存,增加忽略的 Spring Cache 配置
parent
74f66735c6
commit
1c95e12aea
|
@ -39,4 +39,11 @@ public class TenantProperties {
|
||||||
*/
|
*/
|
||||||
private Set<String> ignoreTables = Collections.emptySet();
|
private Set<String> ignoreTables = Collections.emptySet();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 需要忽略多租户的 Spring Cache 缓存
|
||||||
|
*
|
||||||
|
* 即默认所有缓存都开启多租户的功能,所以记得添加对应的 tenant_id 字段哟
|
||||||
|
*/
|
||||||
|
private Set<String> ignoreCaches = Collections.emptySet();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,12 +133,13 @@ public class YudaoTenantAutoConfiguration {
|
||||||
@Primary // 引入租户时,tenantRedisCacheManager 为主 Bean
|
@Primary // 引入租户时,tenantRedisCacheManager 为主 Bean
|
||||||
public RedisCacheManager tenantRedisCacheManager(RedisTemplate<String, Object> redisTemplate,
|
public RedisCacheManager tenantRedisCacheManager(RedisTemplate<String, Object> redisTemplate,
|
||||||
RedisCacheConfiguration redisCacheConfiguration,
|
RedisCacheConfiguration redisCacheConfiguration,
|
||||||
YudaoCacheProperties yudaoCacheProperties) {
|
YudaoCacheProperties yudaoCacheProperties,
|
||||||
|
TenantProperties tenantProperties) {
|
||||||
// 创建 RedisCacheWriter 对象
|
// 创建 RedisCacheWriter 对象
|
||||||
RedisConnectionFactory connectionFactory = Objects.requireNonNull(redisTemplate.getConnectionFactory());
|
RedisConnectionFactory connectionFactory = Objects.requireNonNull(redisTemplate.getConnectionFactory());
|
||||||
RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory,
|
RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory,
|
||||||
BatchStrategies.scan(yudaoCacheProperties.getRedisScanBatchSize()));
|
BatchStrategies.scan(yudaoCacheProperties.getRedisScanBatchSize()));
|
||||||
// 创建 TenantRedisCacheManager 对象
|
// 创建 TenantRedisCacheManager 对象
|
||||||
return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration);
|
return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration, tenantProperties.getIgnoreCaches());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package cn.iocoder.yudao.framework.tenant.core.redis;
|
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.redis.core.TimeoutRedisCacheManager;
|
||||||
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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.RedisCacheManager;
|
||||||
import org.springframework.data.redis.cache.RedisCacheWriter;
|
import org.springframework.data.redis.cache.RedisCacheWriter;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 多租户的 {@link RedisCacheManager} 实现类
|
* 多租户的 {@link RedisCacheManager} 实现类
|
||||||
*
|
*
|
||||||
|
@ -18,16 +21,21 @@ import org.springframework.data.redis.cache.RedisCacheWriter;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class TenantRedisCacheManager extends TimeoutRedisCacheManager {
|
public class TenantRedisCacheManager extends TimeoutRedisCacheManager {
|
||||||
|
|
||||||
|
private final Set<String> ignoreCaches;
|
||||||
|
|
||||||
public TenantRedisCacheManager(RedisCacheWriter cacheWriter,
|
public TenantRedisCacheManager(RedisCacheWriter cacheWriter,
|
||||||
RedisCacheConfiguration defaultCacheConfiguration) {
|
RedisCacheConfiguration defaultCacheConfiguration,
|
||||||
|
Set<String> ignoreCaches) {
|
||||||
super(cacheWriter, defaultCacheConfiguration);
|
super(cacheWriter, defaultCacheConfiguration);
|
||||||
|
this.ignoreCaches = ignoreCaches;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Cache getCache(String name) {
|
public Cache getCache(String name) {
|
||||||
// 如果开启多租户,则 name 拼接租户后缀
|
// 如果开启多租户,则 name 拼接租户后缀
|
||||||
if (!TenantContextHolder.isIgnore()
|
if (!TenantContextHolder.isIgnore()
|
||||||
&& TenantContextHolder.getTenantId() != null) {
|
&& TenantContextHolder.getTenantId() != null
|
||||||
|
&& !CollUtil.contains(ignoreCaches, name)) {
|
||||||
name = name + ":" + TenantContextHolder.getTenantId();
|
name = name + ":" + TenantContextHolder.getTenantId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -190,6 +190,13 @@ yudao:
|
||||||
- system_mail_template
|
- system_mail_template
|
||||||
- system_mail_log
|
- system_mail_log
|
||||||
- system_notify_template
|
- system_notify_template
|
||||||
|
ignore-caches:
|
||||||
|
- permission_menu_ids
|
||||||
|
- oauth_client
|
||||||
|
- notify_template
|
||||||
|
- mail_account
|
||||||
|
- mail_template
|
||||||
|
- sms_template
|
||||||
sms-code: # 短信验证码相关的配置项
|
sms-code: # 短信验证码相关的配置项
|
||||||
expire-times: 10m
|
expire-times: 10m
|
||||||
send-frequency: 1m
|
send-frequency: 1m
|
||||||
|
|
Loading…
Reference in New Issue