简体   繁体   中英

Cache is empty after setting up and using ehcache in Spring

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

I'm not sure about the error message, but you have the @Cacheable on a private method. Since you're making the call from within the same class, it isn't intercepted by Spring and therefore the caching isn't happening.

The way that Spring typically works is by creating proxies for each @Service (or @Component , @Controller , etc). When something calls the service, it actually hits the proxy. The proxy looks at the annotations on the actual target (for example, @Cacheable or @Transactional ) and then does some stuff before/after calling the actual target method.

The way I just described it is a bit of a simplification and there are other ways that Spring can proxy your class . In cases where the proxied method is not specified by an interface, Spring can dynamically generate a subclass of the target class (your service). There's also compile time and load time weaving where the bytecode to implement the annotations is injected into your compiled class files.

If you haven't run into this before, I highly recommend reading the section on AOP in the Spring docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM