对于在 spring (3.1) 中使用 @Cacheable,我有以下内容:
Spring :
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/security/oauth2
http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
<!-- Ehcache library setup -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml" />
专家:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.5.3</version>
</dependency>
要缓存的方法:
@Cacheable(value="cahceName", key="concat(#param1).concat(‘-’).concat(#param2)")
public String cachedMethod(String param1,String param2)
唉,当我调试代码时,我看到即使 param1 和 param2 相同(即未使用 cahce),缓存的方法也被调用了不止一次。
有什么想法吗?
最佳答案
key 显示不正确 -
您的意思可能是 - @Cacheable(value="cacheName", key="#param1.concat(‘-’).concat(#param2)")
此外,如果在没有调试信息的情况下完成编译,则 param1、param2 参数名称将无法用于表达式计算器。相反,您可以通过 p0、p1 等方式引用它们:
@Cacheable(value="cahceName", key="#p0.concat('-').concat(#p1)")
更新:
我在这里进行了一页测试,演示了它是如何工作的 - https://gist.github.com/3315275
关于java - Spring @Cacheable 仍然执行复杂的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11889428/