@Cacheable不会拦截该方法,缓存始终为空 [英] @Cacheable doesn't intercept the method, cache is always empty

查看:515
本文介绍了@Cacheable不会拦截该方法,缓存始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下方法:

@Cacheable(value = "SAMPLE")
public List<SomeObj> find() {
     // Method that initiates and returns the List<SomeObj> and takes around 2-3 seconds, does some logging too
}

在我的配置类之一中进行缓存:

And I am enabling caching in one of my configuration classes:

@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

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
    <version>1.5.14.RELEASE</version>
</dependency>

我声明了 CacheManager ,如下所示:

I am declaring a CacheManager as follows:

@Bean
public CacheManager cacheManager(){
    SimpleCacheManager cacheManager = new SimpleCacheManager();
    cacheManager.setCaches(Collections.singletonList((new ConcurrentMapCache("SAMPLE"))));
    return cacheManager;
}

当我收到 @Autowired CacheManager 实例放入我的 @Service 之一中,我可以看到存在名称为<$的缓存c $ c>样本 ,但其条目始终为空。我一次又一次地调用方法 find(),但是它似乎并不填充缓存。

When I get an @Autowired CacheManager instance into one of my @Services I can see that there exists a cache with name "SAMPLE", but its entries are always empty. I call again and again the method find(), but it doesn't seem to populate the cache.

我试图将一个参数(例如 int a )放入 find()方法,并将其设置为 key = #a @Cacheable ,但没有任何变化。

I have tried to put an argument (say int a) to the find() method and put it as key = "#a" to @Cacheable, but nothing changed.

当我尝试在隔离的环境中重新创建问题时,可以看到它正常运行。但是,当我添加依赖项时(非开放源代码公司的库,其中也包含 EhCache 配置),它不起作用。我该如何调试,我在做什么错?

When I try to recreate the issue in an isolated environment, I can see that it works properly. But when I add my dependencies (non open-source company libraries, which include an EhCache configuration as well) it doesn't work. How can I debug this, what am I doing wrong?

更新:

我还尝试在 @Cacheable 中也使用 cacheManager = myCacheManager

I have also tried to use cacheManager = myCacheManager in @Cacheable as well. No luck.

更新2:

我正在使用 AspectJ 和Spring AOP。我认为这可能与它有关。我已经尝试了 @EnableCaching(mode = AdviceMode.ASPECTJ) @EnableLoadTimeWeaving ,但是还是一样。

I am using AspectJ and Spring AOP. I think that it may have something to do with it. I have tried @EnableCaching(mode = AdviceMode.ASPECTJ) with @EnableLoadTimeWeaving but same thing.

更新3:

我终于能够重现该问题,这里是: client-api-cache

I was finally able to reproduce the issue, here it is: client-api-cache

基本上,当您运行应用程序并向其发送任何行后运行 telnet localhost 9000 时,它应该一次打印 NO CACHED 即使该方法在 CachedController 中被调用了两次(第二个来自缓存)。但这会打印两次。

Basically when you run the application and telnet localhost 9000 after you send any line to it, it should print NOT CACHED once even though the method is called twice in CachedController (Second coming from the cache). But it prints twice.

推荐答案

根本原因是您滥用了 afterPropertiesSet。因此,您正在做的是无限循环,并且永远不会将控制权传递回Spring管道,因此Spring无法正确初始化缓存工具。

The root cause is that you are misusing "afterPropertiesSet". So, what you are doing is endless loop and never pass control back to the Spring pipeline, so Spring isn't able to init caching facility properly.

查看可解决我们问题的代码: https:// dumpz .org / cbx8h28KeAss

Check out code which fixes our problem: https://dumpz.org/cbx8h28KeAss

这篇关于@Cacheable不会拦截该方法,缓存始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