当前位置: 代码网 > it编程>编程语言>Java > springboot集成@Cacheable缓存乱码的问题解决

springboot集成@Cacheable缓存乱码的问题解决

2025年10月27日 Java 我要评论
一、问题及现象会把被标注的方法的返回值缓存到 redis 中,相同的操作不会查数据库而是从缓存中获取数据。springboot 集成 redis,使用 @cacheable 注解之后,把数据缓存到 r

一、问题及现象

会把被标注的方法的返回值缓存到 redis 中,相同的操作不会查数据库而是从缓存中获取数据。

springboot 集成 redis,使用 @cacheable 注解之后,把数据缓存到 redis 中,数据是保存在redis 中了,但是,通过 redis 的可视化管理工具查看缓存的数据时,却发现 redis 中的 key 正常,但是 value 是乱码。如下图所示的乱码:

修改过后,可以正常显示,如下图:

二、原因分析

其实出现上述乱码,一般情况都是没有配置 redis 序列化值导致的,而源码里的配置又没有默认,需要自己去实现。

在网上有很多种写法,我搜索了很多都不适合自己,只有下面这一种可以正常。

三、解决方案

添加一个 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相关配置,用于解决存入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 此类用于:设备基本信息远程调用 </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|调用设备云服务时,根据sn和deviceid获取设备基础信息失败!");
            return null;
        }

        devicebasicinforesponse devicebasicinforesponse = devicebasicinfocommresponse.getdata();

        return devicebasicinfoconvert.instance.convert2devicebasicinfodto(devicebasicinforesponse);
    }
}

到此这篇关于springboot集成@cacheable缓存乱码的问题解决的文章就介绍到这了,更多相关springboot集成@cacheable缓存乱码内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com