Cache是​​设置和使用的Ehcache在Spring后空 [英] Cache is empty after setting up and using ehcache in Spring

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

问题描述

我的code是下面,当我到达终点,并尝试从缓存中打印出来的东西,关键列表是空的。

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());
}

尺寸打印出为零,而且我想不通为什么。我做了一些更新,就像让我@Cacheable方法public作为一个答案建议。我不明白为什么标注为@Cacheable的方法调用将被忽略。

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.

这两个环节加强约翰 - [R给出的答案
<一href=\"http://stackoverflow.com/questions/20494574/spring-cacheable-not-working-whats-wrong-with-my-config\">Stack溢出类似的问题
<一href=\"http://java2practice.com/2013/03/23/spring-cacheable-and-cacheevict-explained-in-simple-terms/\"相对=nofollow> java2practice文章

These two links reinforce the answer given by John R Stack Overflow similar question java2practice article

推荐答案

我不知道有关该错误消息,但你有 @Cacheable 上一个私有方法。既然你让来自同一类中调用,它不是由Spring拦截,因此缓存没有发生。

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.

这是典型的春季的工作方式是由<一个href=\"http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#aop-understanding-aop-proxies\"相对=nofollow>创建代理每个 @Service (或 @Component @Controller 等)。当一些调用服务,它实际上击中代理。代理着眼于对实际的目标注释(例如, @Cacheable @Transactional ),然后做一些东西前/调用实际的目标方法后。

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.

我刚刚描述它的方式是有点简单化,并有其他的方式,Spring可以的代理类。在不是由一个接口指定的代理方法的情况下,春季可以动态地生成目标类(为您服务)的子类。还有编译因为其中的字节code实现的注解被注入到你编译的类文件时间和加载时间编织。

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.

如果你以前没有碰到这个问题,我强烈建议你阅读关于AOP的部分在Spring的文档。

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

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

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