• 【Redis】RedisTemplate序列化传输数据


    使用自定义的序列化

    使用RedisTemplate默认的序列化器发送数据,会将key全都当成Object处理,从而按照对象的方式转成json格式发送到服务器,这样会导致两个问题。一是不方便阅读,二是会大大浪费内存。因此,建议自定义RedisTemplate的序列化器,将key按照String方式传输,value按照json方式传输,下面是代码。

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializer;
    
    @Configuration
    public class RedisConfig {
        @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            // 创建RedisTemplate对象
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    
            // 设置RedisTemplate的连接工厂
            redisTemplate.setConnectionFactory(redisConnectionFactory);
    
            // 设置RedisTemplate的序列化器
            GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
    
            // key和hashKey的序列化器采用String
            redisTemplate.setKeySerializer(RedisSerializer.string());
            redisTemplate.setHashKeySerializer(RedisSerializer.string());
    
            // value和hashValue的序列化器采用jsonRedisSerializer
            redisTemplate.setValueSerializer(jsonRedisSerializer);
            redisTemplate.setHashValueSerializer(jsonRedisSerializer);
            return redisTemplate;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    使用StringRedisTemplate

    上述方法在传输数据时候,存在一个问题。上述方法在传输在redis数据库中时候,会将数据类型也传上去,占用了的很大的内存,如下图所示:

        @Test
        void contextLoads() {
            User u = new User("李斯", 20);
            redisTemplate.opsForValue().set("newName", u);
            System.out.println(redisTemplate.opsForValue().get("newName"));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述
    解决方案是使用StringRedisTemplate:

     @Autowired
        StringRedisTemplate stringRedisTemplate;
    
        // json工具
        static final ObjectMapper objectMapper = new ObjectMapper();
    
        @Test
        void testStringRedisTemplate() throws Exception {
            User u = new User("李斯", 20);
            // 将对象转换为json字符串
            String json = objectMapper.writeValueAsString(u);
            stringRedisTemplate.opsForValue().set("user:200", json);
    
            // 从redis中取出json字符串
            String result = stringRedisTemplate.opsForValue().get("user:200");
            // 将json字符串转换为对象
            User user = objectMapper.readValue(result, User.class);
            System.out.println(user);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

  • 相关阅读:
    操作系统伙伴算法仿真c++
    如何计算多分组交互pp值
    什么是物联网技术?
    arm汇编求最大公约数
    Medium: 9 Important Things to Remember for AB Test
    智慧园区是未来发展的趋势吗?
    深入理解Python中的Lambda(匿名函数)
    跨域访问错误的这一种解决方法
    如何把Map的value转为list
    顺序表(数据结构与算法)
  • 原文地址:https://blog.csdn.net/qq_45722630/article/details/136546888