Commit 0e1babd7f41e87ecabab71524504be6466c42b78

Authored by airhead
1 parent dd6dc2ac

feat(redis): 注解支持多租户缓存

1.增加多租户Cache
2.修复Mac M1启动报错
yudao-dependencies/pom.xml
@@ -58,6 +58,7 @@ @@ -58,6 +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 <!-- 三方云服务相关 --> 62 <!-- 三方云服务相关 -->
62 <minio.version>8.2.2</minio.version> 63 <minio.version>8.2.2</minio.version>
63 <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>
@@ -131,11 +132,6 @@ @@ -131,11 +132,6 @@
131 <artifactId>yudao-spring-boot-starter-biz-error-code</artifactId> 132 <artifactId>yudao-spring-boot-starter-biz-error-code</artifactId>
132 <version>${revision}</version> 133 <version>${revision}</version>
133 </dependency> 134 </dependency>
134 - <dependency>  
135 - <groupId>cn.iocoder.boot</groupId>  
136 - <artifactId>yudao-spring-boot-starter-captcha</artifactId>  
137 - <version>${revision}</version>  
138 - </dependency>  
139 135
140 <!-- Spring 核心 --> 136 <!-- Spring 核心 -->
141 <dependency> 137 <dependency>
@@ -459,12 +455,6 @@ @@ -459,12 +455,6 @@
459 </dependency> 455 </dependency>
460 456
461 <dependency> 457 <dependency>
462 - <groupId>com.anji-plus</groupId>  
463 - <artifactId>spring-boot-starter-captcha</artifactId>  
464 - <version>${aj-captcha.version}</version>  
465 - </dependency>  
466 -  
467 - <dependency>  
468 <groupId>org.apache.velocity</groupId> 458 <groupId>org.apache.velocity</groupId>
469 <artifactId>velocity-engine-core</artifactId> 459 <artifactId>velocity-engine-core</artifactId>
470 <version>${velocity.version}</version> 460 <version>${velocity.version}</version>
@@ -521,6 +511,12 @@ @@ -521,6 +511,12 @@
521 <version>${jsch.version}</version> 511 <version>${jsch.version}</version>
522 </dependency> 512 </dependency>
523 513
  514 + <dependency>
  515 + <groupId>io.netty</groupId>
  516 + <artifactId>netty-all</artifactId>
  517 + <version>${netty-all.version}</version>
  518 + </dependency>
  519 +
524 <!-- 三方云服务相关 --> 520 <!-- 三方云服务相关 -->
525 <dependency> 521 <dependency>
526 <groupId>cn.iocoder.boot</groupId> 522 <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,6 +9,7 @@ import cn.iocoder.yudao.framework.tenant.core.db.TenantDatabaseInterceptor;
9 import cn.iocoder.yudao.framework.tenant.core.job.TenantJob; 9 import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
10 import cn.iocoder.yudao.framework.tenant.core.job.TenantJobHandlerDecorator; 10 import cn.iocoder.yudao.framework.tenant.core.job.TenantJobHandlerDecorator;
11 import cn.iocoder.yudao.framework.tenant.core.mq.TenantRedisMessageInterceptor; 11 import cn.iocoder.yudao.framework.tenant.core.mq.TenantRedisMessageInterceptor;
  12 +import cn.iocoder.yudao.framework.tenant.core.redis.TenantRedisCacheManager;
12 import cn.iocoder.yudao.framework.tenant.core.security.TenantSecurityWebFilter; 13 import cn.iocoder.yudao.framework.tenant.core.security.TenantSecurityWebFilter;
13 import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService; 14 import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService;
14 import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkServiceImpl; 15 import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkServiceImpl;
@@ -25,6 +26,13 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties @@ -25,6 +26,13 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
25 import org.springframework.boot.web.servlet.FilterRegistrationBean; 26 import org.springframework.boot.web.servlet.FilterRegistrationBean;
26 import org.springframework.context.annotation.Bean; 27 import org.springframework.context.annotation.Bean;
27 import org.springframework.context.annotation.Configuration; 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 @Configuration 37 @Configuration
30 @ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true) // 允许使用 yudao.tenant.enable=false 禁用多租户 38 @ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true) // 允许使用 yudao.tenant.enable=false 禁用多租户
@@ -110,4 +118,15 @@ public class YudaoTenantAutoConfiguration { @@ -110,4 +118,15 @@ public class YudaoTenantAutoConfiguration {
110 }; 118 };
111 } 119 }
112 120
  121 + @Bean
  122 + @Primary
  123 + public RedisCacheManager tenantRedisCacheManager(
  124 + RedisTemplate<String, Object> redisTemplate,
  125 + RedisCacheConfiguration redisCacheConfiguration) {
  126 + RedisCacheWriter cacheWriter =
  127 + RedisCacheWriter.nonLockingRedisCacheWriter(
  128 + Objects.requireNonNull(redisTemplate.getConnectionFactory()));
  129 + return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration);
  130 + }
  131 +
113 } 132 }
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 + * @author airhead
  12 + */
  13 +@Slf4j
  14 +public class TenantRedisCacheManager extends RedisCacheManager {
  15 + public TenantRedisCacheManager(
  16 + RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
  17 + super(cacheWriter, defaultCacheConfiguration);
  18 + }
  19 +
  20 + @Override
  21 + public Cache getCache(String name) {
  22 + if (TenantContextHolder.getTenantId() == null) {
  23 + return super.getCache(name);
  24 + }
  25 +
  26 + name = name + ":" + TenantContextHolder.getTenantId();
  27 + return super.getCache(name);
  28 + }
  29 +}
yudao-framework/yudao-spring-boot-starter-redis/pom.xml
@@ -32,6 +32,12 @@ @@ -32,6 +32,12 @@
32 <artifactId>spring-boot-starter-cache</artifactId> <!-- 实现对 Caches 的自动化配置 --> 32 <artifactId>spring-boot-starter-cache</artifactId> <!-- 实现对 Caches 的自动化配置 -->
33 </dependency> 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 </dependencies> 41 </dependencies>
36 42
37 </project> 43 </project>
yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoCacheAutoConfiguration.java
1 package cn.iocoder.yudao.framework.redis.config; 1 package cn.iocoder.yudao.framework.redis.config;
2 2
3 import org.springframework.boot.autoconfigure.cache.CacheProperties; 3 import org.springframework.boot.autoconfigure.cache.CacheProperties;
  4 +import org.springframework.boot.context.properties.EnableConfigurationProperties;
4 import org.springframework.cache.annotation.EnableCaching; 5 import org.springframework.cache.annotation.EnableCaching;
5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Bean;
6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.Configuration;
@@ -13,6 +14,7 @@ import org.springframework.data.redis.serializer.RedisSerializer; @@ -13,6 +14,7 @@ import org.springframework.data.redis.serializer.RedisSerializer;
13 * Cache 配置类,基于 Redis 实现 14 * Cache 配置类,基于 Redis 实现
14 */ 15 */
15 @Configuration 16 @Configuration
  17 +@EnableConfigurationProperties({CacheProperties.class})
16 @EnableCaching 18 @EnableCaching
17 public class YudaoCacheAutoConfiguration { 19 public class YudaoCacheAutoConfiguration {
18 20