溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么使用@Cacheable緩存解決雙冒號(hào)的問題

發(fā)布時(shí)間:2021-12-31 11:28:18 來源:億速云 閱讀:419 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“怎么使用@Cacheable緩存解決雙冒號(hào)的問題”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么使用@Cacheable緩存解決雙冒號(hào)的問題”吧!

@Cacheable緩存解決雙冒號(hào)::

使用spring-data-redis2.x版本時(shí),@Cacheable緩存key值時(shí)默認(rèn)會(huì)給vlue或cacheNames后加上雙引號(hào)

怎么使用@Cacheable緩存解決雙冒號(hào)的問題

通過配置進(jìn)行修改即可滿足項(xiàng)目需求

@Configuration
public class SpringCacheConfig{
    @Bean
    public CacheManager cacheManager(LettuceConnectionFactory lettuceConnectionFactory){        
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()        
                .entryTtl(Duration.ofHour(1)                
                //變雙冒號(hào)為單冒號(hào)
                .computePrefixWith(name - >":")                
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))                
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))    
                .disableCachingNullValues();                
        RedisCacheManager cacheManager = RedisCacheManager.RedisCacheManagerBuilder        
                .fromConnectionFactory(lettuceConnectionFactory)                
                .cacheDefaults(config)                
                .transactionAware()                
                .build();
        return cacheManager;    
    }
    ...其他配置
}

Cacheable的使用總結(jié)

@Cacheable(value = "PERSON",key = "#tagId+'_'+#zz")
public List<Person> getPersonByTagid(Long tagId,String zz)

其中value表示該方法返回的參數(shù)的緩存存在那個(gè)Cache里(即為緩存塊的名字)

緩存結(jié)果以一個(gè)鍵值對(duì)存放在緩存中,注解中key即為鍵值對(duì)的key;value即為方法返回的結(jié)果

key用雙引號(hào),里面#加上方法的參數(shù):獲取方法的參數(shù),  ''表示在參數(shù)之間添加分隔符

常規(guī)使用方法

在springboot的啟動(dòng)類上使用@EnableCaching//開啟緩存

在方法上直接使用@Cacheable()使用緩存

到此,相信大家對(duì)“怎么使用@Cacheable緩存解決雙冒號(hào)的問題”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI