解决redis-cli乱码问题

    科技2024-12-11  14

    @Configuration @EnableCaching public class RedisConfig { @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory){ RedisTemplate<String, String> redisTemplate = new RedisTemplate<String,String>(); redisTemplate.setConnectionFactory(factory); // 使用Jackson2JsonRedisSerialize 替换默认序列化 /**Jackson序列化 json占用的内存最小 */ Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); /**Jdk序列化 JdkSerializationRedisSerializer是最高效的*/ // JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer(); /**String序列化*/ StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); /**将key value 进行stringRedisSerializer序列化*/ redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(stringRedisSerializer); /**将HashKey HashValue 进行序列化*/ redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } }

    使用Jackson2JsonRedisSerializer这个类就可以啦

    Processed: 0.037, SQL: 8