Java spring缓存-为缓存操作返回空密钥
我一直在使用Spring Cache抽象和ehcache。我在目标方法上使用@Cacheable注释,如下所示:Java spring缓存-为缓存操作返回空密钥,java,spring,ehcache,Java,Spring,Ehcache,我一直在使用Spring Cache抽象和ehcache。我在目标方法上使用@Cacheable注释,如下所示: @Component public class DataService { @Cacheable(value="movieFindCache", key="#name") public String findByDirector(String name) { return "hello"; } } 这是我的jUnit测试: public cl
@Component
public class DataService {
@Cacheable(value="movieFindCache", key="#name")
public String findByDirector(String name) {
return "hello";
}
}
这是我的jUnit测试:
public class ServiceTest extends AbstractJUnit4SpringContextTests{
@Resource
private DataService dataService;
@Test
public void test_service() {
System.err.println(dataService.findByDirector("Hello"));
}
}
当我使用jUnit测试进行调试时,这是不正常的。它抛出一个IllegalArgumentException,如下所示:
java.lang.IllegalArgumentException: Null key returned for cache operation (maybe you are using named params on classes without debug info?) CacheableOperation[public java.lang.String com.eliteams.quick4j.web.service.ExcelDataService.getCarData()] caches=[movieFindCache] | key='#name' | condition='' | unless=''
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.cache.interceptor.CacheAspectSupport.generateKey(CacheAspectSupport.java:315)
at org.springframework.cache.interceptor.CacheAspectSupport.collectPutRequests(CacheAspectSupport.java:265)
我有以下配置:
applicationContext.xml:
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml" p:shared="true"/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehCacheManagerFactory"/>
ehcache.xml:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true">
<diskStore path="java.io.tmpdir" />
<cache name="movieFindCache"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap" />
</cache>
注意:如果我没有在@Cacheable注释中指定“key”,它就可以工作
有什么我忘了说明的吗?配置?注释?IllegalArgumentException的消息非常明确。spring文档中的指示了可用于使用参数名称的内容
以及有关的。在此上下文中,您需要
-g
键。您可以尝试将键替换为#p0
参考自
我使用Ehcache3实现了这一点。它在我的本地环境中使用参数名作为键运行良好,例如:
// this would fail
@Cacheable(value="movieFindCache", key="name")
public String findByDirector(String name) {
但是当部署在测试环境中时,我会收到错误。我通过从带有单个参数的方法的@Cacheable注释中删除key属性来解决此问题:
// this worked
@Cacheable("movieFindCache")
public String findByDirector(String name) {
有同样的问题,根本原因是在测试中参数实际上是空的,所以,只是添加了NOTNULL检查
@Cacheable(value="movieFindCache", key="#p0", condition="#p0!=null")
public String findByDirector(String name) {...}
如果只有一个参数,您实际上不需要指定密钥。您可以添加测试吗?好的,谢谢。我添加了它。您错过了
dataService.findByDirector()
哪个参数?
@Cacheable(value="movieFindCache", key="#p0", condition="#p0!=null")
public String findByDirector(String name) {...}