springboot集成@Cacheable緩存亂碼的問(wèn)題解決
一、問(wèn)題及現(xiàn)象
會(huì)把被標(biāo)注的方法的返回值緩存到 Redis 中,相同的操作不會(huì)查數(shù)據(jù)庫(kù)而是從緩存中獲取數(shù)據(jù)。
Springboot 集成 Redis,使用 @Cacheable 注解之后,把數(shù)據(jù)緩存到 Redis 中,數(shù)據(jù)是保存在Redis 中了,但是,通過(guò) Redis 的可視化管理工具查看緩存的數(shù)據(jù)時(shí),卻發(fā)現(xiàn) redis 中的 key 正常,但是 value 是亂碼。如下圖所示的亂碼:

修改過(guò)后,可以正常顯示,如下圖:

二、原因分析
其實(shí)出現(xiàn)上述亂碼,一般情況都是沒(méi)有配置 redis 序列化值導(dǎo)致的,而源碼里的配置又沒(méi)有默認(rèn),需要自己去實(shí)現(xiàn)。
在網(wǎng)上有很多種寫(xiě)法,我搜索了很多都不適合自己,只有下面這一種可以正常。
三、解決方案
添加一個(gè) Redis 的配置類(lèi)即可。如下代碼是我在項(xiàng)目中的代碼,加上重啟項(xiàng)目 Redis 緩存中的 value 即可正常顯示。
package com.iot.back.message.process.config;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
/**
* <p>RedisConfig 此類(lèi)用于:Redis相關(guān)配置,用于解決存入Redis中值亂碼問(wèn)題 </p>
* <p>@author:hujm</p>
* <p>@date:2022年08月18日 18:04</p>
* <p>@remark:</p>
*/
@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
// 序列化值
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair
.fromSerializer(new GenericJackson2JsonRedisSerializer()));
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
return config;
}
}
使用 @Cacheable 注解的類(lèi)
package com.iot.back.message.process.rpc;
import com.iot.back.message.process.convert.DeviceBasicInfoConvert;
import com.iot.back.message.process.dto.DeviceBasicInfoDTO;
import com.iot.basic.iotsmarthome.api.client.device.DeviceCloudClient;
import com.iot.basic.iotsmarthome.api.response.device.DeviceBasicInfoResponse;
import com.iot.framework.core.response.CommResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* <p>DeviceBasicInfoRpc 此類(lèi)用于:設(shè)備基本信息遠(yuǎn)程調(diào)用 </p>
* <p>@author:hujm</p>
* <p>@date:2022年05月23日 15:07</p>
* <p>@remark:</p>
*/
@Slf4j
@Component
public class DeviceBasicInfoRpc {
@Resource
private DeviceCloudClient deviceCloudClient;
@Cacheable(cacheNames = "back-process-service:cache", key = "#sn+':'+#deviceId", sync = true)
public DeviceBasicInfoDTO getDeviceBasicInfo(String sn, Integer deviceId) {
CommResponse<DeviceBasicInfoResponse> deviceBasicInfoCommResponse = deviceCloudClient.getDeviceBasicInfo(sn, deviceId);
if (deviceBasicInfoCommResponse == null || deviceBasicInfoCommResponse.isFail()) {
log.error("P0|DeviceBasicInfoRpc|getDeviceBasicInfo|調(diào)用設(shè)備云服務(wù)時(shí),根據(jù)sn和deviceId獲取設(shè)備基礎(chǔ)信息失敗!");
return null;
}
DeviceBasicInfoResponse deviceBasicInfoResponse = deviceBasicInfoCommResponse.getData();
return DeviceBasicInfoConvert.INSTANCE.convert2DeviceBasicInfoDTO(deviceBasicInfoResponse);
}
}
到此這篇關(guān)于springboot集成@Cacheable緩存亂碼的問(wèn)題解決的文章就介紹到這了,更多相關(guān)springboot集成@Cacheable緩存亂碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java多線程實(shí)現(xiàn)之Executor詳解
這篇文章主要介紹了Java多線程實(shí)現(xiàn)之Executor詳解,Executor 給他一個(gè) Runnable,他就能自動(dòng)很安全的幫你把這個(gè)線程執(zhí)行完畢2023-08-08
Executor 通過(guò)創(chuàng)建線程池的方式來(lái)管理線程,需要的朋友可以參考下
Java編程經(jīng)典小游戲設(shè)計(jì)-打磚塊小游戲源碼
這篇文章主要介紹了Java編程經(jīng)典小游戲設(shè)計(jì)-打磚塊小游戲源碼,還是挺不錯(cuò)的,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Feign遠(yuǎn)程調(diào)用丟失請(qǐng)求頭問(wèn)題
本文介紹了在服務(wù)端項(xiàng)目中如何解決資源訪問(wèn)限制問(wèn)題,首先介紹了問(wèn)題的產(chǎn)生,然后詳細(xì)解析了源碼,最后提出了解決方案,解決方案包括同步和異步兩種,同步時(shí)直接向Spring容器注入RequestInterceptor攔截器2024-09-09
Java精品項(xiàng)目瑞吉外賣(mài)之登陸的完善與退出功能篇
這篇文章主要為大家詳細(xì)介紹了java精品項(xiàng)目-瑞吉外賣(mài)訂餐系統(tǒng),此項(xiàng)目過(guò)大,分為多章獨(dú)立講解,本篇內(nèi)容為新增菜品和分頁(yè)查詢功能的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
JavaWeb使用mvc模式實(shí)現(xiàn)登錄功能
本文主要介紹了JavaWeb使用mvc模式實(shí)現(xiàn)登錄功能,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
使用java?實(shí)現(xiàn)mqtt兩種常用方式
在開(kāi)發(fā)MQTT時(shí)有兩種方式一種是使用Paho Java 原生庫(kù)來(lái)完成,一種是使用spring boot 來(lái)完成,這篇文章主要介紹了使用java?實(shí)現(xiàn)mqtt兩種方式,需要的朋友可以參考下2022-11-11

