博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot自定义Redis缓存配置,保存value格式JSON字符串
阅读量:5901 次
发布时间:2019-06-19

本文共 2734 字,大约阅读时间需要 9 分钟。

Spring Boot自定义Redis缓存,保存格式JSON字符串

部分内容转自

package springboot01cache.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.cache.CacheManager;import org.springframework.context.annotation.Bean;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.core.RedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializationContext;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import springboot01cache.bean.Student;import java.net.UnknownHostException;import java.time.Duration;@Configurationpublic class MyRedisConfig{    //自定义RedisTemplate    @Bean    public RedisTemplate
student_redis_template(RedisConnectionFactory redisConnectionFactory) { RedisTemplate
template = new RedisTemplate
(); template.setConnectionFactory(redisConnectionFactory); //使用JSON格式的序列化,保存 Jackson2JsonRedisSerializer
serializer = new Jackson2JsonRedisSerializer
(Student.class); template.setDefaultSerializer(serializer); return template; } //自定义cacheManager缓存管理器 @Bean public CacheManager cacheManager(RedisConnectionFactory factory) { RedisSerializer
redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); //解决查询缓存转换异常的问题 ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); //配置序列化(解决乱码的问题) RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ZERO) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); return cacheManager; }}
自定义Redis配置

转载于:https://www.cnblogs.com/taopanfeng/p/11009088.html

你可能感兴趣的文章
IT职场人生系列之二:大学生活
查看>>
手把手教你做出好看的文本输入框
查看>>
zabbix 3.2.7 (源码包)安装部署
查看>>
vsCode 快捷键、插件
查看>>
vue-validator(vue验证器)
查看>>
jQuery Ajax MVC 下拉框联动
查看>>
html
查看>>
c#创建文件夹
查看>>
Hibernate事务代码规范写法
查看>>
网络最大流问题算法小结 [转]
查看>>
面试之Java知识整理
查看>>
iOS推送消息报错误“Domain=NSCocoaErrorDomain Code=3000”的可能问题
查看>>
kvm-1
查看>>
hdu1045 Fire Net---二进制枚举子集
查看>>
leetcode 64. Minimum Path Sum
查看>>
textkit
查看>>
CentOS7+CDH5.14.0安装CDH错误排查: HiveServer2 该角色的进程已退出。该角色的预期状态为已启动...
查看>>
The Oregon Trail 俄勒冈之旅
查看>>
Excel VBA连接MySql 数据库获取数据
查看>>
Developing a Service Provider using Java API(Service Provider Interface)(转)
查看>>