• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

redis配置序列化

武飞扬头像
无敌牛牛
帮助1

通过上面我想你已经知道了凡是需要进行“跨平台存储”和”网络传输”的数据,都需要进行序列化。

本质上存储和网络传输 都需要经过 把一个对象状态保存成一种跨平台识别的字节格式,然后其他的平台才可以通过字节信息解析还原对象信息。

redis序列化方式对比:

redis的默认方式是JdkSerializationRedisSerializer

JdkSerializationRedisSerializer: 使用JDK提供的序列化功能。

优点是反序列化时不需要提供类型信息(class),但缺点是需要实现Serializable接口,还有序列化后的结果非常庞大,是JSON格式的5倍左右,这样就会消耗redis服务器的大量内存。

Jackson2JsonRedisSerializer: 使用Jackson库将对象序列化为JSON字符串。

优点是速度快,序列化后的字符串短小精悍,不需要实现Serializable接口。

但缺点也非常致命,那就是此类的构造函数中有一个类型参数,必须提供要序列化对象的类型信息(.class对象)。 通过查看源代码,发现其只在反序列化过程中用到了类型信息。

问题:使用默认的JDK序列化方式,在RDM工具中查看k-v值时会出现“乱码”,不方便查看。

解决:自定义系列化方式,使用Jackson2JsonRedisSerializer

  1.  
    @Configuration
  2.  
    public class RedisConfig extends CachingConfigurerSupport {
  3.  
     
  4.  
    private static final String STANDARD_PATTERN = "yyyy/MM/dd HH:mm:ss";
  5.  
    private static final String DATE_PATTERN = "yyyy/MM/dd";
  6.  
    private static final String TIME_PATTERN = "HH:mm:ss";
  7.  
     
  8.  
    @Bean
  9.  
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
  10.  
    RedisTemplate<String, Object> template = new RedisTemplate<>();
  11.  
    // 使用Jackson2JsonRedisSerialize 替换默认序列化
  12.  
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =
  13.  
    new Jackson2JsonRedisSerializer<>(Object.class);
  14.  
     
  15.  
    ObjectMapper objectMapper = new ObjectMapper();
  16.  
    objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(),
  17.  
    ObjectMapper.DefaultTyping.NON_FINAL,
  18.  
    JsonTypeInfo.As.PROPERTY);
  19.  
    objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
  20.  
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  21.  
    this.registerLocalDateTime(objectMapper);
  22.  
    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
  23.  
     
  24.  
    template.setConnectionFactory(connectionFactory);
  25.  
    // key采用String的序列化方式
  26.  
    template.setKeySerializer(new StringRedisSerializer());
  27.  
    // value序列化方式采用jackson
  28.  
    template.setValueSerializer(jackson2JsonRedisSerializer);
  29.  
    // hash的key也采用String的序列化方式
  30.  
    template.setHashKeySerializer(new StringRedisSerializer());
  31.  
    // hash的value序列化方式采用jackson
  32.  
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
  33.  
    template.afterPropertiesSet();
  34.  
    return template;
  35.  
    }
  36.  
     
  37.  
    /**
  38.  
    * 处理时间类型
  39.  
    *
  40.  
    * @param objectMapper
  41.  
    */
  42.  
    private void registerLocalDateTime(ObjectMapper objectMapper) {
  43.  
    // 设置java.util.Date时间类的序列化以及反序列化的格式
  44.  
    objectMapper.setDateFormat(new SimpleDateFormat(STANDARD_PATTERN));
  45.  
     
  46.  
    JavaTimeModule timeModule = new JavaTimeModule();
  47.  
    // LocalDateTime
  48.  
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(STANDARD_PATTERN);
  49.  
    timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
  50.  
    timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
  51.  
    // LocalDate
  52.  
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
  53.  
    timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter));
  54.  
    timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter));
  55.  
    // LocalTime
  56.  
    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(TIME_PATTERN);
  57.  
    timeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(timeFormatter));
  58.  
    timeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(timeFormatter));
  59.  
    objectMapper.registerModule(timeModule);
  60.  
     
  61.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgjfeah
系列文章
更多 icon
同类精品
更多 icon
继续加载