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

修改過后,可以正常顯示,如下圖:

二、原因分析
其實出現(xiàn)上述亂碼,一般情況都是沒有配置 redis 序列化值導(dǎo)致的,而源碼里的配置又沒有默認,需要自己去實現(xiàn)。
在網(wǎng)上有很多種寫法,我搜索了很多都不適合自己,只有下面這一種可以正常。
三、解決方案
添加一個 Redis 的配置類即可。如下代碼是我在項目中的代碼,加上重啟項目 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 此類用于:Redis相關(guān)配置,用于解決存入Redis中值亂碼問題 </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 注解的類
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 此類用于:設(shè)備基本信息遠程調(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ù)時,根據(jù)sn和deviceId獲取設(shè)備基礎(chǔ)信息失??!");
return null;
}
DeviceBasicInfoResponse deviceBasicInfoResponse = deviceBasicInfoCommResponse.getData();
return DeviceBasicInfoConvert.INSTANCE.convert2DeviceBasicInfoDTO(deviceBasicInfoResponse);
}
}
到此這篇關(guān)于springboot集成@Cacheable緩存亂碼的問題解決的文章就介紹到這了,更多相關(guān)springboot集成@Cacheable緩存亂碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
swagger的請求參數(shù)不顯示,@Apimodel的坑點及解決
這篇文章主要介紹了swagger的請求參數(shù)不顯示,@Apimodel的坑點及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
java編譯后的文件出現(xiàn)xx$1.class的原因及解決方式
這篇文章主要介紹了java編譯后的文件出現(xiàn)xx$1.class的原因及解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Java SpringBoot+vue+實戰(zhàn)項目詳解
這篇文章主要介紹了SpringBoot+VUE實現(xiàn)前后端分離的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2021-09-09
Java使用位運算實現(xiàn)權(quán)限管理的示例詳解
在開發(fā)中,權(quán)限管理是一個非常常見的需求,本文將詳細講解如何使用 Java 中的 位運算 實現(xiàn)一個輕量級、高效的權(quán)限管理系統(tǒng),并提供完整的代碼示例和解釋,感興趣的小伙伴可以了解下2025-06-06

