Commit 310b5b00a7c382778804c56b40707539e3925837
1 parent
185ea9e8
feat(redis): 注解支持多租户缓存
1.增加多租户Cache 2.修复Mac M1启动报错
Showing
5 changed files
with
63 additions
and
0 deletions
yudao-dependencies/pom.xml
| @@ -57,6 +57,7 @@ | @@ -57,6 +57,7 @@ | ||
| 57 | <commons-net.version>3.8.0</commons-net.version> | 57 | <commons-net.version>3.8.0</commons-net.version> |
| 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 | + <netty-all.version>4.1.75.Final</netty-all.version> | ||
| 60 | <!-- 三方云服务相关 --> | 61 | <!-- 三方云服务相关 --> |
| 61 | <minio.version>8.2.2</minio.version> | 62 | <minio.version>8.2.2</minio.version> |
| 62 | <aliyun-java-sdk-core.version>4.5.25</aliyun-java-sdk-core.version> | 63 | <aliyun-java-sdk-core.version>4.5.25</aliyun-java-sdk-core.version> |
| @@ -509,6 +510,12 @@ | @@ -509,6 +510,12 @@ | ||
| 509 | <version>${jsch.version}</version> | 510 | <version>${jsch.version}</version> |
| 510 | </dependency> | 511 | </dependency> |
| 511 | 512 | ||
| 513 | + <dependency> | ||
| 514 | + <groupId>io.netty</groupId> | ||
| 515 | + <artifactId>netty-all</artifactId> | ||
| 516 | + <version>${netty-all.version}</version> | ||
| 517 | + </dependency> | ||
| 518 | + | ||
| 512 | <!-- 三方云服务相关 --> | 519 | <!-- 三方云服务相关 --> |
| 513 | <dependency> | 520 | <dependency> |
| 514 | <groupId>cn.iocoder.boot</groupId> | 521 | <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 |