Commit 0e1babd7f41e87ecabab71524504be6466c42b78
1 parent
dd6dc2ac
feat(redis): 注解支持多租户缓存
1.增加多租户Cache 2.修复Mac M1启动报错
Showing
5 changed files
with
63 additions
and
11 deletions
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> |
| ... | ... | @@ -131,11 +132,6 @@ |
| 131 | 132 | <artifactId>yudao-spring-boot-starter-biz-error-code</artifactId> |
| 132 | 133 | <version>${revision}</version> |
| 133 | 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 | 136 | <!-- Spring 核心 --> |
| 141 | 137 | <dependency> |
| ... | ... | @@ -459,12 +455,6 @@ |
| 459 | 455 | </dependency> |
| 460 | 456 | |
| 461 | 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 | 458 | <groupId>org.apache.velocity</groupId> |
| 469 | 459 | <artifactId>velocity-engine-core</artifactId> |
| 470 | 460 | <version>${velocity.version}</version> |
| ... | ... | @@ -521,6 +511,12 @@ |
| 521 | 511 | <version>${jsch.version}</version> |
| 522 | 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 | 521 | <dependency> |
| 526 | 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 | 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,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 | 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 | ... | ... |