2016-03-06 15 views
0

我試圖使用Spring @Cacheable註釋。作爲緩存註釋的參數的鍵的含義

讓我們看一下:

@Cacheable(value = "employee", key = "#surname") 
public Person findEmployeeBySurname(String firstName, String surname, int age) { 

    return new Person(firstName, surname, age); 

} 

我說得對不對,如果我加2人同姓那麼只有一個將被保存在緩存中,並且該方法將返回有時不正確的結果?

什麼時候應該指定這個參數?

如果我沒有明確地指定(如我理解,當我使用CasheEvict和CashPut註釋時應該知道它),Spring如何創建密鑰?

+0

這是所有在文檔中解釋:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#cache-annotations-cacheable –

+0

@JB Nizet關於春天框架有多少SO問題通過參考文件覆蓋? – gstackoverflow

+1

我還沒有計算出任何統計數據。你想說什麼? –

回答

0

JB Nizet是正確的,這顯然在他鏈接到的文檔!

35.3.1 @Cacheable annotation ...

默認密鑰生成

由於緩存本質上是鍵值存儲,需要被轉化爲高速緩存 接入合適的密鑰的 緩存方法的每次調用。開箱即用的,緩存抽象使用基於以下算法簡單 的KeyGenerator:

If no params are given, return SimpleKey.EMPTY. 
If only one param is given, return that instance. 
If more the one param is given, return a SimpleKey containing all parameters. 

這是最後的三個案例,是importent您mehtods,wihout緩存參數