Spring中设置和使用ehcache后缓存为空 [英] Cache is empty after setting up and using ehcache in Spring

查看:27
本文介绍了Spring中设置和使用ehcache后缓存为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下,当我走到最后并尝试从缓存中打印出一些东西时,密钥列表是空的.

@Configuration@启用缓存公共类 EhcacheConfiguration 实现 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);返回 net.sf.ehcache.CacheManager.newInstance(config);}@豆角,扁豆@覆盖公共缓存管理器缓存管理器(){返回新的 EhCacheManager(ehCacheManager());}@覆盖公共 CacheResolver cacheResolver(){返回新的 SimpleCacheResolver();}@豆角,扁豆@覆盖公共密钥生成器 keyGenerator(){返回新的 SimpleKeyGenerator();}@Override 公共 CacheErrorHandler errorHandler(){返回新的 SimpleCacheErrorHandler();}@服务公共类 DataCalculationsDataServiceImp 实现 DataSubcalculationsDataService{...@Cacheable("数据缓存")公共线程安全列表createCacheableDataList(){返回新的线程安全列表();}@覆盖public void readData(DataCalculationEtity dataCalculationEntity, InputStream inputStream){...ThreadSafeListdataList = createCacheableDataList();..(dataList 是最终赋值的数据)..EhCacheCacheManager manager = new (EhCacheCacheManager)applicationContext.getBean("cacheManager");缓存 dataListCache = cacheManager.getCache("DataCache");net.sf.ehcache.Ehcache ehCache = (net.sf.ehcache.Ehcache) dataListCache.getNativeCache();LOG.info("dataListCache 的大小:" + ehCache.getSize());}

尺寸打印为零,我不知道为什么.我做了一些更新,比如按照一个答案中的建议公开我的 @Cacheable 方法.我不明白为什么对注释为 @Cacheable 的方法的调用会被忽略.

这两个链接强化了 John R 给出的答案堆栈溢出类似问题java2practice 文章

解决方案

我不确定错误消息,但您在私有方法上拥有 @Cacheable.由于您是在同一个类中进行调用,因此 Spring 不会拦截它,因此不会发生缓存.

Spring 通常的工作方式是通过 为每个 @Service(或 @Component@Controller 等)创建代理.当某些东西调用服务时,它实际上会访问代理.代理查看实际目标上的注释(例如,@Cacheable@Transactional),然后在调用实际目标方法之前/之后做一些事情.>

我刚刚描述的方式有点简化,Spring还有其他方式可以代理你的班级.在代理方法不是由接口指定的情况下,Spring 可以动态生成目标类(您的服务)的子类.还有编译时间和加载时间编织,其中用于实现注释的字节码被注入到您编译的类文件中.

如果您以前没有遇到过这种情况,我强烈建议您阅读 Spring 文档中有关 AOP 的部分.

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.

这篇关于Spring中设置和使用ehcache后缓存为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