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

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

二、原因分析
其實(shí)出現(xiàn)上述亂碼,一般情況都是沒有配置 redis 序列化值導(dǎo)致的,而源碼里的配置又沒有默認(rèn),需要自己去實(shí)現(xiàn)。
在網(wǎng)上有很多種寫法,我搜索了很多都不適合自己,只有下面這一種可以正常。
三、解決方案
添加一個 Redis 的配置類即可。如下代碼是我在項(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 此類用于: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è)備基本信息遠(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ù)時,根據(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)文章
Java多線程實(shí)現(xiàn)之Executor詳解
這篇文章主要介紹了Java多線程實(shí)現(xiàn)之Executor詳解,Executor 給他一個 Runnable,他就能自動很安全的幫你把這個線程執(zhí)行完畢2023-08-08
Executor 通過創(chuàng)建線程池的方式來管理線程,需要的朋友可以參考下
Java編程經(jīng)典小游戲設(shè)計(jì)-打磚塊小游戲源碼
這篇文章主要介紹了Java編程經(jīng)典小游戲設(shè)計(jì)-打磚塊小游戲源碼,還是挺不錯的,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Java精品項(xiàng)目瑞吉外賣之登陸的完善與退出功能篇
這篇文章主要為大家詳細(xì)介紹了java精品項(xiàng)目-瑞吉外賣訂餐系統(tǒng),此項(xiàng)目過大,分為多章獨(dú)立講解,本篇內(nèi)容為新增菜品和分頁查詢功能的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
JavaWeb使用mvc模式實(shí)現(xiàn)登錄功能
本文主要介紹了JavaWeb使用mvc模式實(shí)現(xiàn)登錄功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
使用java?實(shí)現(xiàn)mqtt兩種常用方式
在開發(fā)MQTT時有兩種方式一種是使用Paho Java 原生庫來完成,一種是使用spring boot 來完成,這篇文章主要介紹了使用java?實(shí)現(xiàn)mqtt兩種方式,需要的朋友可以參考下2022-11-11

