RedisTemplate集成fastjson

Author Avatar
丁起男 08月 08,2021
  • 在其它设备中阅读本文章

RedisTemplate集成fastjson

    @Bean
    @ConditionalOnClass(RedisOperations.class)
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        //设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //使用fastjson
        FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class);
        //设置序列化方式
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //key采用string的序列化方式
        redisTemplate.setKeySerializer(stringRedisSerializer);
        //hash的key采用string的序列化方式
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        //value采用 fastjson
        redisTemplate.setValueSerializer(serializer);
        //hash的value采用 fastjson
        redisTemplate.setHashValueSerializer(serializer);

        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }