|
@@ -3,17 +3,22 @@ package com.fs.common.config;
|
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import org.springframework.cache.CacheManager;
|
|
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
|
|
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.context.annotation.Primary;
|
|
|
|
|
+import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
|
|
|
|
+import org.springframework.data.redis.cache.RedisCacheManager;
|
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
|
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
|
|
import org.springframework.data.redis.serializer.GenericToStringSerializer;
|
|
import org.springframework.data.redis.serializer.GenericToStringSerializer;
|
|
|
|
|
+import org.springframework.data.redis.serializer.RedisSerializationContext;
|
|
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
|
|
+import java.time.Duration;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Redis 配置类
|
|
* Redis 配置类
|
|
@@ -38,6 +43,31 @@ public class RedisConfig extends CachingConfigurerSupport {
|
|
|
return new TenantKeyRedisSerializer();
|
|
return new TenantKeyRedisSerializer();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Spring Cache(@Cacheable / @CacheEvict)使用与 RedisTemplate 相同的租户 Key 前缀
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public CacheManager cacheManager(
|
|
|
|
|
+ RedisConnectionFactory connectionFactory,
|
|
|
|
|
+ RedisSerializer<String> tenantKeySerializer) {
|
|
|
|
|
+ FastJson2JsonRedisSerializer<Object> valueSerializer =
|
|
|
|
|
+ new FastJson2JsonRedisSerializer<>(Object.class);
|
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
+ mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
|
|
|
+ mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
|
|
|
|
+ valueSerializer.setObjectMapper(mapper);
|
|
|
|
|
+
|
|
|
|
|
+ RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
|
|
|
|
|
+ .entryTtl(Duration.ofDays(7))
|
|
|
|
|
+ .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(tenantKeySerializer))
|
|
|
|
|
+ .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer))
|
|
|
|
|
+ .disableCachingNullValues();
|
|
|
|
|
+
|
|
|
|
|
+ return RedisCacheManager.builder(connectionFactory)
|
|
|
|
|
+ .cacheDefaults(cacheConfiguration)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 通用 RedisTemplate(Object -> Object)
|
|
* 通用 RedisTemplate(Object -> Object)
|
|
|
* 用于大多数业务缓存场景
|
|
* 用于大多数业务缓存场景
|