Spring Cache的使用示例詳解
1. 使用入門
1. 添加依賴
在spring boot中使用Spring Caching需要先引入依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>3.1.5</version>
</dependency>Spring Caching是構(gòu)建在Spring Context的基礎(chǔ)上的,如果原先沒有它的引用的話,需要添加對(duì)應(yīng)的依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.13</version>
</dependency>Spring Context Support里提供了EhCache和Caffeine的CacheManager抽象,如果你打算用這兩個(gè)緩存實(shí)現(xiàn)的話,還有依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>6.0.13</version>
</dependency>2. 啟用緩存
在已經(jīng)添加了spring-boot-starter-cache的前提下,只需要使用@EnableCaching注解,Spring會(huì)默認(rèn)創(chuàng)建一個(gè)ConcurrentMapCacheManager負(fù)責(zé)緩存,不過它并不支持緩存過期。 除此以外,我們也可以手動(dòng)創(chuàng)建CacheManager
@Configuration
@EnableCaching
public class CachingConfig {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("users");
}
}3. 集成EhCache
添加EhCache的依賴
<!-- Ehcache -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<!-- Spring Integration for Ehcache -->
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache-jsr107</artifactId>
</dependency>在src/main/resources創(chuàng)建配置文件ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<cache name="card:id"
maxEntriesLocalHeap="1000"
eternal="false"
timeToIdleSeconds="10"
timeToLiveSeconds="30"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>4. @Cachable
這是最常用的注解,如果緩存中存在,返回緩存中的數(shù)據(jù),否則調(diào)用方法計(jì)算,并將結(jié)果寫入緩存
@Cacheable("card:id")
public String getCardById(Long id) {...}添加了這個(gè)注解,相當(dāng)于執(zhí)行偽代碼
String data = cacheManager.get(cacheKey)
if(data == null) {
data = getCardById(id)
cacheManager.put(cacheKey, data)
}
return cached;@Cachable還有一個(gè)sync屬性,設(shè)置為true時(shí)會(huì)對(duì)緩存加載用cacheKey做同步,如果加了注解@Cacheable("card:id", sync=true)的話,偽代碼如下
String data = cacheManager.get(cacheKey)
if(data == null) {
synchronized(cacheKey) {
data = cacheManager.get(cacheKey);
if(data == null) {
data = getCardById(id)
cacheManager.put(cacheKey, data)
}
}
}
return cached;關(guān)于cacheKey的生成邏輯,見后續(xù)章節(jié)。
5. @CacheEvict
用于從緩存中刪除數(shù)據(jù),假設(shè)我們有一個(gè)更新card的接口,調(diào)用會(huì)清除card:id下的所有緩存。
@CacheEvict(value="card:id", allEntries=true)
public String updateCardById(Long cardId, Customer customer) {...}我們可以可以清除指定key的數(shù)據(jù),key用SpEL來計(jì)算
@CacheEvict(value="card:id",, key = "'card_' + #cardId")
public String updateCardById(Long cardId, Customer customer) {...}6. @CachePut
用于更新緩存數(shù)據(jù),想@Cacheable的區(qū)別是,它不會(huì)檢查緩存中是否有數(shù)據(jù),始終都調(diào)用方法獲取數(shù)據(jù),并更新緩存。
@CachePut("card:id")
public String getCardById(Long id) {...}7. @Caching
用在一個(gè)方法上有多個(gè)緩存操作的時(shí)候,比如
@Caching(evict = {
@CacheEvict("addresses"),
@CacheEvict(value="directory", key="#customer.name") })
public String getAddress(Customer customer) {...}8. @CacheConfig
用于設(shè)置service級(jí)別的緩存配置,比如CustomerDataService的方法都操作addresses這個(gè)緩存,可以這么配置
@CacheConfig(cacheNames={"addresses"})
public class CustomerDataService {
@Cacheable
public String getAddress(Customer customer) {...}
}9. condition/unless
基于條件的緩存,condition基于入?yún)⑴袛?,unless基于返回值判斷
@CachePut(value="addresses", condition="#customer.name=='Tom'")
public String getAddress(Customer customer) {...}
@CachePut(value="addresses", unless="#result.length()<64")
public String getAddress(Customer customer) {...}2. 定制能力
1. Key生成
Spring提供了KeyGenerator來實(shí)現(xiàn)Key的生成,Spring 4.0之后默認(rèn)采用SimpleKeyGenerator來生成Key,邏輯如下:
- 方法沒有參數(shù)的話,默認(rèn)返回SimpleKey.EMPTY
- 方法就一個(gè)參數(shù),返回這個(gè)參數(shù)值
- 方法有多個(gè)參數(shù),將參數(shù)封裝為
SimpleKeySimpleKey會(huì)計(jì)算hashCode來作為緩存讀寫的Key,如果你不想要這個(gè)默認(rèn)行為,可以通過SpEL自定義Key,比如
@Cacheable(cacheNames="books", key="#isbn") public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed) @Cacheable(cacheNames="books", key="#isbn.rawNumber") public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed) @Cacheable(cacheNames="books", key="T(someType).hash(#isbn)") public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)
除此以外,還可以自定義KeyGenerator,將自定義的KeyGenerator定義為bean后,在注解中引用
@Cacheable(cacheNames="books", keyGenerator="myKeyGenerator") public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)
SpEL中可以使用的變量如下
| Name | Description | Example |
|---|---|---|
methodName | 方法名 | #root.methodName |
method | 方法 | #root.method.name |
target | 當(dāng)前方法所屬的實(shí)例 | #root.target |
targetClass | 當(dāng)前方法所在的類 | #root.targetClass |
args | 方法的入?yún)?/td> | #root.args[0] |
caches | 對(duì)應(yīng)的緩存 | #root.caches[0].name |
| 參數(shù)名 | 方法入?yún)⒚琂ava編譯時(shí)帶(-parameters)。否則使用#a<#idx> ,其中#idx是參數(shù)的位置(從0開始) | #iban or #a0 |
result | 方法返回值,只在unless、@CachePut的key、@CacheEvict設(shè)置beforeInvocation=false時(shí)可用。如果返回值用來包裝類(如Optional),#result引用的是內(nèi)部對(duì)象 | #result |
2. 自定義CacheManager
Spring提供了EhCache和Caffeine的默認(rèn)實(shí)現(xiàn),如果使用沒有默認(rèn)實(shí)現(xiàn)的Cache,可以通過自定義CacheManager來實(shí)現(xiàn)
@Bean
CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.setCacheSpecification(...);
cacheManager.setAsyncCacheMode(true);
return cacheManager;
}3. 實(shí)現(xiàn)原理
1. Cache抽象
Spring定義了一個(gè)Cache接口,用來實(shí)現(xiàn)緩存的寫入、讀取和清理,通過CacheManager、CacheResolver創(chuàng)建Cache對(duì)象,集成Spring Boot Starter Cache的時(shí)候其實(shí)就是創(chuàng)建CacheManager。通過將KeyGenerator生成緩存key,傳遞給Cache,來設(shè)置或讀取緩存。

