springboot~RedisTemplate三种序列化方式

2022/9/17 2:17:11

本文主要是介绍springboot~RedisTemplate三种序列化方式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

RedisTemplate默认使用二进制hex的方式进行持久化,它对象的值存储到redis里,当然,你的对象需要先实现java.io.Serializable接口,这种方式兼容性好,速度快,存储空间小;缺点就是没有可读性,所以就有了json的序列化方式,下面总结一下。

一 JdkSerializationRedisSerializer二进制流的方式

@Test
    public void hex() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        //二进制序列化,实体类需要实现Serializable接口
        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
        //序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        template.setValueSerializer(jdkSerializationRedisSerializer);
        template.setHashValueSerializer(jdkSerializationRedisSerializer);
        template.afterPropertiesSet();

        extracted(template, "redis_test111");
        User user = (User) template.opsForValue().get("redis_test111");
    }

二 Jackson2JsonRedisSerializer纯json字符串

 @Test
    public void jsonStr() throws IOException {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =
                new Jackson2JsonRedisSerializer<>(Object.class);
        //序列化
        jackson2JsonRedisSerializer.setObjectMapper(new ObjectMapper());
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        extracted(template, "redis_test11");
        JsonParser jsonParser = mapper.getFactory().createParser(mapper.writeValueAsBytes(template.opsForValue().get("redis_tesqt")));
        User jsonNode = jsonParser.readValueAs(User.class);
        log.info("user={}", jsonNode);
    }

三 Jackson2JsonRedisSerializer带有java类型的方式

RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 日期序列化处理
        om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        om.registerModule(new Jdk8Module())
                .registerModule(new JavaTimeModule())
                .registerModule(new ParameterNamesModule());

        //存储java的类型,方便反序列化,没有这行,将存储为纯json字符串
        om.activateDefaultTyping(
                LaissezFaireSubTypeValidator.instance,
                ObjectMapper.DefaultTyping.NON_FINAL,
                JsonTypeInfo.As.WRAPPER_ARRAY);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        extracted(template, "redis_test1");
        User user = (User) template.opsForValue().get("redis_test");
        log.info("user={}", user);

综上所述,我认为第三种json对象的方式是比较好的处理方式,在序列化和反序列化,及可读性上都表现不错。



这篇关于springboot~RedisTemplate三种序列化方式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程