簡體   English   中英

鍵的含義作為Cacheable注釋的參數

[英]Meaning of a key as an argument to the Cacheable annotation

我正在嘗試使用Spring @Cacheable注解。

讓我們看一下:

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

    return new Person(firstName, surname, age);

}

我是否對,如果我添加2個姓氏相同的人,那么緩存中將只保存一個人,並且該方法有時會返回錯誤的結果?

我何時應指定此參數?

如果我未明確指定密鑰,spring如何創建密鑰(據我了解,當我使用CasheEvict和CashPut批注時應該知道它)?

JB Nizet是正確的,很明顯,在他鏈接到的文檔中!

35.3.1 @Cacheable注釋 ...

默認密鑰生成

由於緩存本質上是鍵值存儲,因此每次調用緩存方法都需要轉換為適合緩存訪問的鍵。 開箱即用的緩存抽象使用基於以下算法的簡單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. 

這是對您的方法重要的三種情況中的最后一種,沒有高速緩存參數

暫無
暫無

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

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