Commit 1be09d092bf18c641f6391900bba8beca142749e
1 parent
597a9238
♻️优化“通过注解增加多租户缓存”的代码
Showing
5 changed files
with
29 additions
and
33 deletions
yudao-dependencies/pom.xml
| @@ -58,7 +58,7 @@ | @@ -58,7 +58,7 @@ | ||
| 58 | <jsch.version>0.1.55</jsch.version> | 58 | <jsch.version>0.1.55</jsch.version> |
| 59 | <tika-core.version>2.4.1</tika-core.version> | 59 | <tika-core.version>2.4.1</tika-core.version> |
| 60 | <aj-captcha.version>1.3.0</aj-captcha.version> | 60 | <aj-captcha.version>1.3.0</aj-captcha.version> |
| 61 | - <netty-all.version>4.1.75.Final</netty-all.version> | 61 | + <netty-all.version>4.1.82.Final</netty-all.version> |
| 62 | <!-- 三方云服务相关 --> | 62 | <!-- 三方云服务相关 --> |
| 63 | <minio.version>8.2.2</minio.version> | 63 | <minio.version>8.2.2</minio.version> |
| 64 | <aliyun-java-sdk-core.version>4.6.0</aliyun-java-sdk-core.version> | 64 | <aliyun-java-sdk-core.version>4.6.0</aliyun-java-sdk-core.version> |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantAutoConfiguration.java
| @@ -30,6 +30,7 @@ import org.springframework.context.annotation.Primary; | @@ -30,6 +30,7 @@ import org.springframework.context.annotation.Primary; | ||
| 30 | import org.springframework.data.redis.cache.RedisCacheConfiguration; | 30 | import org.springframework.data.redis.cache.RedisCacheConfiguration; |
| 31 | import org.springframework.data.redis.cache.RedisCacheManager; | 31 | import org.springframework.data.redis.cache.RedisCacheManager; |
| 32 | import org.springframework.data.redis.cache.RedisCacheWriter; | 32 | import org.springframework.data.redis.cache.RedisCacheWriter; |
| 33 | +import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
| 33 | import org.springframework.data.redis.core.RedisTemplate; | 34 | import org.springframework.data.redis.core.RedisTemplate; |
| 34 | 35 | ||
| 35 | import java.util.Objects; | 36 | import java.util.Objects; |
| @@ -118,21 +119,14 @@ public class YudaoTenantAutoConfiguration { | @@ -118,21 +119,14 @@ public class YudaoTenantAutoConfiguration { | ||
| 118 | }; | 119 | }; |
| 119 | } | 120 | } |
| 120 | 121 | ||
| 121 | - /** | ||
| 122 | - * 引入租户时,tenantRedisCacheManager为主Bean | ||
| 123 | - * | ||
| 124 | - * @param redisTemplate | ||
| 125 | - * @param redisCacheConfiguration | ||
| 126 | - * @return | ||
| 127 | - */ | ||
| 128 | - @Bean | ||
| 129 | - @Primary | ||
| 130 | - public RedisCacheManager tenantRedisCacheManager( | ||
| 131 | - RedisTemplate<String, Object> redisTemplate, | ||
| 132 | - RedisCacheConfiguration redisCacheConfiguration) { | ||
| 133 | - RedisCacheWriter cacheWriter = | ||
| 134 | - RedisCacheWriter.nonLockingRedisCacheWriter( | ||
| 135 | - Objects.requireNonNull(redisTemplate.getConnectionFactory())); | 122 | + @Bean |
| 123 | + @Primary // 引入租户时,tenantRedisCacheManager 为主 Bean | ||
| 124 | + public RedisCacheManager tenantRedisCacheManager(RedisTemplate<String, Object> redisTemplate, | ||
| 125 | + RedisCacheConfiguration redisCacheConfiguration) { | ||
| 126 | + // 创建 RedisCacheWriter 对象 | ||
| 127 | + RedisConnectionFactory connectionFactory = Objects.requireNonNull(redisTemplate.getConnectionFactory()); | ||
| 128 | + RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory); | ||
| 129 | + // 创建 TenantRedisCacheManager 对象 | ||
| 136 | return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration); | 130 | return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration); |
| 137 | } | 131 | } |
| 138 | 132 |
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/redis/TenantRedisCacheManager.java
| @@ -2,34 +2,37 @@ package cn.iocoder.yudao.framework.tenant.core.redis; | @@ -2,34 +2,37 @@ package cn.iocoder.yudao.framework.tenant.core.redis; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; | 3 | import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; |
| 4 | import lombok.extern.slf4j.Slf4j; | 4 | import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.jetbrains.annotations.NotNull; | ||
| 5 | import org.springframework.cache.Cache; | 6 | import org.springframework.cache.Cache; |
| 6 | import org.springframework.data.redis.cache.RedisCacheConfiguration; | 7 | import org.springframework.data.redis.cache.RedisCacheConfiguration; |
| 7 | import org.springframework.data.redis.cache.RedisCacheManager; | 8 | import org.springframework.data.redis.cache.RedisCacheManager; |
| 8 | import org.springframework.data.redis.cache.RedisCacheWriter; | 9 | import org.springframework.data.redis.cache.RedisCacheWriter; |
| 9 | 10 | ||
| 10 | /** | 11 | /** |
| 11 | - * 租户缓存管理 | 12 | + * 多租户的 {@link RedisCacheManager} 实现类 |
| 12 | * | 13 | * |
| 13 | - * 为cacheName增加自动增加租户表示,格式:name+":"+tenantId | 14 | + * 操作指定 name 的 {@link Cache} 时,自动拼接租户后缀,格式为 name + ":" + tenantId |
| 14 | * | 15 | * |
| 15 | * @author airhead | 16 | * @author airhead |
| 16 | */ | 17 | */ |
| 17 | @Slf4j | 18 | @Slf4j |
| 18 | public class TenantRedisCacheManager extends RedisCacheManager { | 19 | public class TenantRedisCacheManager extends RedisCacheManager { |
| 19 | 20 | ||
| 20 | - public TenantRedisCacheManager( | ||
| 21 | - RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) { | ||
| 22 | - super(cacheWriter, defaultCacheConfiguration); | ||
| 23 | - } | 21 | + public TenantRedisCacheManager(RedisCacheWriter cacheWriter, |
| 22 | + RedisCacheConfiguration defaultCacheConfiguration) { | ||
| 23 | + super(cacheWriter, defaultCacheConfiguration); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + @Override | ||
| 27 | + public Cache getCache(@NotNull String name) { | ||
| 28 | + // 如果开启多租户,则 name 拼接租户后缀 | ||
| 29 | + if (!TenantContextHolder.isIgnore() | ||
| 30 | + && TenantContextHolder.getTenantId() != null) { | ||
| 31 | + name = name + ":" + TenantContextHolder.getTenantId(); | ||
| 32 | + } | ||
| 24 | 33 | ||
| 25 | - @Override | ||
| 26 | - public Cache getCache(String name) { | ||
| 27 | - //租户未设置时,返回原始name | ||
| 28 | - if (TenantContextHolder.getTenantId() == null) { | ||
| 29 | - return super.getCache(name); | 34 | + // 继续基于父方法 |
| 35 | + return super.getCache(name); | ||
| 30 | } | 36 | } |
| 31 | 37 | ||
| 32 | - name = name + ":" + TenantContextHolder.getTenantId(); | ||
| 33 | - return super.getCache(name); | ||
| 34 | - } | ||
| 35 | } | 38 | } |
yudao-framework/yudao-spring-boot-starter-redis/pom.xml
| @@ -35,7 +35,6 @@ | @@ -35,7 +35,6 @@ | ||
| 35 | <dependency> | 35 | <dependency> |
| 36 | <groupId>io.netty</groupId> | 36 | <groupId>io.netty</groupId> |
| 37 | <artifactId>netty-all</artifactId> | 37 | <artifactId>netty-all</artifactId> |
| 38 | - <version>4.1.75.Final</version> | ||
| 39 | </dependency> | 38 | </dependency> |
| 40 | 39 | ||
| 41 | </dependencies> | 40 | </dependencies> |
yudao-server/src/main/resources/application-local.yaml
| @@ -51,7 +51,7 @@ spring: | @@ -51,7 +51,7 @@ spring: | ||
| 51 | # url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例 | 51 | # url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例 |
| 52 | # url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${spring.datasource.dynamic.datasource.master.name} # SQLServer 连接的示例 | 52 | # url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${spring.datasource.dynamic.datasource.master.name} # SQLServer 连接的示例 |
| 53 | username: root | 53 | username: root |
| 54 | - password: 123456 | 54 | + password: ${RUOYI_VUE_PRO} |
| 55 | # username: sa | 55 | # username: sa |
| 56 | # password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W | 56 | # password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W |
| 57 | slave: # 模拟从库,可根据自己需要修改 | 57 | slave: # 模拟从库,可根据自己需要修改 |
| @@ -62,7 +62,7 @@ spring: | @@ -62,7 +62,7 @@ spring: | ||
| 62 | # url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例 | 62 | # url: jdbc:oracle:thin:@127.0.0.1:1521:xe # Oracle 连接的示例 |
| 63 | # url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${spring.datasource.dynamic.datasource.slave.name} # SQLServer 连接的示例 | 63 | # url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${spring.datasource.dynamic.datasource.slave.name} # SQLServer 连接的示例 |
| 64 | username: root | 64 | username: root |
| 65 | - password: 123456 | 65 | + password: ${RUOYI_VUE_PRO} |
| 66 | # username: sa | 66 | # username: sa |
| 67 | # password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W | 67 | # password: JSm:g(*%lU4ZAkz06cd52KqT3)i1?H7W |
| 68 | 68 |