java spring cache - 为缓存操作返回空键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29862053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring cache - Null key returned for cache operation
提问by Green Lei
I have been working with Spring Cache Abstraction and ehcache.I'm using the @Cacheable annotation on a target method like so:
我一直在使用 Spring Cache Abstraction 和 ehcache。我在目标方法上使用 @Cacheable 注释,如下所示:
@Component
public class DataService {
@Cacheable(value="movieFindCache", key="#name")
public String findByDirector(String name) {
return "hello";
}
}
This is my jUnit test:
这是我的 jUnit 测试:
public class ServiceTest extends AbstractJUnit4SpringContextTests{
@Resource
private DataService dataService;
@Test
public void test_service() {
System.err.println(dataService.findByDirector("Hello"));
}
}
This is not working fine when I debug with jUnit test. It throws an IllegalArgumentException as follows:
当我使用 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)
I have the following config:
我有以下配置:
applicationContext.xml:
应用上下文.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.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>
note: If I don't specify the "key" in the @Cacheable annotations, it works.
注意:如果我没有在 @Cacheable 注释中指定“键”,它就可以工作。
Is there anything I forgot to specify? config? annotations?
有什么我忘记指定的吗?配置?注释?
回答by david chi
you could try to replace key with #p0
你可以尝试用 #p0 替换 key
@Component
public class DataService {
@Cacheable(value="movieFindCache", key="#p0")
public String findByDirector(String name) {
return "hello";
}
}
reference from Spring Cache Abstraction VS interfaces VS key param ("Null key returned for cache operation" error)
来自Spring Cache Abstraction VS 接口 VS key 参数的引用 (“为缓存操作返回空键”错误)
回答by Wil
I had this happen using ehCache 3. It worked fine in my local environment using the parameter name as the key, for instance:
我使用 ehCache 3 发生了这种情况。它在使用参数名称作为键的本地环境中运行良好,例如:
// this would fail
@Cacheable(value="movieFindCache", key="name")
public String findByDirector(String name) {
but when deployed in a test environment I would receive the error. I resolved this by removing the key property from the @Cacheable annotation for methods with a single parameter:
但是在测试环境中部署时,我会收到错误消息。我通过从带有单个参数的方法的 @Cacheable 注释中删除 key 属性来解决这个问题:
// this worked
@Cacheable("movieFindCache")
public String findByDirector(String name) {
回答by Javasick
Had the same problem, root cause was that in tests parameter was really null, so, just added not null check
有同样的问题,根本原因是在测试参数真的为空,所以,只是添加了非空检查
@Cacheable(value="movieFindCache", key="#p0", condition="#p0!=null")
public String findByDirector(String name) {...}
回答by Louis Jacomet
The message of the IllegalArgumentException
is pretty explicit. The following tablein the spring documentation indicates what is available for using argument names.
的消息IllegalArgumentException
非常明确。spring 文档中的下表指示了可用于使用参数名称的内容。
And the relevant javac
options are documented. You want the -g
ones in this context.
并记录了相关javac
选项。你想要-g
在这种情况下的那些。