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;
}
// 配置一個(gè)CacheManager 來(lái)支持spring cache的緩存注解
@Bean
public CacheManager cacheManager() {
RedisCacheConfiguration configuration = RedisCacheConfiguration
.defaultCacheConfig()
.entryTtl(Duration.ofDays(1)) //過(guò)期時(shí)間
;
return RedisCacheManager
.builder(redisConnectionFactory)
.cacheDefaults(configuration)
.build();
}
}cacheManager()方法繼承的是CachingConfigurerSupport類(lèi)

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,會(huì)從spring容器中查找是否有存在CacheManager,
若存在一個(gè)CacheManager會(huì)使用該CacheManager,若存在多個(gè)CacheManager則會(huì)拋出異常,即必須指定一個(gè)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
BufferedReader中read()方法和readLine()方法的使用
這篇文章主要介紹了BufferedReader中read()方法和readLine()方法的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
testNG項(xiàng)目通過(guò)idea Terminal命令行執(zhí)行的配置過(guò)程
這篇文章主要介紹了testNG項(xiàng)目通過(guò)idea Terminal命令行執(zhí)行,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
mybatis中如何用tinyint保存Boolean類(lèi)型
這篇文章主要介紹了mybatis中如何用tinyint保存Boolean類(lèi)型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
java如何刪除以逗號(hào)隔開(kāi)的字符串中某一個(gè)值
這篇文章主要介紹了java如何刪除以逗號(hào)隔開(kāi)的字符串中某一個(gè)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
springboot2如何禁用自帶tomcat的session功能
這篇文章主要介紹了springboot2如何禁用自帶tomcat的session功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
詳解Spring事件發(fā)布與監(jiān)聽(tīng)機(jī)制
Spring提供了ApplicationContext事件機(jī)制,可以發(fā)布和監(jiān)聽(tīng)事件,這個(gè)特性非常有用。Spring內(nèi)置了一些事件和監(jiān)聽(tīng)器,例如在Spring容器啟動(dòng)前,Spring容器啟動(dòng)后,應(yīng)用啟動(dòng)失敗后等事件發(fā)生后,監(jiān)聽(tīng)在這些事件上的監(jiān)聽(tīng)器會(huì)做出相應(yīng)的響應(yīng)處理2021-06-06
詳解idea maven nexus 常見(jiàn)命令配置
這篇文章主要介紹了idea maven nexus 常見(jiàn)命令配置的相關(guān)知識(shí),通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Mybatis結(jié)果集自動(dòng)映射的實(shí)例代碼
在使用Mybatis時(shí),有的時(shí)候我們可以不用定義resultMap,而是直接在<select>語(yǔ)句上指定resultType。這個(gè)時(shí)候其實(shí)就用到了Mybatis的結(jié)果集自動(dòng)映射,下面通過(guò)本文給大家分享Mybatis結(jié)果集自動(dòng)映射的實(shí)例代碼,一起看看吧2017-02-02

