fast json 的配置 cacheManage
java
@Configuration
public class ConfigRedisCacheManager {
// 缓存自动过期时间
private long expire = 4;
static {
ParserConfig.getGlobalInstance().addAccept("com");
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
}
@Bean
@Primary
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.disableCachingNullValues()
.entryTtl(Duration.ofSeconds(expire))
.serializeKeysWith(keySerializationPair())
.serializeValuesWith(valueSerializationPair());
return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(cacheConfiguration)
.build();
}
private RedisSerializationContext.SerializationPair<String> keySerializationPair() {
return RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer());
}
private RedisSerializationContext.SerializationPair<Object> valueSerializationPair() {
return RedisSerializationContext.SerializationPair.fromSerializer(new FastJsonRedisSerializer<>());
}
public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
try {
return JSON.toJSONBytes(t, SerializerFeature.WriteClassName);
//return JSON.toJSONBytes(t);
} catch (Exception ex) {
throw new SerializationException("Could not write JSON: " + ex.getMessage(), ex);
}
}
@SneakyThrows
@Override
public T deserialize(byte[] bytes) throws SerializationException {
String data = new String(bytes, "UTF-8");
T result = (T) JSON.parse(data);
return result;
}
}
}
jackson 方式 配置 cacheManager
java
/**
* RedisCacheConfiguration Bean
*
* 参考 org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration 的 createConfiguration 方法
*/
@Bean
@Primary
public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
// 设置使用 JSON 序列化方式
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
//config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = JsonMapper.builder().addModule(new JavaTimeModule()).build();
objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.EVERYTHING);
serializer.setObjectMapper(objectMapper);
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));
// 设置 CacheProperties.Redis 的属性
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
return config;
}