#StackBounty: #java #spring #spring-boot #caching #spring-cache @Cacheable doesn't intercept the method, cache is always empty

Bounty: 50

I have a method as following:

@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();
    }

}

I have the following in my 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>

I am declaring a CacheManager as follows:

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

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.

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.

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?

Update:

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

Update 2:

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.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.