zoukankan      html  css  js  c++  java
  • Spring Boot自定义Redis缓存配置,保存value格式JSON字符串

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

    部分内容转自 https://blog.csdn.net/caojidasabi/article/details/83059642

    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;
    
    @Configuration
    public class MyRedisConfig
    {
    
        //自定义RedisTemplate
        @Bean
        public RedisTemplate<Object, Student> student_redis_template(RedisConnectionFactory redisConnectionFactory)
        {
            RedisTemplate<Object, Student> template = new RedisTemplate<Object, Student>();
            template.setConnectionFactory(redisConnectionFactory);
            //使用JSON格式的序列化,保存
            Jackson2JsonRedisSerializer<Student> serializer = new Jackson2JsonRedisSerializer<Student>(Student.class);
            template.setDefaultSerializer(serializer);
            return template;
        }
    
        //自定义cacheManager缓存管理器
        @Bean
        public CacheManager cacheManager(RedisConnectionFactory factory)
        {
            RedisSerializer<String> 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配置
  • 相关阅读:
    linux下svn命令使用大全(share)
    vi 编辑器命令 (share)
    如何成为一名优秀的前端工程师 (share)
    正则表达式入门教程&&经典Javascript正则表达式(share)
    动手学深度学习11- 多层感知机pytorch简洁实现
    动手学深度学习10- pytorch多层感知机从零实现
    动手学深度学习9-多层感知机pytorch
    动手学深度学习8-softmax分类pytorch简洁实现
    动手学深度学习7-从零开始完成softmax分类
    动手学深度学习6-认识Fashion_MNIST图像数据集
  • 原文地址:https://www.cnblogs.com/taopanfeng/p/11009088.html
Copyright © 2011-2022 走看看