2. 注解實(shí)現(xiàn)
通過AOP代理了標(biāo)注@Cacheable、@CacheEvict、@CachePut等注解的方法,程序的入口在ProxyCachingConfiguration中,他會(huì)創(chuàng)建Advisor和Interceptor,實(shí)現(xiàn)對(duì)Bean對(duì)象的AOP。BeanFactoryCacheOpertionSourceAdvisor內(nèi)部使用CacheOperationSource來過濾切點(diǎn)類,如果我們是基于Annotation來使用緩存的話,實(shí)現(xiàn)類是AnnotationCacheOperationSource,它負(fù)責(zé)失敗方法上的@Cacheable等注解。實(shí)際的緩存邏輯由CacheInterceptor實(shí)現(xiàn),核心代碼在CacheAspectSupport類內(nèi),尤其是execute方法。

CacheAspectSupport的execute方法的核心邏輯就是生成key、讀取緩存、實(shí)際調(diào)用方法、寫入緩存。

A. 參考文檔 https://docs.spring.io/spring-framework/reference/integration/cache/annotations.html
到此這篇關(guān)于Spring Cache的使用的文章就介紹到這了,更多相關(guān)Spring Cache使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springmvc DispatcherServlet原理及用法解析
這篇文章主要介紹了Springmvc DispatcherServlet原理及用法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java生成二維碼的兩種實(shí)現(xiàn)方式(基于Spring?Boot)
這篇文章主要給大家介紹了關(guān)于Java生成二維碼的兩種實(shí)現(xiàn)方式,文中的代碼基于Spring?Boot,本文基于JAVA環(huán)境,以SpringBoot框架為基礎(chǔ)開發(fā),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
springboot中將日志信息存儲(chǔ)在catalina.base中過程解析
這篇文章主要介紹了springboot中將日志信息存儲(chǔ)在catalina.base中過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
使用Maven創(chuàng)建和管理多模塊項(xiàng)目的詳細(xì)步驟
使用Maven進(jìn)行多模塊項(xiàng)目管理是一種常見的做法,它可以幫助你組織大型項(xiàng)目,使其結(jié)構(gòu)更加清晰,便于維護(hù)和構(gòu)建,以下是使用Maven創(chuàng)建和管理多模塊項(xiàng)目的詳細(xì)步驟,需要的朋友可以參考下2024-10-10
關(guān)于SpringBoot靜態(tài)資源路徑管理問題
這篇文章主要介紹了SpringBoot靜態(tài)資源路徑管理,主要包括默認(rèn)靜態(tài)資源路徑,增加靜態(tài)資源路徑前綴的相關(guān)操作,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
java web開發(fā)之servlet圖形驗(yàn)證碼功能的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了java web開發(fā)之servlet中圖形驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

