[英]Spring cache using @Cacheable during @PostConstruct does not work
[英]Spring Cacheable does not work
我正在使用Spring配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven />
<mvc:resources mapping="/static/**" location="/static/" />
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<property name="name" value="default"/>
</bean>
</set>
</property>
</bean>
<bean id="bucketsGenerator" class="net.rwchess.utils.PythonBucketsGenerationService">
<constructor-arg index="0" value="/home/bodia/server/jetty/webapps/ROOT/WEB-INF/python/"/>
</bean>
..........
<mvc:default-servlet-handler />
</beans>
並且PythonBucketsGenerationService
方法generateBuckets()
可以緩存其返回值。 所以我宣布:
@Service
public class PythonBucketsGenerationService {
private String pythonDir;
public PythonBucketsGenerationService(String pythonDir) {
this.pythonDir = pythonDir;
}
@Cacheable(value = "buckets")
public List<Bucket> generateBuckets(List<TournamentPlayer> players) {
.....
}
}
問題是即使當List<TournamentPlayer> players
的hashCode具有相同的值(我檢查過)時,也會在每次調用時執行該方法。 TournamentPlayer
正確覆蓋了equals()
和hashCode()
。 這有什么不對?
您很可能缺少啟用注釋驅動的緩存配置(緩存:注釋驅動)。 看這個例子:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven />
</beans>
有關更多文檔,請轉到此頁面: http : //docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/cache.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.