Java 如何使用JCache annotation@CacheRemoveAll清除多个缓存?
我有一个方法,它的执行应该清除Spring+JCache+ehcache 3.5项目中的两个缓存 我试过:Java 如何使用JCache annotation@CacheRemoveAll清除多个缓存?,java,spring,ehcache,jcache,Java,Spring,Ehcache,Jcache,我有一个方法,它的执行应该清除Spring+JCache+ehcache 3.5项目中的两个缓存 我试过: @CacheRemoveAll(cacheName = "cache1") @CacheRemoveAll(cacheName = "cache2") public void methodToBeCalled(){ } 及 首先,我得到: Duplicate annotation of non-repeatable type @CacheRemoveAll 在第二分钟,我得到: Dup
@CacheRemoveAll(cacheName = "cache1")
@CacheRemoveAll(cacheName = "cache2")
public void methodToBeCalled(){
}
及
首先,我得到:
Duplicate annotation of non-repeatable type @CacheRemoveAll
在第二分钟,我得到:
Duplicate attribute cacheName in annotation @CacheRemoveAll
你不能。注释不能重复,属性不能重复 您需要一个
@CacheRemoveAlls
注释,但框架没有计划这样做
最好的解决方案是在methodToBeCalled
的开头为两个缓存调用removeAll
代码如下所示:
public class MyClass {
@Autowired
private CacheManager cacheManager; // this is a Spring CacheManager
public void methodToBeCalled(){
cacheManager.getCache("cache1").clear();
cacheManager.getCache("cache2").clear();
}
}
如何在Java代码中调用此方法?我这样问是因为我使用ehcache.xml和SpringXMLServlet配置设置了缓存,并且我只通过注释使用它。那么我如何在代码中引用它呢?就像春天的一切一样。你注射它。我会更新我的答案给你看。
public class MyClass {
@Autowired
private CacheManager cacheManager; // this is a Spring CacheManager
public void methodToBeCalled(){
cacheManager.getCache("cache1").clear();
cacheManager.getCache("cache2").clear();
}
}