Spring @cacheable註解實現的快取

OkidoGreen發表於2020-04-05

https://blog.csdn.net/u010251278/article/details/76229085

 

在軟體開發中使用快取已經有一個非常久的歷史了。快取是一種很好的設計思想,一旦你用了他,你將會發現他確實很有用。Spring3.1版本的核心對快取做了實現。在Java推出Annotation特性之前,實現快取的一個難點在於它與業務邏輯程式碼的耦合性太強。然而,Spring中使用@Cacheable 和@CacheEvict實現快取在某種程度上解決了這個問題,基本思想是在方法加上@Cacheable註解,這個方法的返回值將具有快取特性。

 

@Cacheable註解可以用在方法或者類級別。當他應用於方法級別的時候,就是如上所說的快取返回值了。當應用在類級別的時候,這個類的所有方法的返回值都將被快取。

@Cacheable(value = "employee")
public class EmployeeDAO {
 
  public Person findEmployee(String firstName, String surname, int age) {
 
    return new Person(firstName, surname, age);
  }
 
  public Person findAnotherEmployee(String firstName, String surname, int age) {
 
    return new Person(firstName, surname, age);
  }
}

 

@Cacheable註解有三個引數,value是必須的,還有key和condition。第一個引數,也就是value指明瞭快取將被存到什麼地方。
 

@Cacheable(value = "employee")
 public Person findEmployee(String firstName, String surname, int age) {
 
   return new Person(firstName, surname, age);
 }

上面的程式碼保證findEmployee的返回值Person物件將被儲存在"employee"中。

任何儲存在快取中的資料為了高速訪問都需要一個key。Spring預設使用被@Cacheable註解的方法的簽名來作為key,當然你可以重寫key,自定義key可以使用SpEL表示式。

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

在findEmployeeBySurname()的註解中"#surname"是一個SpEL表示式,他將使用findEmployeeBySurname()方法中的surname引數作為key。
 

@Cacheable的最後一個引數是condition(可選),同樣的,也是引用一個SpEL表示式。但是這個引數將指明方法的返回結果是否被快取。

@Cacheable(value = "employee", condition = "#age < 25")
 public Person findEmployeeByAge(String firstName, String surname, int age) {
 
   return new Person(firstName, surname, age);
 }

 

上面的例子中,只有年齡小於25的時候才被快取。
 

在快速看完了如何使用快取後,我們接下來看看快取帶來的效果。

@Test
  public void testCache() {
 
    Person employee1 = instance.findEmployee("John", "Smith", 33);
    Person employee2 = instance.findEmployee("John", "Smith", 33);
 
    assertEquals(employee1, employee2);
  }

 

上面的例子很簡單,第一次呼叫findEmployee,findEmployee方法將被執行,Spring將他的返回值一個person物件存入快取。第二次呼叫findEmployee的時候findEmployee將不被執行,Spring直接將快取中的資料作為返回值返回。所以employee1 和employee2引用了同樣的物件。
 

而下面的例子中,我們將年齡小於25作為快取條件,就將得到不同的結果。

@Test
 public void testCacheWithAgeAsCondition() {
 
   Person employee1 = instance.findEmployeeByAge("John", "Smith", 33);
   Person employee2 = instance.findEmployeeByAge("John", "Smith", 33);
 
   assertEquals(employee1, employee2);
 }

下面的例子我們在findEmployeeBySurname的方法的註解中自定義了key,我們使用了自定義的key生成方式,以確保不同的surname將會指向不同的人。看下面的程式

@Test
 public void testCacheOnSurnameAsKey() {
 
   Person employee1 = instance.findEmployeeBySurname("John", "Smith", 22);
   Person employee2 = instance.findEmployeeBySurname("Jack", "Smith", 55);
 
   assertEquals(employee1, employee2);
 }

 

我們想找到兩個不同的人,但是兩個人的surname是相同的,你將發現兩次呼叫返回了相同的結果,這不是Spring的問題,而是我們的cache key的生成方式有問題。所以在我們定義key的時候要小心注意key的生成策略,避免造成這種問題。
 

最後總結一下流程,當執行到一個被@Cacheable註解的方法時,Spring首先檢查condition條件是否滿足,如果不滿足,執行方法,返回;如果滿足,在value所命名的快取空間中查詢使用key儲存的物件,如果找到,將找到的結果返回,如果沒有找到執行方法,將方法的返回值以key-物件的方式存入value快取中,然後方法返回。這裡只是介紹了簡單的spring @cacheable註解的用法,更詳細的使用可以參考下面的參考資料。

相關文章