I've set up a scenario using caffeine cache and I can't get it working, the real method is always called when the parameters are the same. Here is my config:
pom.xml
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
...
Configuration class for the CacheManager
@Configuration
@EnableCaching
public class CachingConfig {
public static final String CACHE_NAME = "test";
@Bean
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager(CACHE_NAME);
cacheManager.setCaffeine(caffeineConfig());
return cacheManager;
}
private Caffeine caffeineConfig() {
return Caffeine.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.maximumSize(1024 * 1024 * 256);
}
}
And then the class with the cacheable method:
@CacheConfig(cacheNames = {CachingConfig.CACHE_NAME})
public class MyClass{
@Cacheable
public Object cacheableMethod(String a, String b, Boolean c) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new Object()
}
I've tried also adding the cache name to the Cacheable annotation:
@Cacheable(value = CachingConfig.CACHE_NAME)
And moving @EnableCaching
to the Spring Boot main application class.
The real method is always been called.
Any ideas of what I'm doing wrong?
Thanks
The @Cacheable method has to be located inside a @Bean, @Component, @Service...