Spring Cache指定CacheManager方式
Spring Cache指定CacheManager
配置文件application.properties中配置redis的相關(guān)配置
spring.redis.database= spring.redis.host= spring.redis.port=
Spring Cache 配置文件
@EnableCaching @Configuration public class CacheConfig extends CachingConfigurerSupport { private final RedisConnectionFactory redisConnectionFactory; @Autowired public CacheConfig(RedisConnectionFactory redisConnectionFactory) { this.redisConnectionFactory = redisConnectionFactory; } // 配置一個CacheManager 來支持spring cache的緩存注解 @Bean public CacheManager cacheManager() { RedisCacheConfiguration configuration = RedisCacheConfiguration .defaultCacheConfig() .entryTtl(Duration.ofDays(1)) //過期時間 ; return RedisCacheManager .builder(redisConnectionFactory) .cacheDefaults(configuration) .build(); } }
cacheManager()方法繼承的是CachingConfigurerSupport類
AbstractCachingConfiguration是spring cache的配置文件加載方法
可以看到useCachingConfigurer方法里調(diào)用了cacheManager方法
就這樣完成了指定spring cache的 CacheManager
@Configuration public abstract class AbstractCachingConfiguration implements ImportAware { ......省略 @Autowired(required = false) void setConfigurers(Collection<CachingConfigurer> configurers) { if (CollectionUtils.isEmpty(configurers)) { return; } if (configurers.size() > 1) { throw new IllegalStateException(configurers.size() + " implementations of " + "CachingConfigurer were found when only 1 was expected. " + "Refactor the configuration such that CachingConfigurer is " + "implemented only once or not at all."); } CachingConfigurer configurer = configurers.iterator().next(); useCachingConfigurer(configurer); } /** * Extract the configuration from the nominated {@link CachingConfigurer}. */ protected void useCachingConfigurer(CachingConfigurer config) { this.cacheManager = config::cacheManager; this.cacheResolver = config::cacheResolver; this.keyGenerator = config::keyGenerator; this.errorHandler = config::errorHandler; } }
若不指定CacheManager,會從spring容器中查找是否有存在CacheManager,
若存在一個CacheManager會使用該CacheManager,若存在多個CacheManager則會拋出異常,即必須指定一個
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java并發(fā)編程ReentrantReadWriteLock加讀鎖流程
這篇文章主要介紹了Java并發(fā)編程ReentrantReadWriteLock加讀鎖流程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05SpringBoot集成Access?DB實(shí)現(xiàn)數(shù)據(jù)導(dǎo)入和解析
microsoft?office?access是由微軟發(fā)布的關(guān)聯(lián)式數(shù)據(jù)庫管理系統(tǒng),它結(jié)合了?microsoft?jet?database?engine?和?圖形用戶界面兩項(xiàng)特點(diǎn),是一種關(guān)系數(shù)據(jù)庫工具,本文給大家介紹了SpringBoot集成Access?DB實(shí)現(xiàn)數(shù)據(jù)導(dǎo)入和解析,需要的朋友可以參考下2024-11-11IDEA連接mysql數(shù)據(jù)庫報錯的解決方法
這篇文章主要介紹了IDEA連接mysql數(shù)據(jù)庫報錯的解決方法,文中有非常詳細(xì)的圖文示例,對出現(xiàn)Server returns invalid timezone. Go to ‘Advanced‘ tab and set ‘serverTimezone‘ prope報錯的小伙伴們很有幫助喲,需要的朋友可以參考下2021-05-05Java實(shí)現(xiàn)指定線程執(zhí)行順序的三種方式示例
這篇文章主要介紹了Java實(shí)現(xiàn)指定線程執(zhí)行順序的三種方式,包括通過共享對象鎖加上可見變量,通過主線程Join()以及通過線程執(zhí)行時Join()等三種實(shí)現(xiàn)方法,需要的朋友可以參考下2019-01-01Java整數(shù)和字符串相互轉(zhuǎn)化實(shí)例詳解
這篇文章主要介紹了Java整數(shù)和字符串相互轉(zhuǎn)化實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02Monaco?Editor實(shí)現(xiàn)sql和java代碼提示實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Monaco?Editor代碼提示sql和java實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Java 實(shí)現(xiàn)攔截器Interceptor的攔截功能方式
這篇文章主要介紹了Java 實(shí)現(xiàn)攔截器Interceptor的攔截功能方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10基于idea解決springweb項(xiàng)目的Java文件無法執(zhí)行問題
這篇文章給大家介紹了基于idea解決springweb項(xiàng)目的Java文件無法執(zhí)行問題,文中通過圖文結(jié)合的方式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02