天天看點

cacheable更新[email protected]不會攔截該方法,緩存始終為空

我有如下方法:

@Cacheable(value = "SAMPLE")

public List find() {

// Method that initiates and returns the List and takes around 2-3 seconds, does some logging too

}

我在其中一個配置類中啟用了緩存:

@EnableCaching

@Configuration

public SomeConf extends CachingConfigurerSupport {

// Here I also initialize my classes with @Cacheable annotation

@Bean

@Override

public CacheManager cacheManager() {

SimpleCacheManager cacheManager = new SimpleCacheManager();

cacheManager.setCaches(Collections.singletonList((new ConcurrentMapCache("SAMPLE"))));

return cacheManager;

}

@Bean

@Override

public CacheResolver cacheResolver() {

return new SimpleCacheResolver(cacheManager());

}

@Bean

@Override

public KeyGenerator keyGenerator() {

return new SimpleKeyGenerator();

}

}

我的内容如下pom.xml:

org.springframework.boot

spring-boot-starter-cache

1.5.14.RELEASE

我聲明CacheManager如下:

@Bean

public CacheManager cacheManager(){

SimpleCacheManager cacheManager = new SimpleCacheManager();

cacheManager.setCaches(Collections.singletonList((new ConcurrentMapCache("SAMPLE"))));

return cacheManager;

}

當我将一個@Autowired

CacheManager執行個體放入其中一個執行個體時,我@Service可以看到存在一個名為name的緩存"SAMPLE",但其條目始終為空。我一次又一次地調用方法find(),但是它似乎并未填充緩存。

我試圖把一個參數(比如int a)的find()方法,并把它作為key = "#a"對@Cacheable的,但什麼都沒有改變。

當我嘗試在隔離的環境中重新建立問題時,可以看到它正常運作。但是,當我添加我的依賴項(非開源公司庫,其中也包括EhCache配置)時,它不起作用。我該如何調試,我在做什麼錯?

更新:

我也試圖利用cacheManager = myCacheManager在@Cacheable為好。沒運氣。

更新2:

我正在使用AspectJSpring AOP。我認為這可能與它有關。我已經嘗試過@EnableCaching(mode =

AdviceMode.ASPECTJ),@EnableLoadTimeWeaving但同樣的事情。

更新3:

基本上,當您運作該應用程式telnet localhost 9000并向其發送任何行之後,NOT

CACHED即使該方法被兩次調用CachedController(第二次來自緩存),它也應該列印一次。但是它列印兩次。