簡體   English   中英

spring cache - 為緩存操作返回空鍵

[英]spring cache - Null key returned for cache operation

我一直在使用 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)

我有以下配置:

應用程序上下文.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注釋中指定“鍵”,它可以工作。

有什么我忘記指定的嗎? 配置? 注釋?

你可以嘗試用 #p0 替換 key

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

來自Spring Cache Abstraction VS 接口 VS 鍵參數的引用(“為緩存操作返回空鍵”錯誤)

有同樣的問題,根本原因是在測試參數真的是空的,所以,只是添加了非空檢查

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

我使用 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) {

IllegalArgumentException的消息非常明確。 spring 文檔中的下表指示了可用於使用參數名稱的內容。

記錄了相關的javac選項 在這種情況下,您需要-g

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM