I have configured my cache as follows:
@Configuration
@EnableCaching
public class CacheConfig {
@Bean(name = "caffeineCachingProvider")
public CachingProvider caffeineCachingProvider() {
return Caching.getCachingProvider("com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider");
}
@Bean(name = "caffeineCacheManager")
public JCacheCacheManager getSpringCacheManager() {
CacheManager cacheManager = caffeineCachingProvider().getCacheManager();
CaffeineConfiguration<String, List<Product>> caffeineConfiguration = new CaffeineConfiguration<>();
caffeineConfiguration.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new AccessedExpiryPolicy(new Duration(TimeUnit.MINUTES, 60))));
caffeineConfiguration.setCopierFactory(Copier::identity);
cacheManager.createCache("informerCache", caffeineConfiguration);
return new JCacheCacheManager(cacheManager);
}
}
Also I have the @Service
that uses it in following way:
@Service
public class InformerService {
@CacheResult(cacheName = "informerCache")
public List<Product> getProducts(@CacheKey String category, @CacheKey String countrySign, @CacheKey long townId) throws Exception {
Thread.sleep(5000);
// do some work
}
}
So I have the next behavior.
- When I'm calling the service method first time it takes 5 seconds then doing some work as expected.
- Calling method the second time with same parameters - > caching works -> returns result immediately
- Calling the third time with same parameters again results in
Thread.sleep
And all over again.
How to solve this ? Is that the issue about proxying ? What did I miss ?