gpt4 book ai didi

java - Spring 5 @Cacheable 不起作用,即使拦截器调用在堆栈上

转载 作者:行者123 更新时间:2023-12-01 22:03:47 25 4
gpt4 key购买 nike

我的缓存注释注册,正在 TRACE 日志中显示:

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']
@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;
}

另外在调试时, stack trace

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

关于在 Spring 的缓存实现中设置调试点以找出它不缓存的原因有什么想法吗?

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

<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 中定义的缓存也在应用程序启动时注册,因此缓存正在启动。

<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>

最佳答案

所以问题是我在初始化上下文之前过早调用 @Cacheable 函数。一旦我调用它,它就工作正常。

我添加:

 implements Runnable, ApplicationListener<ContextRefreshedEvent>  {

@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 事件完成时调用,否则它会被调用两次。我不知道如何区分这些事件。

关于java - Spring 5 @Cacheable 不起作用,即使拦截器调用在堆栈上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58704790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com