• Redis数据序列化器


    Redis序列化

    Redis 数据序列化器用于将数据在存储到 Redis 中时进行序列化(编码)和反序列化(解码)。

    RedisTemplate的两种序列化实践方案:

    方案一:

    • 自定义RedisTemplate

    • 修改RedisTemplate的序列化器为GenericJackson2JsonRedisSerializer

    方案二:

    • 使用StringRedisTemplate

    • 写入Redis时,手动把对象序列化为JSON

    • 读取Redis时,手动把读取到的JSON反序列化为对象

    方案三“:

    • 自定义RedisTemplate

    • 修改RedisTemplate的序列化器为FastJsonRedisSerializer

    方案一

    RedisTemplate可以接收任意Object作为值写入Redis:

    1. // 写入一条String数据
    2. redisTemplate.opsForValue().set("name", "李白");
    3. // 获取string数据
    4. Object name = redisTemplate.opsForValue().get("name");
    5. System.out.println("name = " + name);

    只不过写入前会把Object序列化为字节形式,默认是采用JDK序列化,得到的结果是这样的:

    缺点:

    • 可读性差
    • 内存占用较大

    我们可以自定义RedisTemplate的序列化方式,代码如下:

    1. import org.springframework.context.annotation.Bean;
    2. import org.springframework.context.annotation.Configuration;
    3. import org.springframework.data.redis.connection.RedisConnectionFactory;
    4. import org.springframework.data.redis.core.RedisTemplate;
    5. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
    6. import org.springframework.data.redis.serializer.RedisSerializer;
    7. @Configuration
    8. public class RedisConfig {
    9. @Bean
    10. public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){
    11. // 创建RedisTemplate对象
    12. RedisTemplate template = new RedisTemplate<>();
    13. // 设置连接工厂
    14. template.setConnectionFactory(connectionFactory);
    15. // 创建JSON序列化工具
    16. GenericJackson2JsonRedisSerializer jsonRedisSerializer =
    17. new GenericJackson2JsonRedisSerializer();
    18. // 设置Key的序列化
    19. template.setKeySerializer(RedisSerializer.string());
    20. template.setHashKeySerializer(RedisSerializer.string());
    21. // 设置Value的序列化
    22. template.setValueSerializer(jsonRedisSerializer);
    23. template.setHashValueSerializer(jsonRedisSerializer);
    24. // 返回
    25. return template;
    26. }
    27. }

    这里采用了JSON序列化来代替默认的JDK序列化方式。最终结果如图:

    当我们存的数据是一个对象时:

    1. Student student = new Student("李白",28);
    2. // 写入一条String数据
    3. redisTemplate.opsForValue().set("student", student);
    4. // 获取string数据
    5. Object name = redisTemplate.opsForValue().get("student");
    6. System.out.println("name = " + name);

    能将Java对象自动的序列化为JSON字符串,并且查询时能自动把JSON反序列化为Java对象。不过,其中记录了序列化时对应的class名称,目的是为了查询时实现自动反序列化。这会带来额外的内存开销。  

    方案二 

    为了在反序列化时知道对象的类型,JSON序列化器会将类的class类型写入json结果中,存入Redis,会带来额外的内存开销。为了减少内存的消耗,我们可以采用手动序列化的方式,换句话说,就是不借助默认的序列化器,而是我们自己来控制序列化的动作,同时,我们只采用String的序列化器,这样,在存储value时,我们就不需要在内存中就不用多存储数据,从而节约我们的内存空间

    1. private static final ObjectMapper mapper = new ObjectMapper();
    2. @Resource
    3. private StringRedisTemplate stringRedisTemplate;
    4. @Test
    5. void testSaveUser() throws JsonProcessingException {
    6. // 创建对象
    7. Student student = new Student("李白",28);
    8. // 手动序列化
    9. String json = mapper.writeValueAsString(student);
    10. // 写入数据
    11. stringRedisTemplate.opsForValue().set("student", json);
    12. // 获取数据
    13. String jsonUser = stringRedisTemplate.opsForValue().get("user:200");
    14. // 手动反序列化
    15. Student user1 = mapper.readValue(jsonUser, Student.class);
    16. System.out.println("user1 = " + user1);
    17. }

    此时我们再来看一看存储的数据,小伙伴们就会发现那个class数据已经不在了,节约了我们的空间~  

    方案三

    方案二每次的手动序列化十分麻烦,我们可以指定序列化器为FastJsonRedisSerializer

    1. import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.data.redis.connection.RedisConnectionFactory;
    5. import org.springframework.data.redis.core.RedisTemplate;
    6. import org.springframework.data.redis.serializer.StringRedisSerializer;
    7. @Configuration
    8. public class RedisConfig {
    9. @Bean
    10. @SuppressWarnings(value = { "unchecked", "rawtypes" })
    11. public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory)
    12. {
    13. RedisTemplate template = new RedisTemplate<>();
    14. template.setConnectionFactory(connectionFactory);
    15. // 创建JSON序列化工具
    16. FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class);
    17. // 使用StringRedisSerializer来序列化和反序列化redis的key值
    18. template.setKeySerializer(new StringRedisSerializer());
    19. template.setValueSerializer(serializer);
    20. // Hash的key也采用StringRedisSerializer的序列化方式
    21. template.setHashKeySerializer(new StringRedisSerializer());
    22. template.setHashValueSerializer(serializer);
    23. template.afterPropertiesSet();
    24. return template;
    25. }
    26. }

    测试

    1. Student student = new Student("厉害", 22);
    2. // 写入一条String数据
    3. redisTemplate.opsForValue().set("student", student);
    4. // 获取string数据
    5. Object name = redisTemplate.opsForValue().get("student");
    6. System.out.println("student = " + name);

    结果 

     

  • 相关阅读:
    jquery-ajax
    社区团购模式系统的优势与解决方案
    Nacos在Windows本地安装并启动教程
    Blender之锁定摄像机到视图方位
    day06-前后端项目上传到gitee、后端多方式登录接口、发送短信功能、发送短信封装、短信验证码接口、短信登录接口
    Apache Doris 系列: 入门篇-创建数据表
    Shell 常用操作指令
    【持续跟更新】随笔记录
    弱口令破解工具--超级弱口令工具/hydra
    神经网络训练需要联网吗,神经网络训练电脑配置
  • 原文地址:https://blog.csdn.net/qq_63431773/article/details/133844236