2012-08-09 79 views
6

用於@Cacheable在彈簧的使用(3.1)我有以下:彈簧@Cacheable複雜鍵仍然執行

彈簧:

<?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" /> 

的Maven:

<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),我也會看到緩存的方法被多次調用。

任何想法?

+0

注:要使緩存工作,您需要調用接口中的方法。 – checklist 2012-08-13 18:52:13

+0

嗨覈對清單 - 你能否指定哪兩項建議解決了你的問題:key =「#param1.concat(' - ')。concat(#param2)」or key =「#p0.concat(' - ')。 concat(#p1)「??或兩者? – HellishHeat 2013-12-10 12:08:32

+0

嗨。這兩個工作 – checklist 2013-12-11 10:21:06

回答

15

關鍵顯示不正確 -

您可能意味着 - @Cacheable(value="cacheName", key="#param1.concat(‘-’).concat(#param2)")

此外,如果編譯沒有調試信息,參數1完成,參數2參數名稱將不會提供給表達式求值。相反,你可以參考他們使用P0,P1等這樣:

@Cacheable(value="cahceName", key="#p0.concat('-').concat(#p1)")

更新:

我這裏有一個頁面的測試,證明了這是如何工作 - https://gist.github.com/3315275

+1

你可以請詳細說明調試部分?爲什麼是p1不同於paam1 – checklist 2012-08-09 20:17:09

+0

這裏有一個相關的問題就可以了 - http://stackoverflow.com/questions/11041506/java-compiler-automatically-renaming-parameters-obfuscating – 2012-08-09 20:27:15

+0

我已經修復了密鑰但仍然沒有工作。我甚至完全拿走了鑰匙,並且我的方法不斷被調用。有什麼我可能做錯了嗎? – checklist 2012-08-10 06:33:02