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

1.增加多租户Cache
2.修复Mac M1启动报错
This commit is contained in:
airhead 2022-08-23 16:56:51 +08:00
parent 185ea9e861
commit 310b5b00a7
5 changed files with 63 additions and 0 deletions

View File

@ -57,6 +57,7 @@
<commons-net.version>3.8.0</commons-net.version> <commons-net.version>3.8.0</commons-net.version>
<jsch.version>0.1.55</jsch.version> <jsch.version>0.1.55</jsch.version>
<tika-core.version>2.4.1</tika-core.version> <tika-core.version>2.4.1</tika-core.version>
<netty-all.version>4.1.75.Final</netty-all.version>
<!-- 三方云服务相关 --> <!-- 三方云服务相关 -->
<minio.version>8.2.2</minio.version> <minio.version>8.2.2</minio.version>
<aliyun-java-sdk-core.version>4.5.25</aliyun-java-sdk-core.version> <aliyun-java-sdk-core.version>4.5.25</aliyun-java-sdk-core.version>
@ -509,6 +510,12 @@
<version>${jsch.version}</version> <version>${jsch.version}</version>
</dependency> </dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty-all.version}</version>
</dependency>
<!-- 三方云服务相关 --> <!-- 三方云服务相关 -->
<dependency> <dependency>
<groupId>cn.iocoder.boot</groupId> <groupId>cn.iocoder.boot</groupId>

View File

@ -9,6 +9,7 @@ import cn.iocoder.yudao.framework.tenant.core.db.TenantDatabaseInterceptor;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob; import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJobHandlerDecorator; import cn.iocoder.yudao.framework.tenant.core.job.TenantJobHandlerDecorator;
import cn.iocoder.yudao.framework.tenant.core.mq.TenantRedisMessageInterceptor; import cn.iocoder.yudao.framework.tenant.core.mq.TenantRedisMessageInterceptor;
import cn.iocoder.yudao.framework.tenant.core.redis.TenantRedisCacheManager;
import cn.iocoder.yudao.framework.tenant.core.security.TenantSecurityWebFilter; import cn.iocoder.yudao.framework.tenant.core.security.TenantSecurityWebFilter;
import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService; import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService;
import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkServiceImpl; import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkServiceImpl;
@ -25,6 +26,13 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Objects;
@Configuration @Configuration
@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true) // 允许使用 yudao.tenant.enable=false 禁用多租户 @ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true) // 允许使用 yudao.tenant.enable=false 禁用多租户
@ -110,4 +118,15 @@ public class YudaoTenantAutoConfiguration {
}; };
} }
@Bean
@Primary
public RedisCacheManager tenantRedisCacheManager(
RedisTemplate<String, Object> redisTemplate,
RedisCacheConfiguration redisCacheConfiguration) {
RedisCacheWriter cacheWriter =
RedisCacheWriter.nonLockingRedisCacheWriter(
Objects.requireNonNull(redisTemplate.getConnectionFactory()));
return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration);
}
} }

View File

@ -0,0 +1,29 @@
package cn.iocoder.yudao.framework.tenant.core.redis;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
/**
* @author airhead
*/
@Slf4j
public class TenantRedisCacheManager extends RedisCacheManager {
public TenantRedisCacheManager(
RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
super(cacheWriter, defaultCacheConfiguration);
}
@Override
public Cache getCache(String name) {
if (TenantContextHolder.getTenantId() == null) {
return super.getCache(name);
}
name = name + ":" + TenantContextHolder.getTenantId();
return super.getCache(name);
}
}

View File

@ -32,6 +32,12 @@
<artifactId>spring-boot-starter-cache</artifactId> <!-- 实现对 Caches 的自动化配置 --> <artifactId>spring-boot-starter-cache</artifactId> <!-- 实现对 Caches 的自动化配置 -->
</dependency> </dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.75.Final</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.framework.redis.config; package cn.iocoder.yudao.framework.redis.config;
import org.springframework.boot.autoconfigure.cache.CacheProperties; import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -13,6 +14,7 @@ import org.springframework.data.redis.serializer.RedisSerializer;
* Cache 配置类基于 Redis 实现 * Cache 配置类基于 Redis 实现
*/ */
@Configuration @Configuration
@EnableConfigurationProperties({CacheProperties.class})
@EnableCaching @EnableCaching
public class YudaoCacheAutoConfiguration { public class YudaoCacheAutoConfiguration {