My code is below, when I get to the end and try to print out something from the cache, the key list is empty.
@Configuration
@EnableCaching
public class EhcacheConfiguration implements CachingConfigurer
{
CacheConfiguration cacheConfiguration = new CacheConfiguration();
cacheConfiguration.setName("DataCache");
cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
cacheConfiguration.setMaxEntiresLocalHeap(1000);
cacheConfiguration.setEternal(false);
net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
config.addCache(cacheConfiguration);
return net.sf.ehcache.CacheManager.newInstance(config);
}
@Bean
@Override
public CacheManager cacheManager()
{
return new EhCacheManager(ehCacheManager());
}
@Override
public CacheResolver cacheResolver()
{
return new SimpleCacheResolver();
}
@Bean
@Override
public KeyGenerator keyGenerator()
{
return new SimpleKeyGenerator();
}
@Override public CacheErrorHandler errorHandler()
{
return new SimpleCacheErrorHandler();
}
@Service
public class DataCalculationsDataServiceImp implements DataSubcalculationsDataService
{
.
.
.
@Cacheable("DataCache")
public ThreadSafeList<float[]> createCacheableDataList()
{
return new ThreadSafeList<float[]>();
}
@Override
public void readData(DataCalculationEtity dataCalculationEntity, InputStream inputStream)
{
.
.
.
ThreadSafeList<float[]> dataList = createCacheableDataList();
.
.
(dataList is eventually assigned data)
.
.
EhCacheCacheManager manager = new (EhCacheCacheManager)applicationContext.getBean("cacheManager");
Cache dataListCache = cacheManager.getCache("DataCache");
net.sf.ehcache.Ehcache ehCache = (net.sf.ehcache.Ehcache) dataListCache.getNativeCache();
LOG.info("Size of dataListCache: " + ehCache.getSize());
}
The size prints out as zero, and I cannot figure out why. I made some updates, like making my @Cacheable method public as suggested in one answer. I don't see why the call to the method annotated as @Cacheable would be ignored.
These two links reinforce the answer given by John R Stack Overflow similar question java2practice article