关于前端:springboot集成Cacheable缓存乱码的问题解决方案

27次阅读

共计 3300 个字符,预计需要花费 9 分钟才能阅读完成。

本文目录

一、问题及景象

会把被标注的办法的返回值缓存到 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);
    }
}

完结!

欢送关注我的公众号:敲代码的老贾,回复“支付”赠送《Java 面试》材料,阿里,腾讯,字节,美团,饿了么等大厂

正文完
 0