Redis緩存-序列化對象存儲亂碼問題的解決
使用Redis緩存對象會出現(xiàn)下圖現(xiàn)象:

鍵值對都是亂碼形式。
解決以上問題:
如果是xml配置的
我們直接注入官方給定的keySerializer,valueSerializer,hashKeySerializer即可:
<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的時候使用以下方法:
@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存入中文,取出時會出現(xiàn)亂碼情況,如圖:

解決
我們再啟動redis時,可以在redis-cli 后面加上 --raw,如圖

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
基于Redis6.2.6版本部署Redis?Cluster集群的問題
這篇文章主要介紹了基于Redis6.2.6版本部署Redis?Cluster集群,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
redis中opsForList().range()的使用方法詳解
這篇文章主要給大家介紹了關于redis中opsForList().range()的使用方法,文中通過實例代碼以及圖文介紹的非常詳細,對大家學習或者使用redis具有一定的參考學習價值,需要的朋友可以參考下2023-03-03
CentOS系統(tǒng)中Redis數(shù)據(jù)庫的安裝配置指南
Redis是一個基于主存存儲的數(shù)據(jù)庫,性能很強,這里我們就來看一下CentOS系統(tǒng)中Redis數(shù)據(jù)庫的安裝配置指南,包括將Redis作為系統(tǒng)服務運行的技巧等,需要的朋友可以參考下2016-06-06
Redis使用RedisTemplate導致key亂碼問題解決
本文主要介紹了Redis使用RedisTemplate導致key亂碼問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-06-06
Spring?Boot?整合Redis?實現(xiàn)優(yōu)惠卷秒殺?一人一單功能
這篇文章主要介紹了Spring?Boot?整合Redis?實現(xiàn)優(yōu)惠卷秒殺?一人一單,在分布式系統(tǒng)下,高并發(fā)的場景下,會出現(xiàn)此類庫存超賣問題,本篇文章介紹了采用樂觀鎖來解決,需要的朋友可以參考下2022-09-09

