简体   繁体   English

调用使用@Cacheable注释的方法(org.springframework.cache.annotation.Cacheable)

[英]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.

相关问题 使用 org.springframework.cache.annotation.Cacheable 时从缓存中删除项目的超时时间是多少 - What is the timeout for items getting removed from cache when using org.springframework.cache.annotation.Cacheable 从使用 @Cacheable 注释的方法中清除缓存 - Clearing cache from a method annotated with @Cacheable RefreshAheadCache 不调用 @cacheable 注释方法 - RefreshAheadCache not calling @cacheable annotated methods org.springframework.data.domain.PageImpl 无法反序列化,当我想使用带有注释 @Cacheable(spring cache) 的 findAll(Pageable pageable) 时? - org.springframework.data.domain.PageImpl can't be deserialized,when i want to use findAll(Pageable pageable) with annotation @Cacheable(spring cache)? 用@Cacheable注释的方法不会被拦截 - Method annotated with @Cacheable not getting intercepted 缓存刷新显示带有@cacheable注释的错误 - Cache refresh is showing error with @cacheable annotation 在 Cacheable 方法类中调用 Spring Cacheable 方法转换异常 - Calling Spring Cacheable method within Cacheable method class cast exception Spring的@Cacheable注释是否与注释方法的bean具有相同的范围? - Can Spring's @Cacheable annotation have the same scope as the annotated method's bean? @Before AOP 不会触发带有 @Cacheable 注释的方法 - @Before AOP not triggering to method with @Cacheable annotation Spring @Cacheable注解用于不同服务中的相同方法 - Spring @Cacheable annotation for same method in different service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM