当前位置:   article > 正文

SpringBoot Redis读写与数据序列化 RedisTemplate 与 StringRedisTemplate 防转字节

SpringBoot Redis读写与数据序列化 RedisTemplate 与 StringRedisTemplate 防转字节

介绍

RedisTemplate 对象在底层默认会转成字节,造成了内存的开销很大,这是他底层进行处理的,造成可读性差,如需要转成简单的字符串存储需要进行序列化的配置。

在这里插入图片描述

RedisTemplate

配置类

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
    {
        RedisTemplate<String,Object> template =new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer =new GenericJackson2JsonRedisSerializer();
        //序列号工具
        
        template.setKeySerializer(RedisSerializer.string());//字符串类型的Key为字符串类型
        template.setHashKeySerializer(RedisSerializer.string());//哈希类型的key也为字符串类型

        template.setValueSerializer(genericJackson2JsonRedisSerializer); //值的序列号 JSON
        template.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        return template;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

经过配置,值为转成的键值对类型
在这里插入图片描述

取值转为实体对象

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String name;
    private Integer age;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

读写序列化

@RestController
public class BasicController {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @GetMapping("/hello")
    public User  hello( ) {

        redisTemplate.opsForValue().set("user:login:123", new User("dpc",20));
        //设置值

        User user =(User)redisTemplate.opsForValue().get("user:login:123");
        //取出值

      return  user;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

返回结果
{“name”:“dpc”,“age”:20}

Redis存储结果
在这里插入图片描述
通过 “@class”: “com.example.demo.demos.web.User”,这个属性映射到实体类上,@class是JSON序列化类自动添加的,不可以去除。

当然也可以直接手动的序列化和反序列化节省内存空间。这样就不会出现改属性


StringRedisTemplate

Spring默认提供了一个StringRedisTemplate类,它的key和value的序列化方式默认就是String方式。省去了我们自定义RedisTemplate的过程,也就是说上面的的@Configuration配置类可以不用写了

字符串存取

@Autowired
    private StringRedisTemplate stringRedisTemplate;
  • 1
  • 2
 stringRedisTemplate.opsForValue().set("name","dpc");
        String str =stringRedisTemplate.opsForValue().get("name");
  • 1
  • 2

在这里插入图片描述

实体类对象串存取

@RestController
public class BasicController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static  final ObjectMapper obj=new ObjectMapper();
    //手动序列化工具

    @GetMapping("/hello")
    public User  hello( ) throws JsonProcessingException {

       User user= new User("dpc",20);
        //实体类对象
        String json =obj.writeValueAsString(user);
        //将实体类转成JSON 也就是字符串
       
        stringRedisTemplate.opsForValue().set("user:login:123", json);
        //设置值 json格式写入 也就是字符串
        
        User userMsg =obj.readValue(stringRedisTemplate.opsForValue().get("user:login:123"),User.class) ;
        //取出JSON字符串 转成java对象

      return  userMsg;
    }


}
  • 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

效果一致
在这里插入图片描述
原理是将对象转成JSON字符串,然后取出时将json字符串转成实体类就可以了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/693498
推荐阅读
相关标签
  

闽ICP备14008679号