• SpringDataRedis的序列化方式(二)


    概述

    示例分析

    @Test
        public void testSpring() {
            // 插入一条string类型数据
            redisTemplate.opsForValue().set("name", "李四");
            // 读取一条string类型数据
            final Object name = redisTemplate.opsForValue().get("name");
            log.info("name:{}", name);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    可以看到我们对key为name设置了值“李四”。
    但是通过redis客户端查询name值为null。

    > get name
    null
    
    • 1
    • 2

    在客户端上我们发现实际写入的key如下,键和值都是看不懂的数据。
    在这里插入图片描述
    原因是什么呢?
    原因是默认的keySerializer和valueSerializer是jdk序列化器。
    RedisTemplate的源码中如下:

    public void afterPropertiesSet() {
            super.afterPropertiesSet();
            boolean defaultUsed = false;
            if (this.defaultSerializer == null) {
                this.defaultSerializer = new JdkSerializationRedisSerializer(this.classLoader != null ? this.classLoader : this.getClass().getClassLoader());
            }
    
            if (this.enableDefaultSerializer) {
                if (this.keySerializer == null) {
                    this.keySerializer = this.defaultSerializer;
                    defaultUsed = true;
                }
    
                if (this.valueSerializer == null) {
                    this.valueSerializer = this.defaultSerializer;
                    defaultUsed = true;
                }
    
                if (this.hashKeySerializer == null) {
                    this.hashKeySerializer = this.defaultSerializer;
                    defaultUsed = true;
                }
    
                if (this.hashValueSerializer == null) {
                    this.hashValueSerializer = this.defaultSerializer;
                    defaultUsed = true;
                }
            }
    
            if (this.enableDefaultSerializer && defaultUsed) {
                Assert.notNull(this.defaultSerializer, "default serializer null and not all serializers initialized");
            }
    
            if (this.scriptExecutor == null) {
                this.scriptExecutor = new DefaultScriptExecutor(this);
            }
    
            this.initialized = true;
        }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    设置redis序列化器

    package com.study.redis.redisstudy.config;
    
    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;
    
    /**
     * @Description :
     * @Version : V1.0.0
     * @Date : 2022/8/9 22:09
     */
    @Configuration
    public class RedisConfig {
    
        @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
            // 创建RedisTemplate对象
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            // 设置连接工厂
            template.setConnectionFactory(connectionFactory);
            // 创建JSON序列化工具
            GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
            // 设置key的序列化
            template.setKeySerializer(RedisSerializer.string());
            template.setHashKeySerializer(RedisSerializer.string());
            // 设置value的序列化
            template.setValueSerializer(jsonRedisSerializer);
            template.setHashValueSerializer(jsonRedisSerializer);
            return template;
        }
    }
    
    • 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
    • 32
    • 33
    • 34

    使用StringRedisTemplate

    尽管JSON的序列化方式可以满足我们的需求,但依然存在以下问题。
    会额外存放类型的信息,占用内存空间。
    在这里插入图片描述
    为了在反序列时知道对象的类型,JSON序列化器会将class类型写入json结果中,存入Redis,会带来额外的内存开销。
    为了节省内存空间,我们并不会使用JSON序列化器来处理value,而是统一使用String序列化器 ,要求只能存储String类型的key和value。当需要存储Java对象时,收到完成对象的序列化和反序列化。
    spring默认提供了一个StringRedisTemplate类,它的key和value的序列化方式默认就是String方式。省去我们自定义RedisTemplate的过程。
    在这里插入图片描述

    总结

    在这里插入图片描述

  • 相关阅读:
    Linux企业应用——Docker(一)之初步了解Docker以及Docker的安装
    git基本手册
    早在多久MySQL 啥时候用表锁,啥时候用行锁?
    在标准的C++ 语法中,请问有 MyCppClass*& mycppclass 这样的变量定义方式吗?
    开发者,MySQL专栏完更,助你轻松从安装到入门进阶
    国民技术 N32G45x 串口踩坑
    vue2升级vue3指南(二)—— 语法warning&error篇
    DSPE-PEG-Hydrazide DSPE-PEG-HZ 磷脂-聚乙二醇-酰肼科研用化学试剂
    最「难搞」的英伟达也开源了,下一个会是谁?
    RabbitMQ 学习(七)-- 高级发布确认
  • 原文地址:https://blog.csdn.net/tianzhonghaoqing/article/details/126256522