Commit 597a9238281743a1243e1a85d7a1775330b9e737

Authored by 芋道源码
Committed by Gitee
2 parents 7643ed5a 02c45e40

!257 通过注解增加多租户缓存

Merge pull request !257 from CrazyAirhead/master
yudao-dependencies/pom.xml
... ... @@ -58,6 +58,7 @@
58 58 <jsch.version>0.1.55</jsch.version>
59 59 <tika-core.version>2.4.1</tika-core.version>
60 60 <aj-captcha.version>1.3.0</aj-captcha.version>
  61 + <netty-all.version>4.1.75.Final</netty-all.version>
61 62 <!-- 三方云服务相关 -->
62 63 <minio.version>8.2.2</minio.version>
63 64 <aliyun-java-sdk-core.version>4.6.0</aliyun-java-sdk-core.version>
... ... @@ -521,6 +522,12 @@
521 522 <version>${jsch.version}</version>
522 523 </dependency>
523 524  
  525 + <dependency>
  526 + <groupId>io.netty</groupId>
  527 + <artifactId>netty-all</artifactId>
  528 + <version>${netty-all.version}</version>
  529 + </dependency>
  530 +
524 531 <!-- 三方云服务相关 -->
525 532 <dependency>
526 533 <groupId>cn.iocoder.boot</groupId>
... ...
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantAutoConfiguration.java
... ... @@ -9,6 +9,7 @@ import cn.iocoder.yudao.framework.tenant.core.db.TenantDatabaseInterceptor;
9 9 import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
10 10 import cn.iocoder.yudao.framework.tenant.core.job.TenantJobHandlerDecorator;
11 11 import cn.iocoder.yudao.framework.tenant.core.mq.TenantRedisMessageInterceptor;
  12 +import cn.iocoder.yudao.framework.tenant.core.redis.TenantRedisCacheManager;
12 13 import cn.iocoder.yudao.framework.tenant.core.security.TenantSecurityWebFilter;
13 14 import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService;
14 15 import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkServiceImpl;
... ... @@ -25,6 +26,13 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
25 26 import org.springframework.boot.web.servlet.FilterRegistrationBean;
26 27 import org.springframework.context.annotation.Bean;
27 28 import org.springframework.context.annotation.Configuration;
  29 +import org.springframework.context.annotation.Primary;
  30 +import org.springframework.data.redis.cache.RedisCacheConfiguration;
  31 +import org.springframework.data.redis.cache.RedisCacheManager;
  32 +import org.springframework.data.redis.cache.RedisCacheWriter;
  33 +import org.springframework.data.redis.core.RedisTemplate;
  34 +
  35 +import java.util.Objects;
28 36  
29 37 @Configuration
30 38 @ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true) // 允许使用 yudao.tenant.enable=false 禁用多租户
... ... @@ -110,4 +118,22 @@ public class YudaoTenantAutoConfiguration {
110 118 };
111 119 }
112 120  
  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()));
  136 + return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration);
  137 + }
  138 +
113 139 }
... ...
yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/redis/TenantRedisCacheManager.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.tenant.core.redis;
  2 +
  3 +import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
  4 +import lombok.extern.slf4j.Slf4j;
  5 +import org.springframework.cache.Cache;
  6 +import org.springframework.data.redis.cache.RedisCacheConfiguration;
  7 +import org.springframework.data.redis.cache.RedisCacheManager;
  8 +import org.springframework.data.redis.cache.RedisCacheWriter;
  9 +
  10 +/**
  11 + * 租户缓存管理
  12 + *
  13 + * 为cacheName增加自动增加租户表示,格式:name+":"+tenantId
  14 + *
  15 + * @author airhead
  16 + */
  17 +@Slf4j
  18 +public class TenantRedisCacheManager extends RedisCacheManager {
  19 +
  20 + public TenantRedisCacheManager(
  21 + RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
  22 + super(cacheWriter, defaultCacheConfiguration);
  23 + }
  24 +
  25 + @Override
  26 + public Cache getCache(String name) {
  27 + //租户未设置时,返回原始name
  28 + if (TenantContextHolder.getTenantId() == null) {
  29 + return super.getCache(name);
  30 + }
  31 +
  32 + name = name + ":" + TenantContextHolder.getTenantId();
  33 + return super.getCache(name);
  34 + }
  35 +}
... ...
yudao-framework/yudao-spring-boot-starter-redis/pom.xml
... ... @@ -32,6 +32,12 @@
32 32 <artifactId>spring-boot-starter-cache</artifactId> <!-- 实现对 Caches 的自动化配置 -->
33 33 </dependency>
34 34  
  35 + <dependency>
  36 + <groupId>io.netty</groupId>
  37 + <artifactId>netty-all</artifactId>
  38 + <version>4.1.75.Final</version>
  39 + </dependency>
  40 +
35 41 </dependencies>
36 42  
37 43 </project>
... ...
yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoCacheAutoConfiguration.java
1 1 package cn.iocoder.yudao.framework.redis.config;
2 2  
3 3 import org.springframework.boot.autoconfigure.cache.CacheProperties;
  4 +import org.springframework.boot.context.properties.EnableConfigurationProperties;
4 5 import org.springframework.cache.annotation.EnableCaching;
5 6 import org.springframework.context.annotation.Bean;
6 7 import org.springframework.context.annotation.Configuration;
... ... @@ -13,6 +14,7 @@ import org.springframework.data.redis.serializer.RedisSerializer;
13 14 * Cache 配置类,基于 Redis 实现
14 15 */
15 16 @Configuration
  17 +@EnableConfigurationProperties({CacheProperties.class})
16 18 @EnableCaching
17 19 public class YudaoCacheAutoConfiguration {
18 20  
... ...