spring boot 快取配置

2021-09-01 12:31:12 字數 4316 閱讀 9673

隨著時間的積累,應用的使用使用者不斷增加,資料規模也越來越大,往往資料庫查詢操作會成為影響使用者使用體驗的瓶頸,此時使用快取往往是解決這一問題非常好的手段之一。spring 3開始提供了強大的基於註解的快取支援,可以通過註解配置方式低侵入的給原有spring應用增加快取功能,提高資料訪問效能。

在spring boot中對於快取的支援,提供了一系列的自動化配置,使我們可以非常方便的使用快取。下面我們通過乙個簡單的例子來展示,我們是如何給乙個既有應用增加快取功能的。

準備工作

為了更好的理解快取,我們先對該工程做一些簡單的改造。

@runwith(springjunit4classrunner.class)

@autowired

private userrepository userrepository;

@before

public void before()

@test

public void test() throws exception

}

hibernate: insert into user (age, name) values (?, ?)

hibernate: select user0_.id as id1_0_, user0_.age as age2_0_, user0_.name as name3_0_ from user user0_ where user0_.name=?

第一次查詢:10

hibernate: select user0_.id as id1_0_, user0_.age as age2_0_, user0_.name as name3_0_ from user user0_ where user0_.name=?

第二次查詢:10

在測試用例執行前,插入了一條user記錄。然後每次findbyname呼叫時,都執行了一句select語句來查詢使用者名為aaa的記錄。

引入快取

org.springframework.boot

spring-boot-starter-cache

@enablecaching

public static void main(string args)

}

@cacheconfig(cachenames = "users")

public inte***ce userrepository extends jparepository

hibernate: insert into user (age, name) values (?, ?)

hibernate: select user0_.id as id1_0_, user0_.age as age2_0_, user0_.name as name3_0_ from user user0_ where user0_.name=?

第一次查詢:10

第二次查詢:10

到這裡,我們可以看到,在呼叫第二次findbyname函式時,沒有再執行select語句,也就直接減少了一次資料庫的讀取操作。

為了可以更好的觀察,快取的儲存,我們可以在單元測試中注入cachemanager。

@autowired

private cachemanager cachemanager;

使用debug模式執行單元測試,觀察cachemanager中的快取集users以及其中的user物件的快取加深理解。

回過頭來我們再來看,這裡使用到的兩個註解分別作了什麼事情。

除了這裡用到的兩個註解之外,還有下面幾個核心註解:

完成了上面的快取實驗之後,可能大家會問,那我們在spring boot中到底使用了什麼快取呢?

在spring boot中通過@enablecaching註解自動化配置合適的快取管理器(cachemanager),spring boot根據下面的順序去偵測快取提供者:

除了按順序偵測外,我們也可以通過配置屬性spring.cache.type來強制指定。我們可以通過debug除錯檢視cachemanager物件的例項來判斷當前使用了什麼快取。

本文中不對所有的快取做詳細介紹,下面以常用的ehcache為例,看看如何配置來使用ehcache進行快取管理。

在spring boot中開啟ehcache非常簡單,只需要在工程中加入ehcache.xml配置檔案並在pom.xml中增加ehcache依賴,框架只要發現該檔案,就會建立ehcache的快取管理器。

net.sf.ehcache

ehcache

完成上面的配置之後,再通過debug模式執行單元測試,觀察此時cachemanager已經是ehcachemanager例項,說明ehcache開啟成功了。

spring.cache.ehcache.config=classpath:config/another-config.xml
spel 字面量:

spel引用bean , 屬性和方法:

spel支援的運算符號:

@cacheable 支援如下幾個引數:

value:快取位置名稱,不能為空,如果使用ehcache,就是ehcache.xml中宣告的cache的name

key:快取的key,預設為空,既表示使用方法的引數型別及引數值作為key,支援spel

condition:觸發條件,只有滿足條件的情況才會加入快取,預設為空,既表示全部都加入快取,支援spel

@cacheevict 支援如下幾個引數:

value:快取位置名稱,不能為空,同上

key:快取的key,預設為空,同上

condition:觸發條件,只有滿足條件的情況才會清除快取,預設為空,支援spel

allentries:true表示清除value中的全部快取,預設為false

spel表示式

spel表示式可基於上下文並通過使用快取抽象,提供與root獨享相關聯的快取特定的內建引數。

名稱    位置    描述    示例

methodname    root物件    當前被呼叫的方法名    #root.methodname

method    root物件    當前被呼叫的方法    #root.method.name

target    root物件    當前被呼叫的目標物件例項    #root.target

targetclass    root物件    當前被呼叫的目標物件的類    #root.targetclass

args    root物件    當前被呼叫的方法的引數列表    #root.args[0]

caches    root物件    當前方法呼叫使用的快取列表    #root.caches[0].name

argument name    執行上下文    當前被呼叫的方法的引數,如findartisan(artisan artisan),可以通過#artsian.id獲得引數    #artsian.id

result    執行上下文    方法執行後的返回值(僅當方法執行後的判斷有效,如 unless cacheevict的beforeinvocation=false)    #result

--------------------- 

@cacheable不能配置  condition = "#result != null" ,因為這個註解在進入方法前去檢測condition,而這時還沒有result,會造成result為null的情況。

@cacheevict 想要需要配置condition = "#result != null"時 ,同時需要配置 beforeinvocation = false。beforeinvocation 的意思為 是否在方法執行前就刪除快取,預設為 false。如果不配置這兩個,會造成方法返回為空時,也會刪除快取,但是返回值都為空了,還會不會有快取呢?所以這個看具體情況了。

@cacheput 使用condition = "#result != null" 沒問題。但是如果像我這樣配置的key

key = "'roomid_'+ #result.id"

則需要配置 condition = "#result != null" ,否則當result為null時,會報異常。

同時經過測試,當@cacheable、@cacheput(沒有設定condition返回不為空)標註的方法返回值為null時,不會存入redis快取。

SpringBoot 配置靜態檔案快取

spring resources chain strategy content enabled true paths cache true compressed false enabled true cache cachecontrol cache public trueimport org.spr...

springboot快取測試

配置pom.xml加入快取依賴 org.springframework.boot spring boot starter cache 程式入口加上註解 enablecaching快取對應的實體類序列化 implements serializable 在快取的需要快取的service 加上 cache...

SpringBoot快取使用

org.springframework.boot spring boot starter cache 專案使用springboot自帶的快取機制實現快取 redis快取 redis是一款記憶體快取記憶體資料庫 membase快取 memcache是乙個自由和開放源 高效能 分配的記憶體物件快取系統。...