最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot使用@Cacheable出現(xiàn)預(yù)覽工具亂碼的解決方法

 更新時(shí)間:2023年10月06日 09:14:38   作者:逆水行舟x  
直接使用注解進(jìn)行緩存數(shù)據(jù),我們?cè)偈褂霉ぞ呷ヮA(yù)覽存儲(chǔ)的數(shù)據(jù)時(shí)發(fā)現(xiàn)是亂碼,這是由于默認(rèn)序列化的問(wèn)題,所以接下來(lái)將給大家介紹一下SpringBoot使用@Cacheable出現(xiàn)預(yù)覽工具亂碼的解決方法,需要的朋友可以參考下

直接使用注解進(jìn)行緩存數(shù)據(jù),我們?cè)偈褂霉ぞ呷ヮA(yù)覽存儲(chǔ)的數(shù)據(jù)時(shí)發(fā)現(xiàn)是亂碼,這是由于默認(rèn)序列化的問(wèn)題,默認(rèn)是使用JdkSerializationRedisSerializer,我們將其更改即可

解決辦法

我們重新定義一個(gè)org.springframework.data.redis.cache.RedisCacheConfiguration的Bean,并修改序列化器即可

/**
 * 此類解決 @Cacheable 存入的緩存使用預(yù)覽工具時(shí)亂碼問(wèn)題
 *
 * @author YinShangwen
 * @since 2023/10/5 14:02
 */
@Configuration
public class RedisCacheConfig {
    @Bean
    public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        // 工具預(yù)覽亂碼問(wèn)題
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer()));
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
    }
}

??
注意:如果之前有@Cacheable方式存儲(chǔ)的緩存需要清理掉。否則會(huì)因?yàn)樾蛄谢?反序列化方式不一致而導(dǎo)致錯(cuò)誤

源碼導(dǎo)讀

RedisCache#put

找到 org.springframework.data.redis.cache.RedisCache#put 方法。這個(gè)方法就是最終存入的方法

/*
 * (non-Javadoc)
 * @see org.springframework.cache.Cache#put(java.lang.Object, java.lang.Object)
 */
@Override
public void put(Object key, @Nullable Object value) {
    Object cacheValue = preProcessCacheValue(value);
    if (!isAllowNullValues() && cacheValue == null) {
        throw new IllegalArgumentException(String.format(
                "Cache '%s' does not allow 'null' values. Avoid storing null via '@Cacheable(unless=\"#result == null\")' or configure RedisCache to allow 'null' via RedisCacheConfiguration.",
                name));
    }
    cacheWriter.put(name, createAndConvertCacheKey(key), serializeCacheValue(cacheValue), cacheConfig.getTtl());
}

serializeCacheValue

我們注意到serializeCacheValue(cacheValue)

private final RedisCacheConfiguration cacheConfig;
/**
 * Serialize the value to cache.
 *
 * @param value must not be {@literal null}.
 * @return never {@literal null}.
 */
protected byte[] serializeCacheValue(Object value) {
    if (isAllowNullValues() && value instanceof NullValue) {
        return BINARY_NULL_VALUE;
    }
    return ByteUtils.getBytes(cacheConfig.getValueSerializationPair().write(value));
}

getValueSerializationPair

再看 cacheConfig.getValueSerializationPair() 是什么

private final SerializationPair<Object> valueSerializationPair;
/**
 * @return never {@literal null}.
 */
public SerializationPair<Object> getValueSerializationPair() {
    return valueSerializationPair;
}

這個(gè)變量就是最終決定序列化的類了,如何設(shè)置呢?在RedisCacheConfiguration中有如下方法

/**
 * Define the {@link SerializationPair} used for de-/serializing cache values.
 *
 * @param valueSerializationPair must not be {@literal null}.
 * @return new {@link RedisCacheConfiguration}.
 */
public RedisCacheConfiguration serializeValuesWith(SerializationPair<?> valueSerializationPair) {
    Assert.notNull(valueSerializationPair, "ValueSerializationPair must not be null!");
    return new RedisCacheConfiguration(ttl, cacheNullValues, usePrefix, keyPrefix, keySerializationPair,
            valueSerializationPair, conversionService);
}

默認(rèn)的RedisCacheConfiguration如何被裝載

找到類org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration

注意:類名相同但包路徑不相同
org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration
org.springframework.data.redis.cache.RedisCacheConfiguration

class RedisCacheConfiguration {
    ...
	private org.springframework.data.redis.cache.RedisCacheConfiguration createConfiguration(
			CacheProperties cacheProperties, ClassLoader classLoader) {
		Redis redisProperties = cacheProperties.getRedis();
		org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
				.defaultCacheConfig();
        // 重點(diǎn)
		config = config.serializeValuesWith(
				SerializationPair.fromSerializer(new JdkSerializationRedisSerializer(classLoader)));
		if (redisProperties.getTimeToLive() != null) {
			config = config.entryTtl(redisProperties.getTimeToLive());
		}
		if (redisProperties.getKeyPrefix() != null) {
			config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
		}
		if (!redisProperties.isCacheNullValues()) {
			config = config.disableCachingNullValues();
		}
		if (!redisProperties.isUseKeyPrefix()) {
			config = config.disableKeyPrefix();
		}
		return config;
	}
}

我們只看org.springframework.data.redis.cache.RedisCacheConfiguration是如何創(chuàng)建的所以省略了部分代碼

我們看到默認(rèn)使用的序列化器是 JdkSerializationRedisSerializer

