最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Cache的使用示例詳解

 更新時間:2025年01月17日 16:32:00   作者:randy.lou  
SpringCache是構(gòu)建在SpringContext基礎上的緩存實現(xiàn),提供了多種緩存注解,如@Cachable、@CacheEvict、@CachePut等,本文通過實例代碼介紹了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的基礎上的,如果原先沒有它的引用的話,需要添加對應的依賴

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>6.0.13</version>
</dependency>

Spring Context Support里提供了EhCache和Caffeine的CacheManager抽象,如果你打算用這兩個緩存實現(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會默認創(chuàng)建一個ConcurrentMapCacheManager負責緩存,不過它并不支持緩存過期。 除此以外,我們也可以手動創(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)用方法計算,并將結(jié)果寫入緩存

@Cacheable("card:id")
public String getCardById(Long id) {...}

添加了這個注解,相當于執(zhí)行偽代碼

String data = cacheManager.get(cacheKey)
if(data == null) {
	data = getCardById(id)
	cacheManager.put(cacheKey, data)
}
return cached;

@Cachable還有一個sync屬性,設置為true時會對緩存加載用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;

關于cacheKey的生成邏輯,見后續(xù)章節(jié)。

5. @CacheEvict

用于從緩存中刪除數(shù)據(jù),假設我們有一個更新card的接口,調(diào)用會清除card:id下的所有緩存。

@CacheEvict(value="card:id", allEntries=true)
public String updateCardById(Long cardId, Customer customer) {...}

我們可以可以清除指定key的數(shù)據(jù),key用SpEL來計算

@CacheEvict(value="card:id",, key = "'card_' + #cardId")
public String updateCardById(Long cardId, Customer customer) {...}

6. @CachePut

用于更新緩存數(shù)據(jù),想@Cacheable的區(qū)別是,它不會檢查緩存中是否有數(shù)據(jù),始終都調(diào)用方法獲取數(shù)據(jù),并更新緩存。

@CachePut("card:id")
public String getCardById(Long id) {...}

7. @Caching

用在一個方法上有多個緩存操作的時候,比如

@Caching(evict = { 
  @CacheEvict("addresses"), 
  @CacheEvict(value="directory", key="#customer.name") })
public String getAddress(Customer customer) {...}

8. @CacheConfig

用于設置service級別的緩存配置,比如CustomerDataService的方法都操作addresses這個緩存,可以這么配置

@CacheConfig(cacheNames={"addresses"})
public class CustomerDataService {
    @Cacheable
    public String getAddress(Customer customer) {...}
}

9. condition/unless

基于條件的緩存,condition基于入?yún)⑴袛啵瑄nless基于返回值判斷

@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來實現(xiàn)Key的生成,Spring 4.0之后默認采用SimpleKeyGenerator來生成Key,邏輯如下:

  • 方法沒有參數(shù)的話,默認返回SimpleKey.EMPTY
  • 方法就一個參數(shù),返回這個參數(shù)值
  • 方法有多個參數(shù),將參數(shù)封裝為SimpleKeySimpleKey會計算hashCode來作為緩存讀寫的Key,如果你不想要這個默認行為,可以通過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中可以使用的變量如下

NameDescriptionExample
methodName方法名#root.methodName
method方法#root.method.name
target當前方法所屬的實例#root.target
targetClass當前方法所在的類#root.targetClass
args方法的入?yún)?/td>#root.args[0]
caches對應的緩存#root.caches[0].name
參數(shù)名方法入?yún)⒚琂ava編譯時帶(-parameters)。否則使用#a<#idx> ,其中#idx是參數(shù)的位置(從0開始)#iban or #a0
result方法返回值,只在unless、@CachePut的key、@CacheEvict設置beforeInvocation=false時可用。如果返回值用來包裝類(如Optional),#result引用的是內(nèi)部對象#result

2. 自定義CacheManager

Spring提供了EhCache和Caffeine的默認實現(xiàn),如果使用沒有默認實現(xiàn)的Cache,可以通過自定義CacheManager來實現(xiàn)

@Bean
CacheManager cacheManager() {
	CaffeineCacheManager cacheManager = new CaffeineCacheManager();
	cacheManager.setCacheSpecification(...);
	cacheManager.setAsyncCacheMode(true);
	return cacheManager;
}

3. 實現(xiàn)原理

1. Cache抽象

