阅读 737

Redis缓存-序列化对象存储乱码问题的解决

Redis缓存-序列化对象存储乱码问题的解决

这篇文章主要介绍了Redis缓存-序列化对象存储乱码问题的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

使用Redis缓存对象会出现下图现象:

键值对都是乱码形式。

解决以上问题:

如果是xml配置的

我们直接注入官方给定的keySerializer,valueSerializer,hashKeySerializer即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<bean id="apiRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
        p:connection-factory-ref="apiCacheRedisConnectionFactory">
        <property name="keySerializer">
            <bean
                class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean
                class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
        </property>
 
        <property name="hashKeySerializer">
            <bean
                class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
        </property>
        <property name="hashValueSerializer">
            <bean
                class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
        </property>
        <property name="stringSerializer">
            <bean
                class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
    </bean>

spring boot 项目配置RedisConfig的时候使用以下方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Configuration
public class RedisConfig {
    @Bean("jsonRedisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
        template.setConnectionFactory(redisConnectionFactory);      //解决日期序列化问题
        ObjectMapper om = new ObjectMapper();
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(om);
        template.setDefaultSerializer(genericJackson2JsonRedisSerializer);
        return template;
 
    }
}

Redis存入中文,取出来是乱码wenti

默认情况下,用redis存入中文,取出时会出现乱码情况,如图:

解决

我们再启动redis时,可以在redis-cli 后面加上 --raw,如图

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

  • 解决RedisTemplate存储至缓存数据出现乱码的情况

  • redis 解决key的乱码问题,并清理详解

  • Redis存取序列化与反序列化性能问题详解

  • 详解Redis 分布式锁遇到的序列化问题


原文链接:https://www.cnblogs.com/zhuzhen/p/8125793.html


文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