Spring Cache + Caffeine的整合與使用示例詳解
前言
對于一些項目里需要對數(shù)據(jù)庫里的某些數(shù)據(jù)一直重復(fù)請求的,且這些數(shù)據(jù)基本是固定的,在這種情況下,可以借助簡單使用本地緩存來緩存這些數(shù)據(jù)。這些介紹一下Spring Cache和Caffeine的使用。
引入依賴和CacheConfig
在pom文件里面引入下面的依賴:
<dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
在啟動類上加上@EnableCaching的注解
@EnableCaching public class SpringBootApplication{ }
新建一個CacheConfig類
@Configuration public class CacheConfig { /******************************** * @function : 生成緩存管理器 * @parameter : [] * @return : org.springframework.cache.CacheManager * @date : 2023/12/13 14:46 ********************************/ @Primary @Bean("customCacheManager") public CacheManager customCacheManager() { SimpleCacheManager simpleCacheManager = new SimpleCacheManager(); List<Cache> cacheList = new ArrayList<>(); cacheList.add(customCache()); simpleCacheManager.setCaches(cacheList); return simpleCacheManager; } /******************************** * @function : 生成自定義緩存容器 * @parameter : [] * @return : org.springframework.cache.Cache * @date : 2023/12/13 14:46 ********************************/ public Cache customCache() { return new CaffeineCache("customCache", Caffeine.newBuilder() .build(), true); } }
這里customCache()方法我并沒有設(shè)置相關(guān)過期時間和最大值,不設(shè)置會導(dǎo)致沒有默認過期時間和最大值。如果需要設(shè)置可以參考下面的寫法
public Cache customCache() { return new CaffeineCache("customCache", Caffeine.newBuilder() .maximumSize(100) .initialCapacity(100) .expireAfterWrite(10, TimeUnit.MINUTES) .recordStats() .build(), true); }
CaffeineCache參數(shù)的講解
- "customCache": 這是緩存的名稱。在應(yīng)用程序中,你可以通過這個名稱來獲取對應(yīng)的緩存實例。
Caffeine.newBuilder()
: 這是創(chuàng)建Caffeine緩存實例的起始點。newBuilder()
返回一個Caffeine
構(gòu)建器對象,用于配置和定制緩存的各種屬性。.maximumSize(100)
: 這是設(shè)置緩存的最大容量,即緩存可以容納的最大條目數(shù)。在這個例子中,緩存的最大容量被設(shè)置為100。.initialCapacity(100)
: 這是設(shè)置緩存的初始容量,即在緩存初始化時分配的內(nèi)部數(shù)據(jù)結(jié)構(gòu)的初始大小。在這個例子中,初始容量被設(shè)置為100。.expireAfterWrite(10, TimeUnit.MINUTES)
: 這是設(shè)置緩存項在被寫入后的過期時間。在這個例子中,緩存項將在被寫入后的10分鐘內(nèi)過期。.recordStats()
: 這是啟用緩存統(tǒng)計信息的選項。啟用后,你可以從緩存實例中獲取有關(guān)緩存使用情況的統(tǒng)計信息,例如命中率、加載次數(shù)等。
使用中,對過期策略的使用會比較重要,對于過期的策略有:
1.寫入后過期 (expireAfterWrite
): 緩存項被寫入后的一段時間內(nèi)過期??梢酝ㄟ^以下方式配置:
Caffeine.newBuilder() .expireAfterWrite(10, TimeUnit.MINUTES) .build();
在上述示例中,緩存項將在被寫入后的10分鐘內(nèi)過期。
2.訪問后過期 (expireAfterAccess
): 緩存項在一段時間內(nèi)沒有被訪問后過期。可以通過以下方式配置:
Caffeine.newBuilder() .expireAfterAccess(15, TimeUnit.MINUTES) .build();
在上述示例中,緩存項將在最后一次訪問后的15分鐘內(nèi)過期。
3.定時過期 (expireAfter
): 緩存項在指定的固定時間內(nèi)過期,不考慮寫入或訪問??梢酝ㄟ^以下方式配置:
Caffeine.newBuilder() .expireAfter(1, TimeUnit.HOURS) .build();
在上述示例中,緩存項將在創(chuàng)建后的1小時內(nèi)過期。
這些過期定時策略可以根據(jù)具體的使用場景和需求進行組合或選擇。
上面不同寫法將會導(dǎo)致生成不同的localcache實現(xiàn)類,可以在build方法中看到:
進入isBounded()方法:
如果使用緩存會調(diào)用localcache的get方法,最后進入computeIfAbsent()方法,對比上面兩個實現(xiàn)類的實現(xiàn),先是BoundedLocalCache:
UnboundedLocalCache:
下面這個并不會去檢查是否過期。
使用示范
在MVC的使用,可以將緩存的注解標(biāo)識于service層:
@Service @Slf4j @CacheConfig(cacheNames = "customCache", cacheManager = "customCacheManager") public class CalDataInitServiceImpl implements ICalDataInitService { @Cacheable(key = "#root.methodName + #sendCardName") public int getSlotCount(String sendCardName) { ..方法體 return calCard.getSlotCount(); } ... @CachePut(key = "#param") public String updateCache(String param) { // 對數(shù)據(jù)庫更新某個值 return updatedValue; } @CacheEvict(key = "#param") public void evictCache(String param) { // 對數(shù)據(jù)庫刪除某個值 } }
使用到的注解解析:
@CacheConfig(cacheNames = "customCache", cacheManager = "customCacheManager")
標(biāo)注在類上,cacheNames表示當(dāng)前使用的緩存名字,在創(chuàng)建緩存的時候有指定,第二個cacheManager是創(chuàng)建cacheManager管理器時指定的Bean名稱,這里是 @Bean("customCacheManager")。
@Cacheable
是Spring框架中用于聲明緩存規(guī)則的注解之一。它通常用于標(biāo)記在方法上,以指示Spring在執(zhí)行方法前先檢查緩存,如果緩存中已有數(shù)據(jù),則直接返回緩存中的數(shù)據(jù),而不執(zhí)行方法體。如果緩存中沒有數(shù)據(jù),則執(zhí)行方法體,并將方法的返回值存入緩存。
以下是 以@Cacheable
注解為例的主要參數(shù)介紹和使用方式:
1.value
(或 cacheNames
): 指定緩存的名稱,可以指定一個或多個緩存。如果指定多個緩存,Spring會依次檢查緩存,直到找到第一個有數(shù)據(jù)的緩存或全部檢查完畢。示例:
@Cacheable(value = "myCache") public String getCachedData() { // 方法體 }
2.key
: 指定緩存項的鍵。默認情況下,Spring會使用方法的參數(shù)作為鍵,但你也可以通過 key
屬性指定自定義的緩存鍵。示例:
@Cacheable(value = "myCache", key = "#param") public String getCachedData(String param) { // 方法體 }
3.condition
: 指定條件表達式,只有當(dāng)條件滿足時才會緩存。示例:
@Cacheable(value = "myCache", condition = "#result != null") public String getCachedData() { // 方法體 }
4.unless
: 指定一個條件表達式,當(dāng)條件為 true 時,不會將結(jié)果放入緩存。示例:
@Cacheable(value = "myCache", unless = "#result == null") public String getCachedData() { // 方法體 }
5.keyGenerator
: 指定自定義的緩存鍵生成器。這個屬性允許你提供一個實現(xiàn)了 org.springframework.cache.interceptor.KeyGenerator
接口的類,用于生成緩存鍵。示例:
@Cacheable(value = "myCache", keyGenerator = "customKeyGenerator") public String getCachedData() { // 方法體 }
6.sync
: 是否啟用同步模式。如果設(shè)置為 true,可以解決并發(fā)查的問題,Spring會在調(diào)用方法時鎖定緩存,防止多個線程同時訪問數(shù)據(jù)庫。默認為 false。示例:
@Cacheable(value = "myCache", sync = true) public String getCachedData() { // 方法體 }
這些是 @Cacheable
注解的一些常用參數(shù)??梢愿鶕?jù)實際需要選擇合適的參數(shù)來定義緩存規(guī)則。
在Spring中,除了 @Cacheable
,另外一些注解及其簡要介紹:
1.@CacheEvict
: 用于從緩存中移除數(shù)據(jù)。通常用于在方法執(zhí)行后清空指定緩存。示例:
@CacheEvict(value = "myCache", key = "#param") public void evictCache(String param) { // 方法體 }
2.@CachePut
: 用于將方法的返回值更新到緩存中,常用于更新緩存而不影響方法的執(zhí)行。示例:
@CachePut(value = "myCache", key = "#param") public String updateCache(String param) { // 方法體 return updatedValue; }
3.@Caching
: 用于將多個緩存相關(guān)的注解組合在一起,實現(xiàn)復(fù)雜的緩存操作。示例:
@Caching( evict = {@CacheEvict(value = "cache1", key = "#param1")}, put = {@CachePut(value = "cache2", key = "#param2")} ) public String complexCacheOperation(String param1, String param2) { // 方法體 }
4.@CacheConfig
: 用于在類級別配置緩存的一些公共屬性,避免在每個方法上都重復(fù)指定相同的緩存名稱等信息。示例:
@CacheConfig(cacheNames = "commonCache") public class MyService { @Cacheable public String getCachedData(String param) { // 方法體 } }
這些注解可以單獨使用,也可以結(jié)合使用,以滿足不同的緩存需求。
清空緩存的方法
清空所有緩存,可以不指定 value
和 key
,如下所示:
@CacheEvict(allEntries = true) public void evictAllCaches() { // 方法體 }
在這個例子中,allEntries = true
表示清空所有緩存。
如果你想根據(jù)某個條件來判斷是否清空緩存,可以使用 condition
屬性,例如:
@CacheEvict(value = "myCache", key = "#param", condition = "#param != 'noEviction'") public void evictCacheConditionally(String param) { // 方法體 }
在上述例子中,只有當(dāng) param
不等于 'noEviction'
時才會執(zhí)行緩存清空操作。
除了 @CacheEvict
,在一些特定場景下,@CachePut
也可以被用來“清空”緩存,因為它將方法的返回值放入緩存,如果返回值為 null
,相當(dāng)于移除緩存項。這種方式通常在更新操作時使用。
注意事項
如下圖代碼所示,如果在updateCache方法又調(diào)用了同個類里面的getSlotCount()方法,是不會使用到緩存的,這是因為緩存的實現(xiàn)是通過AOP實現(xiàn),在同個類里面調(diào)用方法,實際是通過this來調(diào),不會調(diào)用到代理對象,因此相當(dāng)于@Cacheable注解在這種情況是不生效的。
@Service @Slf4j @CacheConfig(cacheNames = "customCache", cacheManager = "customCacheManager") public class CalDataInitServiceImpl implements ICalDataInitService { @Cacheable(key = "#root.methodName + #sendCardName") public int getSlotCount(String sendCardName) { ..方法體 return calCard.getSlotCount(); } ... @CachePut(key = "#param") public String updateCache(String param) { getSlotCount("xx"); // 對數(shù)據(jù)庫更新某個值 return updatedValue; } }
到此這篇關(guān)于Spring Cache + Caffeine的整合與使用的文章就介紹到這了,更多相關(guān)Spring Cache Caffeine內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring MVC之WebApplicationContext_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Spring MVC之WebApplicationContext的相關(guān)資料,需要的朋友可以參考下2017-08-08Spring Boot 使用 Swagger 構(gòu)建 RestAPI 接口文檔
這篇文章主要介紹了Spring Boot 使用 Swagger 構(gòu)建 RestAPI 接口文檔,幫助大家更好的理解和使用Spring Boot框架,感興趣的朋友可以了解下2020-10-10java 中序列化NotSerializableException問題解決辦法
這篇文章主要介紹了java 中序列化NotSerializableException問題解決辦法的相關(guān)資料,這里對序列化問題進行描述說明,并提供解決辦法,希望能幫助到大家,需要的朋友可以參考下2017-08-08