SpringBoot 緩存 Caffeine使用解析
Redis和Caffeine的區(qū)別
相同點(diǎn)
- 兩個(gè)都是緩存的方式
不同點(diǎn)
- redis是分布式緩存,通過(guò)網(wǎng)絡(luò)將數(shù)據(jù)存儲(chǔ)到redis服務(wù)器內(nèi)存里
- caffeine是將數(shù)據(jù)存儲(chǔ)在本地應(yīng)用里
- caffeine和redis相比,沒(méi)有了網(wǎng)絡(luò)IO上的消耗
聯(lián)系
- 一般將兩者結(jié)合起來(lái),形成一二級(jí)緩存。
- 使用流程大致如下:
- 先去一級(jí)緩存中查找數(shù)據(jù)(caffeine-本地應(yīng)用內(nèi)),
- 如果沒(méi)有的話,去二級(jí)緩存中查找數(shù)據(jù)(redis-內(nèi)存),
- 再?zèng)]有,再去數(shù)據(jù)庫(kù)中查找數(shù)據(jù)(數(shù)據(jù)庫(kù)-磁盤)
Spring Boot 緩存 Caffeine使用
1.需要添加的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.8.6</version>
</dependency>
2.配置
在SpringBoot中配置Caffeine,控制緩存行為(例如過(guò)期時(shí)間,緩存大小限制等)
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Configuration
@EnableCaching //開啟緩存
public class CaffeinConfig {
@Bean
//配置Caffeine緩存行為(例如到期,緩存大小限制等)
public Caffeine caffeineConfig() {
Caffeine caffeine = Caffeine.newBuilder()
.expireAfterWrite(60, TimeUnit.MINUTES)
.maximumSize(1000);
return caffeine;
}
@Bean
public CacheManager cacheManager(Caffeine caffeine) {
CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
caffeineCacheManager.setCaffeine(caffeine);
return caffeineCacheManager;
}
}
Caffeine配置說(shuō)明:
initialCapacity=[integer]:初始的緩存空間大小
maximumSize=[long]:緩存的最大條數(shù)
maximumWeight=[long]:緩存的最大權(quán)重
expireAfterAccess=[duration]:最后一次寫入或訪問(wèn)后經(jīng)過(guò)固定時(shí)間過(guò)期
expireAfterWrite=[duration]:最后一次寫入后經(jīng)過(guò)固定時(shí)間過(guò)期
refreshAfterWrite=[duration]:創(chuàng)建緩存或者最近一次更新緩存后經(jīng)過(guò)固定的時(shí)間間隔,刷新緩存
recordStats:開發(fā)統(tǒng)計(jì)功能
注意:
expireAfterWrite和expireAfterAccess同時(shí)存在時(shí),以expireAfterWrite為準(zhǔn)。
maximumSize和maximumWeight不可以同時(shí)使用
3.使用Caffeine緩存
示例1:
使用 @Cacheable(cacheNames = “xxx”) 或 @Cacheable(value = “xxx”) 注解在方法上。
@Cacheable(value = "caffeinSet_Value")
public String caffeinSetValue(Integer number) {
String str = number % 2 == 0 ? number + "是偶數(shù)" : number + "是奇數(shù)";
return str;
}
說(shuō)明: 每次執(zhí)行方法 caffeinSetValue 時(shí),會(huì)先去 caffeinSet_Value 緩存里根據(jù)傳入的 number 查找有沒(méi)有匹配的緩存,有則直接返回結(jié)果;沒(méi)有則執(zhí)行方法,執(zhí)行完后將結(jié)果加入緩存里,下次如果匹配直接返回。
示例2:
當(dāng)有多個(gè)參數(shù)時(shí),@Cacheable 注解里可以使用 key 來(lái)選擇參數(shù)進(jìn)行判斷緩存是否存在??梢允褂?condition 來(lái)進(jìn)行條件篩選,只有滿足條件的才會(huì)加入緩存。
/**
* number為偶數(shù)時(shí)才會(huì)緩存,緩存的key是傳入的number值
*/
@Cacheable(value = "caffeinSet_Value", key = "#number", condition = "#number%2==0")
public String caffeinSetValue(Integer number,String st) {
String str = number % 2 == 0 ? number + "是偶數(shù)" : number + "是奇數(shù)";
return str+st;
}
說(shuō)明: 假如傳入的參數(shù) number 是2,首先判斷 caffeinSet_Value 緩存里有沒(méi)有 key 是2的,有則直接回結(jié)果。沒(méi)有則執(zhí)行方法,因?yàn)闈M足 condition 的條件則最后將結(jié)果加入緩存。
假如傳入的參數(shù) number 是奇數(shù),則每次都會(huì)執(zhí)行方法,因?yàn)椴粷M足 condition ,不會(huì)被加入緩存。
示例3:
@Cacheable(value = "caffeinSet_Value", key = "#student.name", condition = "#student.age>10")
public Student caffeinSetValue(Student student,Integer number) {
System.out.println(11111);
return student;
}
說(shuō)明: 根據(jù)student對(duì)象里的name去caffeinSetValue緩存里查找。只有student對(duì)象里的age大于10的時(shí)候才會(huì)緩存結(jié)果。
注意:
一個(gè)方法A調(diào)同一個(gè)類里的另一個(gè)有緩存注解的方法B,這樣是不走緩存的。
例如在同一個(gè)CaffeinConsumer 類里面 invalidCache 調(diào)用 caffeinSetValue,是不走緩存的,緩存是不生效的;
@Service
public class CaffeinConsumer {
public String invalidCache(Integer number,String st) {
String str = caffeinSetValue(number,st);
return str;
}
/**
* number為偶數(shù)時(shí)才會(huì)緩存,緩存的key是傳入的number值
*/
@Cacheable(value = "caffeinSet_Value", key = "#number", condition = "#number%2==0")
public String caffeinSetValue(Integer number,String st) {
String str = number % 2 == 0 ? number + "是偶數(shù)" : number + "是奇數(shù)";
return str+st;
}
}
解決方案:
1.不使用注解的方式,直接取 Ehcache 的 CacheManger 對(duì)象,把需要緩存的數(shù)據(jù)放到里面,類似于使用 Map,緩存的邏輯自己控制;或者可以使用redis的緩存方式去添加緩存;
2.把方法A和方法B放到兩個(gè)不同的類里面,例如:如果兩個(gè)方法都在同一個(gè)service接口里,把方法B放到另一個(gè)service里面,這樣在A方法里調(diào)B方法,就可以使用B方法的緩存。
Caffeine其他常用注解
1.@CachePut:
被@CachePut標(biāo)注的方法在執(zhí)行前不會(huì)去檢查緩存中是否存在之前執(zhí)行過(guò)的結(jié)果,而是每次都會(huì)執(zhí)行該方法,并將執(zhí)行結(jié)果以鍵值對(duì)的形式存入指定的緩存中。
2.@CacheEvict:
@CacheEvict是用來(lái)標(biāo)注在需要清除緩存元素的方法或類上的。當(dāng)標(biāo)記在一個(gè)類上時(shí)表示其中所有的方法的執(zhí)行都會(huì)觸發(fā)緩存的清除操作。
@CacheEvict可以指定的屬性有value、key、condition、allEntries和beforeInvocation。其中value、key和condition的語(yǔ)義與@Cacheable對(duì)應(yīng)的屬性類似。
即value表示清除操作是發(fā)生在哪些Cache上的(對(duì)應(yīng)Cache的名稱);key表示需要清除的是哪個(gè)key,如未指定則會(huì)使用默認(rèn)策略生成的key;condition表示清除操作發(fā)生的條件。如果要清除所有緩存可使用屬性 allEntries=true
手動(dòng)添加、獲取、刪除緩存
上面的一些示例通過(guò)注解來(lái)進(jìn)行緩存操作,有時(shí)候我們需要在一些方法里對(duì)緩存進(jìn)行操作增刪改查:
1.從緩存中獲取數(shù)據(jù)
假設(shè)上面示例2中向名為 “caffeinSet_Value”的緩存里加入的鍵是8,值是"8是偶數(shù)!!"。
下面手動(dòng)獲取此緩存:
@Autowired
CacheManager cacheManager;
public String caffeinGetValue() {
Cache cache = cacheManager.getCache("caffeinSet_Value");
//獲取緩存名稱。name為caffeinSetValue
String name = cache.getName();
//獲取caffeinSetValue緩存里建是8的緩存
Cache.ValueWrapper value = cache.get(8);
String str ="";
if (null != value) {
//獲取值,8是偶數(shù)!!
str = String.valueOf(value.get());
}
return str;
}
2.向緩存中添加數(shù)據(jù)
@Autowired
CacheManager cacheManager;
public String caffeinPutValue() {
Cache cache = cacheManager.getCache("caffeinSet_Value");
//獲取緩存名稱。name為caffeinSetValue
String name = cache.getName();
/*
//向緩存中put數(shù)據(jù)。如果不存在key是20的才會(huì)加入
cache.putIfAbsent(number, "添加測(cè)試");
*/
//向緩存中put數(shù)據(jù)。如果存在key是20的會(huì)覆蓋原來(lái)的數(shù)據(jù)
cache.put(20,"20是偶數(shù)?。?);
return "成功";
}
3.刪除緩存中的數(shù)據(jù)
刪除caffeinSet_Value緩存中的某條緩存:
@Autowired
CacheManager cacheManager;
public String caffeinDeleteValue() {
Cache cache = cacheManager.getCache("caffeinSet_Value");
//獲取緩存名稱。name為caffeinSetValue
String name = cache.getName();
//只有20這條數(shù)據(jù)存在才會(huì)刪除
boolean bo = cache.evictIfPresent(20);
return String.valueOf(bo);
}
刪除caffeinSet_Value緩存中的所有緩存:
@Autowired
CacheManager cacheManager;
public String caffeinDeleteAllValue() {
Cache cache = cacheManager.getCache("caffeinSet_Value");
//獲取緩存名稱。name為caffeinSetValue
String name = cache.getName();
//刪除caffeinSet_Value中的所有緩存
boolean bo = cache.invalidate();
return String.valueOf(bo);
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
原生java代碼實(shí)現(xiàn)碼云第三方驗(yàn)證登錄的示例代碼
這篇文章主要介紹了原生java代碼實(shí)現(xiàn)碼云第三方驗(yàn)證登錄的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Java 中橋接模式——對(duì)象結(jié)構(gòu)型模式的實(shí)例詳解
這篇文章主要介紹了Java 中橋接模式——對(duì)象結(jié)構(gòu)型模式的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文大家能掌握這部分知識(shí),需要的朋友可以參考下2017-09-09
springboot讀取自定義配置文件節(jié)點(diǎn)的方法
這篇文章主要介紹了springboot讀取自定義配置文件節(jié)點(diǎn)的方法,本文給大家介紹的非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-05-05
Spring?Security?OAuth?Client配置加載源碼解析
這篇文章主要為大家介紹了Spring?Security?OAuth?Client配置加載源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
利用Spring Social輕松搞定微信授權(quán)登錄的方法示例
這篇文章主要介紹了利用Spring Social輕松搞定微信授權(quán)登錄的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Java請(qǐng)求調(diào)用參數(shù)格式為form-data類型的接口代碼示例
這篇文章主要給大家介紹了關(guān)于Java請(qǐng)求調(diào)用參數(shù)格式為form-data類型的接口的相關(guān)資料,文中給出了詳細(xì)的代碼示例,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08

