|
@@ -1,12 +1,25 @@
|
|
|
package com.etotem.cfc.config;
|
|
package com.etotem.cfc.config;
|
|
|
|
|
|
|
|
|
|
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
|
|
+import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
|
|
|
|
|
+import org.springframework.cache.CacheManager;
|
|
|
|
|
+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;
|
|
|
|
|
+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.serializer.GenericJackson2JsonRedisSerializer;
|
|
|
|
|
+import org.springframework.data.redis.serializer.RedisSerializationContext;
|
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
|
|
|
|
|
|
|
+import java.time.Duration;
|
|
|
|
|
+
|
|
|
@Configuration
|
|
@Configuration
|
|
|
|
|
+@EnableCaching
|
|
|
public class RedisConfig {
|
|
public class RedisConfig {
|
|
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
@@ -21,4 +34,33 @@ public class RedisConfig {
|
|
|
template.afterPropertiesSet();
|
|
template.afterPropertiesSet();
|
|
|
return template;
|
|
return template;
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Spring Cache 的 Redis 缓存管理器。
|
|
|
|
|
+ * - value 使用 GenericJackson2JsonRedisSerializer 序列化,支持缓存复杂对象
|
|
|
|
|
+ * - 默认 TTL 10 分钟(缓存名带 "#{ttl}" 后缀可自定义,如 "products#3600")
|
|
|
|
|
+ * - 禁用空值缓存(防止缓存穿透)
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public CacheManager cacheManager(RedisConnectionFactory factory) {
|
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
+ mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
|
|
|
+ mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
|
|
|
|
|
+ ObjectMapper.DefaultTyping.NON_FINAL);
|
|
|
|
|
+ GenericJackson2JsonRedisSerializer jsonSerializer =
|
|
|
|
|
+ new GenericJackson2JsonRedisSerializer(mapper);
|
|
|
|
|
+
|
|
|
|
|
+ RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
|
|
|
|
|
+ .serializeKeysWith(RedisSerializationContext.SerializationPair
|
|
|
|
|
+ .fromSerializer(new StringRedisSerializer()))
|
|
|
|
|
+ .serializeValuesWith(RedisSerializationContext.SerializationPair
|
|
|
|
|
+ .fromSerializer(jsonSerializer))
|
|
|
|
|
+ .entryTtl(Duration.ofMinutes(10))
|
|
|
|
|
+ .disableCachingNullValues();
|
|
|
|
|
+
|
|
|
|
|
+ return RedisCacheManager.builder(factory)
|
|
|
|
|
+ .cacheDefaults(config)
|
|
|
|
|
+ .transactionAware()
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|