首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring5 @Cacheable不工作,即使拦截器调用在堆栈上也是如此

Spring5 @Cacheable不工作,即使拦截器调用在堆栈上也是如此
EN

Stack Overflow用户
提问于 2019-11-05 13:03:16
回答 1查看 756关注 0票数 0

My Cache annotation注册,它显示在跟踪日志中:

代码语言:javascript
复制
2019-11-04 23:55:52,229 TRACE AnnotationCacheOperationSource:102 - **Adding cacheable method 'getMaximumSimilarItems'** with attribute: [Builder[public java.lang.Integer com.quote.manager.impl.SystemConfigManagerImpl.getMaximumSimilarItems()] caches=[attachments] | key='#root.methodName' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false']
代码语言:javascript
复制
@Cacheable(value = "attachments", key = "#root.methodName")
    public Integer getMaximumSimilarItems() {

        logger.info("GetMax");
        SystemConfig systemConfig = systemConfigDao.getSystemConfig();

        Integer maxSimilarItems = systemConfig.getMaximumSimilarItems();

        if(maxSimilarItems != null) {
            return maxSimilarItems;
        }

        return 20;
    }

此外,在调试时,

我在调用的堆栈上和调用之后看到了缓存拦截器。但是,仍然没有缓存任何内容。我尝试了多种缓存实现。

关于如何在Spring的缓存实现中设置调试点来找出为什么它不缓存,你有什么想法吗?

我尝试了XML配置和Java config @EnableCaching。

代码语言:javascript
复制
<bean id="cacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
        <property name="cacheManager">
        <bean class="org.springframework.cache.jcache.JCacheManagerFactoryBean" 
            p:cacheManagerUri="classpath:ehcache.xml" />
        </property>
    </bean> 

<cache:annotation-driven  cache-manager="cacheManager" />


2019-11-05 11:16:51,370  INFO Eh107CacheManager:378 - Registering Ehcache MBean javax.cache:type=CacheStatistics,CacheManager=file./home/work/workspace-quotes/.metadata/.plugins/org.eclipse.wst.server.core/tmp7/wtpwebapps/app/WEB-INF/classes/ehcache.xml,Cache=attachments
2019-11-05 11:16:51,756  INFO Eh107CacheManager:378 - Registering Ehcache MBean javax.cache:type=CacheStatistics,CacheManager=file./home/work/workspace-quotes/.metadata/.plugins/org.eclipse.wst.server.core/tmp7/wtpwebapps/app/WEB-INF/classes/ehcache.xml,Cache=attachments

我看到在ehCache.xml中定义的缓存在应用程序启动时也在注册,所以缓存正在启动。

代码语言:javascript
复制
<ehcache:config
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  xmlns:ehcache='http://www.ehcache.org/v3'
  xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.1.xsd">

  <ehcache:cache alias="attachments">

    <ehcache:resources>
      <ehcache:heap unit="entries">100</ehcache:heap>
      <ehcache:offheap unit="MB">1</ehcache:offheap>
    </ehcache:resources>
  </ehcache:cache>
</ehcache:config>
EN

回答 1

Stack Overflow用户

发布于 2019-11-07 12:58:24

所以问题是,在上下文初始化之前,我调用@Cacheable函数太早了。一旦我调用了它,它就工作得很好。

我补充道:

代码语言:javascript
复制
 implements Runnable, ApplicationListener<ContextRefreshedEvent>  {

代码语言:javascript
复制
@Override
public void onApplicationEvent(ContextRefreshedEvent arg0) {

String name = arg0.getApplicationContext().getDisplayName();

if (name.contains("Root")) {
    // ANY CODE That requires @Cacheable is called HERE!.. 
    // Will not work if ran before this, such as inside @PostConstruct

    }
}

" Root“检查是一种黑客攻击,它只在Root事件完成时调用,否则会被调用两次。我不知道如何区分这两个事件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58704790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档