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 項(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); //解決日期序列化問題
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
默認(rèn)情況下,用redis存入中文,取出時(shí)會出現(xiàn)亂碼情況,如圖:

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

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

