当前位置: 首页 > Web前端 > HTML5

springboot集成@Cacheable解决乱码问题

时间:2023-04-05 01:03:27 HTML5

本文内容一、问题及现象被标记的方法的返回值会缓存在Redis中。同样的操作不会去查数据库而是从缓存中获取数据。Springboot集成了Redis。使用@Cacheable注解后,数据缓存在Redis中。数据存储在Redis中。但是通过Redis的可视化管理工具查看缓存数据时发现redis中的key是正常的,但是value是乱码。乱码如下图:修改后可以正常显示,如下图:2.原因分析其实上述乱码一般都是没有配置redis序列化值造成的,并且源码中没有默认配置,需要自己实现。网上有很多写法,我也搜索了很多,都没有适合自己的。只有下面一个可以正常。3.解决办法只需要增加一个Redis配置类即可。以下代码是我在项目中的代码,重启项目后可以正常显示Redis缓存中的值。包com.iot.back.message.process.config;导入org.springframework.boot.autoconfigure.cache.CacheProperties;导入org.springframework.cache.annotation.CachingConfigurerSupport;导入org.springframework.cache.annotation.EnableCaching;导入org.springframework.context.annotation.Bean;导入org.springframework.context.annotation.Configuration;导入org.springframework.data.redis.cache.RedisCacheConfiguration;导入org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;导入org.springframework.data.redis.serializer.RedisSerializationContext;/***

RedisConfig此类用于:Redis相关配置,用于解决存入Redis中值得乱码问题

*

@author:hujm

*

@date:2022年08月18日18:04

*

@remark:

*/@EnableCaching@ConfigurationpublicclassRedisConfigextendsCachingConfigurerSupport{@BeanpublicRedisCacheConfigurationredisCacheConfiguration(CachePropertiescacheProperties){缓存eProperties.RedisredisProperties=cacheProperties.getRedis();RedisCacheConfigurationconfig=RedisCacheConfiguration.defaultCacheConfig();//序列化值config=config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(newGenericJackson2JsonRedisSerializer()));如果(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();}返回配置;}}使用@Cacheable注解的类包com.iot.back.message.process.rpc;importcom.iot.back.message.process.convert.DeviceBasicInfo转换;导入com.iot.back.message.process.dto.DeviceBasicInfoDTO;导入com.iot.basic.iotsmarthome.api.client.device.DeviceCloudClient;导入com.iot.basic.iotsmarthome.api.response.device.DeviceBasicInfoResponse;导入com.iot.framework.core.response.CommResponse;导入lombok.extern.slf4j.Slf4j;导入org.springframework.cache.annotation.Cacheable;导入org.springframework.stereotype.Component;导入javax.annotation.Resource;/***

DeviceBasicInfoRpc这种类型用于:设备基础信息远程程序调用

*

@author:hujm

*

@date:2022年05月23日15:07

*

@remark:

*/@Slf4j@ComponentpublicclassDeviceBasicInfoRpc{@ResourceprivateDeviceCloudClientdeviceCloudClient;@Cacheable(cacheNames="back-process-service:cache",key="#sn+':'+#deviceId",sync=true)publicDeviceBasicInfoDTOgetDeviceBasicInfo(Stringsn,IntegerdeviceId){CommResponsedeviceBasicInfoCommResponse=deviceCloudClient.getDeviceBasicInfo(sn,deviceId);if(deviceBasicInfoCommResponse==null||DeviceBasicInformationfailed!");returnnull;}DeviceBasicInfoResponsedeviceBasicInfoResponse=deviceBasicInfoCommResponse.getData();returnDeviceBasicInfoConvert.INSTANCE.convert2DeviceBasicInfoDTO(deviceBasicInfoResponse);}}完成!欢迎关注我的公众号:敲代码的老贾,回复“领取”,送《Java面试》信息,阿里、腾讯、字节、美团、饿了么等大厂