以上就是SpringBoot使用@Cacheable出現(xiàn)預(yù)覽工具亂碼的解決方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot使用@Cacheable出現(xiàn)亂碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Mybatis-Plus之ID自動(dòng)增長(zhǎng)的設(shè)置實(shí)現(xiàn)

    Mybatis-Plus之ID自動(dòng)增長(zhǎng)的設(shè)置實(shí)現(xiàn)

    本文主要介紹了Mybatis-Plus之ID自動(dòng)增長(zhǎng)的設(shè)置實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Springmvc完成ajax功能實(shí)例詳解

    Springmvc完成ajax功能實(shí)例詳解

    在本篇文章里小編給大家整理了關(guān)于Springmvc完成ajax功能實(shí)例內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)下。
    2019-09-09
  • MyBatisPlus的簡(jiǎn)介及案例詳解

    MyBatisPlus的簡(jiǎn)介及案例詳解

    MyBatisPlus(簡(jiǎn)稱MP)是基于MyBatis框架基礎(chǔ)上開(kāi)發(fā)的增強(qiáng)型工具,旨在簡(jiǎn)化開(kāi)發(fā)、提高效率。本文將為大家詳細(xì)介紹一下MyBatisPlus是使用,需要的可以參考一下
    2022-07-07
  • java實(shí)現(xiàn)隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼詳解

    java實(shí)現(xiàn)隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼詳解

    這篇文章主要介紹了java實(shí)現(xiàn)隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼詳解,簡(jiǎn)單介紹了隊(duì)列結(jié)構(gòu)以應(yīng)用場(chǎng)景,涉及詳細(xì)實(shí)現(xiàn)代碼,還是比較不錯(cuò)的,這里分享給大家,需要的朋友可以參考下。
    2017-11-11
  • java算法入門(mén)之有效的括號(hào)刪除有序數(shù)組中的重復(fù)項(xiàng)實(shí)現(xiàn)strStr

    java算法入門(mén)之有效的括號(hào)刪除有序數(shù)組中的重復(fù)項(xiàng)實(shí)現(xiàn)strStr

    大家好,我是哪吒,一個(gè)熱愛(ài)編碼的Java工程師,本著"欲速則不達(dá),欲達(dá)則欲速"的學(xué)習(xí)態(tài)度,在程序猿這條不歸路上不斷成長(zhǎng),所謂成長(zhǎng),不過(guò)是用時(shí)間慢慢擦亮你的眼睛,少時(shí)看重的,年長(zhǎng)后卻視若鴻毛,少時(shí)看輕的,年長(zhǎng)后卻視若泰山,成長(zhǎng)之路,亦是漸漸放下執(zhí)念,內(nèi)心歸于平靜的旅程
    2021-08-08
  • SpringMVC訪問(wèn)靜態(tài)資源的三種方式小結(jié)

    SpringMVC訪問(wèn)靜態(tài)資源的三種方式小結(jié)

    這篇文章主要介紹了SpringMVC訪問(wèn)靜態(tài)資源的三種方式小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • JAVA獲得域名IP地址的方法

    JAVA獲得域名IP地址的方法

    這篇文章主要介紹了JAVA獲得域名IP地址的方法,涉及java域名操作的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • SpringAOP中@annotation與execution的深度示例對(duì)比分析

    SpringAOP中@annotation與execution的深度示例對(duì)比分析

    Spring AOP中@annotation通過(guò)注解標(biāo)記方法,適合定制化橫切邏輯;execution基于方法簽名匹配,用于通用功能增強(qiáng),兩者互補(bǔ),根據(jù)需求選擇,本文給大家介紹SpringAOP中@annotation與execution的深度示例對(duì)比分析,感興趣的朋友一起看看吧
    2025-08-08
  • Java函數(shù)式編程(十一):遍歷目錄

    Java函數(shù)式編程(十一):遍歷目錄

    這篇文章主要介紹了Java函數(shù)式編程(十一):遍歷目錄,本文是系列文章的第11篇,其它文章請(qǐng)參閱本文底部的相關(guān)文章,需要的朋友可以參考下
    2014-09-09
  • Spring實(shí)現(xiàn)IoC和DI的方法詳解

    Spring實(shí)現(xiàn)IoC和DI的方法詳解

    IoC全稱Inversion of Control (控制反轉(zhuǎn)) ,這里的控制其實(shí)是控制權(quán)的意思,可以理解為對(duì)象的獲取權(quán)力和方式發(fā)生了發(fā)轉(zhuǎn),DI依賴注?是?個(gè)過(guò)程,是指IoC容器在創(chuàng)建Bean時(shí), 去提供運(yùn)?時(shí)所依賴的資源,?資源指的就是對(duì)象,本文介紹了Spring實(shí)現(xiàn)IoC和DI的方法
    2024-08-08

最新評(píng)論

星座| 尖扎县| 胶州市| 农安县| 泗洪县| 安化县| 富平县| 米易县| 安岳县| 麟游县| 洱源县| 绍兴县| 克东县| 旬邑县| 大石桥市| 宝山区| 托克托县| 娱乐| 甘德县| 白山市| 来安县| 神池县| 留坝县| 合江县| 南溪县| 罗定市| 日土县| 淅川县| 昌乐县| 闽清县| 安西县| 大关县| 焦作市| 文安县| 静海县| 曲沃县| 缙云县| 馆陶县| 古丈县| 土默特右旗| 邹平县|