mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-26 17:21:53 +08:00
♻️优化“通过注解增加多租户缓存”的代码
This commit is contained in:
parent
597a923828
commit
1be09d092b
@ -58,7 +58,7 @@
|
|||||||
<jsch.version>0.1.55</jsch.version>
|
<jsch.version>0.1.55</jsch.version>
|
||||||
<tika-core.version>2.4.1</tika-core.version>
|
<tika-core.version>2.4.1</tika-core.version>
|
||||||
<aj-captcha.version>1.3.0</aj-captcha.version>
|
<aj-captcha.version>1.3.0</aj-captcha.version>
|
||||||
<netty-all.version>4.1.75.Final</netty-all.version>
|
<netty-all.version>4.1.82.Final</netty-all.version>
|
||||||
<!-- 三方云服务相关 -->
|
<!-- 三方云服务相关 -->
|
||||||
<minio.version>8.2.2</minio.version>
|
<minio.version>8.2.2</minio.version>
|
||||||
<aliyun-java-sdk-core.version>4.6.0</aliyun-java-sdk-core.version>
|
<aliyun-java-sdk-core.version>4.6.0</aliyun-java-sdk-core.version>
|
||||||
|
@ -30,6 +30,7 @@ import org.springframework.context.annotation.Primary;
|
|||||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
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 org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -118,21 +119,14 @@ public class YudaoTenantAutoConfiguration {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 引入租户时,tenantRedisCacheManager为主Bean
|
|
||||||
*
|
|
||||||
* @param redisTemplate
|
|
||||||
* @param redisCacheConfiguration
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Bean
|
@Bean
|
||||||
@Primary
|
@Primary // 引入租户时,tenantRedisCacheManager 为主 Bean
|
||||||
public RedisCacheManager tenantRedisCacheManager(
|
public RedisCacheManager tenantRedisCacheManager(RedisTemplate<String, Object> redisTemplate,
|
||||||
RedisTemplate<String, Object> redisTemplate,
|
|
||||||
RedisCacheConfiguration redisCacheConfiguration) {
|
RedisCacheConfiguration redisCacheConfiguration) {
|
||||||
RedisCacheWriter cacheWriter =
|
// 创建 RedisCacheWriter 对象
|
||||||
RedisCacheWriter.nonLockingRedisCacheWriter(
|
RedisConnectionFactory connectionFactory = Objects.requireNonNull(redisTemplate.getConnectionFactory());
|
||||||
Objects.requireNonNull(redisTemplate.getConnectionFactory()));
|
RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
|
||||||
|
// 创建 TenantRedisCacheManager 对象
|
||||||
return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration);
|
return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,34 +2,37 @@ package cn.iocoder.yudao.framework.tenant.core.redis;
|
|||||||
|
|
||||||
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;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.springframework.cache.Cache;
|
import org.springframework.cache.Cache;
|
||||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 租户缓存管理
|
* 多租户的 {@link RedisCacheManager} 实现类
|
||||||
*
|
*
|
||||||
* 为cacheName增加自动增加租户表示,格式:name+":"+tenantId
|
* 操作指定 name 的 {@link Cache} 时,自动拼接租户后缀,格式为 name + ":" + tenantId
|
||||||
*
|
*
|
||||||
* @author airhead
|
* @author airhead
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class TenantRedisCacheManager extends RedisCacheManager {
|
public class TenantRedisCacheManager extends RedisCacheManager {
|
||||||
|
|
||||||
public TenantRedisCacheManager(
|
public TenantRedisCacheManager(RedisCacheWriter cacheWriter,
|
||||||
RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
|
RedisCacheConfiguration defaultCacheConfiguration) {
|
||||||
super(cacheWriter, defaultCacheConfiguration);
|
super(cacheWriter, defaultCacheConfiguration);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Cache getCache(String name) {
|
public Cache getCache(@NotNull String name) {
|
||||||
//租户未设置时,返回原始name
|
// 如果开启多租户,则 name 拼接租户后缀
|
||||||
if (TenantContextHolder.getTenantId() == null) {
|
if (!TenantContextHolder.isIgnore()
|
||||||
|
&& TenantContextHolder.getTenantId() != null) {
|
||||||
|
name = name + ":" + TenantContextHolder.getTenantId();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 继续基于父方法
|
||||||
return super.getCache(name);
|
return super.getCache(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
name = name + ":" + TenantContextHolder.getTenantId();
|
|
||||||
return super.getCache(name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,6 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.netty</groupId>
|
<groupId>io.netty</groupId>
|
||||||
<artifactId>netty-all</artifactId>
|
<artifactId>netty-all</artifactId>
|
||||||
<version>4.1.75.Final</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
@ -51,7 +51,7 @@ spring:
|
|||||||
# url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
|
# url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
|
||||||
# url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${spring.datasource.dynamic.datasource.master.name} # SQLServer 连接的示例
|
# url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${spring.datasource.dynamic.datasource.master.name} # SQLServer 连接的示例
|
||||||
username: root
|
username: root
|
||||||
password: 123456
|
password: ${RUOYI_VUE_PRO}
|
||||||
# username: sa
|
# username: sa
|
||||||
# password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W
|
# password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W
|
||||||
slave: # 模拟从库,可根据自己需要修改
|
slave: # 模拟从库,可根据自己需要修改
|
||||||
@ -62,7 +62,7 @@ spring:
|
|||||||
# url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
|
# url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例
|
||||||
# url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${spring.datasource.dynamic.datasource.slave.name} # SQLServer 连接的示例
|
# url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${spring.datasource.dynamic.datasource.slave.name} # SQLServer 连接的示例
|
||||||
username: root
|
username: root
|
||||||
password: 123456
|
password: ${RUOYI_VUE_PRO}
|
||||||
# username: sa
|
# username: sa
|
||||||
# password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W
|
# password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user