fast json 的配置 cacheManage
@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
/**
* 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;
}
文章来源地址https://www.toymoban.com/news/detail-597018.html
文章来源:https://www.toymoban.com/news/detail-597018.html
到了这里,关于spring boot 配置cacheManager EnableCaching EnableCaching的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!