【问题标题】:spring cache - Null key returned for cache operationspring cache - 为缓存操作返回空键
【发布时间】:2015-04-25 06:54:33
【问题描述】:

我一直在使用 Spring Cache Abstraction 和 ehcache。我在目标方法上使用 @Cacheable 注释,如下所示:

@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
  • 可以添加测试吗?
  • 好的,谢谢。我加了。
  • 你错过了dataService.findByDirector()的参数
  • 请问哪个参数?

标签: java spring ehcache


【解决方案1】:

你可以尝试用#p0替换key

@Component
public class DataService {
    @Cacheable(value="movieFindCache", key="#p0")
    public String findByDirector(String name) {
        return "hello";
    }
}

参考自 Spring Cache Abstraction VS interfaces VS key param ("Null key returned for cache operation" error)

【讨论】:

    【解决方案2】:

    遇到同样的问题,根本原因是tests参数确实为null,所以,只是添加了not null检查

    @Cacheable(value="movieFindCache", key="#p0", condition="#p0!=null")
    public String findByDirector(String name) {...}
    

    【讨论】:

      【解决方案3】:

      我在使用 ehCache 3 时遇到过这种情况。它在我的本地环境中使用参数名称作为键运行良好,例如:

      // this would fail
      @Cacheable(value="movieFindCache", key="name")
          public String findByDirector(String name) {
      

      但是当部署在测试环境中时,我会收到错误消息。我通过从具有单个参数的方法的 @Cacheable 注释中删除 key 属性解决了这个问题:

      // this worked
      @Cacheable("movieFindCache")
          public String findByDirector(String name) {
      

      【讨论】:

        【解决方案4】:

        IllegalArgumentException 的信息非常明确。 spring 文档中的 following table 指示了可用于使用参数名称的内容。

        还有相关的javac options are documented。在这种情况下,您需要 -g

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-05-01
          • 2015-04-17
          • 1970-01-01
          • 2013-12-05
          • 2014-10-12
          • 1970-01-01
          • 1970-01-01
          • 2022-08-08
          相关资源
          最近更新 更多