Spring定義了一個Cache接口,用來實現(xiàn)緩存的寫入、讀取和清理,通過CacheManager、CacheResolver創(chuàng)建Cache對象,集成Spring Boot Starter Cache的時候其實就是創(chuàng)建CacheManager。通過將KeyGenerator生成緩存key,傳遞給Cache,來設置或讀取緩存。

2. 注解實現(xiàn)

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

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

A. 參考文檔 https://docs.spring.io/spring-framework/reference/integration/cache/annotations.html

到此這篇關于Spring Cache的使用的文章就介紹到這了,更多相關Spring Cache使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • java如何將list按照指定數(shù)量分成小list

    java如何將list按照指定數(shù)量分成小list

    本文介紹了四種不同的方法對集合進行分區(qū)操作,包括手動編寫代碼、使用Guava庫、Apache Commons Collection庫以及Java 8的流操作,每種方法都有其特點和適用場景,需要注意的是,部分方法返回的是原集合的視圖,而部分則返回的是新的集合
    2024-11-11
  • Spring項目中Ordered接口的應用之全局過濾器(GlobalFilter)的順序控制

    Spring項目中Ordered接口的應用之全局過濾器(GlobalFilter)的順序控制

    在Spring框架,尤其是Spring Cloud Gateway或Spring WebFlux項目中,Ordered接口扮演著重要的角色,特別是在實現(xiàn)全局過濾器(GlobalFilter)時,用于控制過濾器執(zhí)行的優(yōu)先級,下面將介紹如何在Spring項目中使用Ordered接口來管理Global Filter的執(zhí)行順序,需要的朋友可以參考下
    2024-06-06
  • MybatisPlus使用代碼生成器遇到的小問題(推薦)

    MybatisPlus使用代碼生成器遇到的小問題(推薦)

    這篇文章主要介紹了MybatisPlus使用代碼生成器遇到的小問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • java注解的全面分析

    java注解的全面分析

    這篇文章主要介紹了java注解的全面分析的相關資料,Java提供的一種原程序中的元素關聯(lián)任何信息和任何元數(shù)據(jù)的途徑和方法,需要的朋友可以參考下
    2017-08-08
  • springboot application無法使用$獲取pom變量的問題及解決

    springboot application無法使用$獲取pom變量的問題及解決

    這篇文章主要介紹了springboot application無法使用$獲取pom變量的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Springboot使用redis實現(xiàn)接口Api限流的示例代碼

    Springboot使用redis實現(xiàn)接口Api限流的示例代碼

    本文主要介紹了Springboot使用redis實現(xiàn)接口Api限流的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • Java中Supplier和Consumer接口的使用超詳細教程

    Java中Supplier和Consumer接口的使用超詳細教程

    文章闡述了Java8中Supplier與Consumer函數(shù)式接口的設計理念及應用價值,強調(diào)其通過封裝數(shù)據(jù)供給與消費邏輯,提升代碼靈活性與可維護性,對比傳統(tǒng)匿名內(nèi)部類展現(xiàn)簡潔優(yōu)勢,并在策略、觀察者等設計模式實現(xiàn)高效應用,為函數(shù)式編程提供標準化工具,感興趣的朋友跟隨小編一起看看吧
    2025-08-08
  • Java編寫實現(xiàn)九宮格應用

    Java編寫實現(xiàn)九宮格應用

    這篇文章主要為大家詳細介紹了Java編寫實現(xiàn)九宮格應用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Java獲取用戶IP屬地模擬抖音詳解

    Java獲取用戶IP屬地模擬抖音詳解

    細心的小伙伴可能會發(fā)現(xiàn),抖音新上線了 IP 屬地的功能,小伙伴在發(fā)表動態(tài)、發(fā)表評論以及聊天的時候,都會顯示自己的 IP 屬地信息,本篇文章我們來模擬實現(xiàn)這一功能
    2022-07-07
  • 如何在Spring data中使用r2dbc詳解

    如何在Spring data中使用r2dbc詳解

    這篇文章主要給大家介紹了關于如何在Spring data中使用r2dbc的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11

最新評論

德昌县| 香格里拉县| 资讯 | 滁州市| 界首市| 榕江县| 祁连县| 西充县| 泾川县| 威信县| 都兰县| 板桥市| 平塘县| 西和县| 临潭县| 金寨县| 泰州市| 商洛市| 文化| 上蔡县| 克拉玛依市| 九江县| 新余市| 黑河市| 孟津县| 康定县| 江油市| 和田县| 宁陵县| 海盐县| 三河市| 九龙坡区| 崇礼县| 阳信县| 神池县| 大冶市| 平度市| 荆州市| 合川市| 奉节县| 若尔盖县|