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

鍵值對(duì)都是亂碼形式。
解決以上問(wè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 項(xiàng)目配置RedisConfig的時(shí)候使用以下方法:
@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); //解決日期序列化問(wèn)題
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存入中文,取出來(lái)是亂碼wenti
默認(rèn)情況下,用redis存入中文,取出時(shí)會(huì)出現(xiàn)亂碼情況,如圖:

解決
我們?cè)賳?dòng)redis時(shí),可以在redis-cli 后面加上 --raw,如圖

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Redis緩存預(yù)熱的實(shí)現(xiàn)方法
緩存預(yù)熱是一種在程序啟動(dòng)或緩存失效之后,主動(dòng)將熱點(diǎn)數(shù)據(jù)加載到緩存中的策略,本文將給大家分享一下如何實(shí)現(xiàn)Redis的緩存預(yù)熱,文中有詳細(xì)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-10-10
Spring+Redis+RabbitMQ開發(fā)限流和秒殺項(xiàng)目功能
本項(xiàng)目將通過(guò)整合Springboot和Redis以及Lua腳本來(lái)實(shí)現(xiàn)限流和秒殺的效果,將通過(guò)RabbitMQ消息隊(duì)列來(lái)實(shí)現(xiàn)異步保存秒殺結(jié)果的效果,對(duì)Spring?Redis?RabbitMQ限流秒殺功能實(shí)現(xiàn)感興趣的朋友一起看看吧2022-02-02
Redis實(shí)現(xiàn)分布式事務(wù)的示例
Redis雖不支持傳統(tǒng)SQL數(shù)據(jù)庫(kù)ACID特性的事務(wù),但提供了事務(wù)特性,允許多命令捆綁執(zhí)行,通過(guò)命令MULTI、EXEC、DISCARD、WATCH實(shí)現(xiàn),感興趣的可以了解一下2024-10-10
redis cluster支持pipeline的實(shí)現(xiàn)思路
本文給大家介紹redis cluster支持pipeline的實(shí)現(xiàn)思路,在 cluster 上執(zhí)行 pipeline 可能會(huì)由于 redis 節(jié)點(diǎn)擴(kuò)縮容 中途 redirection 切換連接導(dǎo)致結(jié)果丟失,具體細(xì)節(jié)問(wèn)題請(qǐng)參考下本文2021-06-06
Redis數(shù)據(jù)庫(kù)的應(yīng)用場(chǎng)景介紹
這篇文章主要介紹了Redis數(shù)據(jù)庫(kù)的應(yīng)用場(chǎng)景介紹,本文講解了MySql+Memcached架構(gòu)的問(wèn)題、Redis常用數(shù)據(jù)類型、Redis數(shù)據(jù)類型應(yīng)用和實(shí)現(xiàn)方式、Redis實(shí)際應(yīng)用場(chǎng)景等內(nèi)容,需要的朋友可以參考下2015-06-06
Redis 出現(xiàn)錯(cuò)誤1067的解決辦法
這篇文章主要介紹了Redis 出現(xiàn)錯(cuò)誤1067的解決辦法的相關(guān)資料,Redis 錯(cuò)誤1067:進(jìn)程意外終止,Redis不能啟動(dòng),Redis啟動(dòng)不了,需要的朋友可以參考下2017-07-07

