[英]Calling a method annotated with @Cacheable (org.springframework.cache.annotation.Cacheable)
When calling a method annotated with @Cacheable (org.springframework.cache.annotation.Cacheable), that exists in a different(third party) project causes the following error. 当调用带有@Cacheable注释的方法(org.springframework.cache.annotation.Cacheable)时,存在于另一个(第三方)项目中的方法将导致以下错误。
java.lang.IllegalArgumentException: Cannot find cache named 'usersCache' for CacheableOperation[public com.epsilon.amp.infra.model.AuthUser com.epsilon.amp.infra.dao.ContextDao.loadContextUser(java.lang.String,java.lang.String)] caches=[usersCache] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless=''
The annotation over the method on the third party project is as follows 第三方项目上方法的注释如下
@Cacheable("usersCache")
@Transactional(readOnly = true)
@Override
Adding annotations and enabling caching in my project doesn't fix the problem. 在我的项目中添加注释并启用缓存并不能解决问题。 What could have possibly gone wrong? 可能出了什么问题?
I suggest to you to create a Configuration like that : 我建议您创建这样的配置:
@Configuration
public class CacheService extends CachingConfigurerSupport {
@Bean
public CacheManager cacheManager() {
ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager() {
@Override
protected Cache createConcurrentMapCache(final String name) {
return new ConcurrentMapCache(name,
CacheBuilder.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES).maximumSize(100).build().asMap(), false);
}
};
return cacheManager;
}
@Bean
@Primary
public CacheManager guavaCacheManager() {
return new GuavaCacheManager();
}
}
After that, you must add this annotation at the head of your class you want to cache method. 之后,必须在要缓存方法的类的开头添加此批注。
@Cacheable(cacheNames = "guavaCacheManager")
And at the head of your Application class this annotation : @EnableCaching
在您的Application类的开头,此注释为: @EnableCaching
Guava info : https://github.com/google/guava 番石榴信息: https : //github.com/google/guava
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.