SpringBoot使用Caffeine實(shí)現(xiàn)內(nèi)存緩存示例詳解
一、引入依賴
<!--caffeine-->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>caffeine提供了四種緩存策略:分別為手動(dòng)加載、自動(dòng)加載、異步手動(dòng)加載、異步自動(dòng)加載。
二、手動(dòng)加載
時(shí)間:
expireAfterWrite: 基于寫入時(shí)間
expireAfterAccess:基于訪問時(shí)間
expireAfter:可以根據(jù)讀更新寫入來調(diào)整有效期
權(quán)重:
maximumWeight:基于權(quán)重的容量策略,主要應(yīng)用于緩存中的元素存在不同的權(quán)重場(chǎng)景。
maximumSize:基于容量策略,當(dāng)緩存內(nèi)元素個(gè)數(shù)超過時(shí),通過基于就近度和頻率的算法來驅(qū)逐掉不會(huì)再被使用到的元素
查詢緩存&添加緩存:
asMap: 獲取所有緩存數(shù)據(jù)
getIfPresent(key):如果存在這個(gè)key的緩存數(shù)據(jù)就返回,如果不存在就返回null
get(key,(key)->{緩存初始化}):如果指定key的緩存不存在,就設(shè)置緩存的初始化數(shù)據(jù)并返回。
添加緩存:
put(key, val):添加緩存
清空緩存:
invalidate(key): 移除指定key的緩存
invalidateAll:移除所有緩存
測(cè)試
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
/**
* @author qinxun
* @date 2023-06-05
* @Descripion: caffeine使用
*/
@SpringBootTest
public class CacheTest {
/**
* 手動(dòng)加載
*/
@Test
void test1() throws InterruptedException {
Cache<Object, Object> cache = Caffeine.newBuilder()
// 設(shè)置寫入2分鐘后失效
.expireAfterWrite(2, TimeUnit.MINUTES)
// 設(shè)置最多緩存數(shù)量
.maximumSize(50)
.build();
// getIfPresent:獲取指定key的數(shù)據(jù) 不存在返回null
Object id = cache.getIfPresent("id");
System.out.println(id);
// get: 如果數(shù)據(jù)不存在就使用lambda表達(dá)式返回的數(shù)據(jù)設(shè)置緩存 并返回緩存數(shù)據(jù)
Object obj = cache.get("id", (e) -> 10);
System.out.println(obj);
// put: 添加緩存
cache.put("id", 20);
Object obj1 = cache.getIfPresent("id");
System.out.println(obj1);
// put:添加緩存
cache.put("name", "qq");
// asMap: 獲取緩存所有數(shù)據(jù)
ConcurrentMap<@NonNull Object, @NonNull Object> map = cache.asMap();
System.out.println(map);
// invalidate: 移除指定key緩存
cache.invalidate("name");
ConcurrentMap<@NonNull Object, @NonNull Object> map1 = cache.asMap();
System.out.println(map1);
// invalidateAll: 移除所有緩存
cache.invalidateAll();
ConcurrentMap<@NonNull Object, @NonNull Object> map2 = cache.asMap();
System.out.println(map2);
}
}null
10
20
{name=qq, id=20}
{id=20}
{}
三、自動(dòng)加載
/**
* 自動(dòng)加載
*/
@Test
void test2(){
AtomicInteger atomicInteger = new AtomicInteger(1);
LoadingCache<String, Integer> cache = Caffeine.newBuilder()
// 設(shè)置數(shù)據(jù)寫入2分鐘后過期
.expireAfterWrite(2, TimeUnit.MINUTES)
// 設(shè)置最多緩存數(shù)量
.maximumSize(50)
.build(new CacheLoader<String, Integer>() {
@Override
public @Nullable Integer load(@NonNull String s) throws Exception {
// 如果緩存的數(shù)據(jù)不存在 就返回這個(gè)自動(dòng)加載的數(shù)據(jù)
return atomicInteger.get();
}
});
// get:如果緩存的數(shù)據(jù)不存在 就使用自動(dòng)加載的數(shù)據(jù)
Integer data1 = cache.get("id");
System.out.println(data1);
cache.put("age",2);
//getAll:和get類似 如果緩存的key不存在就自動(dòng)加載默認(rèn)數(shù)據(jù) 否則返回緩存數(shù)據(jù)
List<String> keyList = Lists.newArrayList("id","age");
Map<@NonNull String, @NonNull Integer> map = cache.getAll(keyList);
System.out.println(map);
}1
{id=1, age=2}
四、異步手動(dòng)加載
- getIfPresent: 存在時(shí)返回CompletableFuture,不存在時(shí)返回null,因此注意npe的問題
- get(key, Function<>): 第二個(gè)參數(shù)表示加載數(shù)據(jù)的邏輯
- put(key, CompletableFuture<>): 手動(dòng)加入緩存,注意這里也不是直接加一個(gè)具體的value到緩存
- synchronous().invalidate() : 同步清除緩存
- getAll: 一次獲取多個(gè)緩存,同樣的是在緩存的取緩存,不在的根據(jù)第二個(gè)傳參進(jìn)行加載
/**
* 異步手動(dòng)加載
*/
@Test
void test3() throws ExecutionException, InterruptedException {
AsyncCache<Object, Object> asyncCache = Caffeine.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES)
.maximumSize(50)
.buildAsync();
// getIfPresent(key): 不存在就返回null
CompletableFuture<Object> idData = asyncCache.getIfPresent("id");
System.out.println(idData);
//get(key,(key)->{}):第二個(gè)參數(shù)表示當(dāng)不存在時(shí),初始化一個(gè),并寫入緩存中
CompletableFuture<Object> idData2 = asyncCache.get("id", (key) -> 5);
System.out.println(idData2.get());
//put(key,CompletableFuture): 手動(dòng)寫入一個(gè)緩存
asyncCache.put("id", CompletableFuture.supplyAsync(() -> 10));
//asMap:返回所有緩存的數(shù)據(jù)
ConcurrentMap<@NonNull Object, @NonNull CompletableFuture<Object>> map = asyncCache.asMap();
for (Map.Entry<Object, CompletableFuture<Object>> entry : map.entrySet()) {
System.out.println(entry.getKey() + "==>" + entry.getValue().get());
}
//synchronous().invalidate(key): 移除緩存
asyncCache.synchronous().invalidate("id");
asyncCache.put("age", CompletableFuture.supplyAsync(() -> 20));
ConcurrentMap<@NonNull Object, @NonNull CompletableFuture<Object>> map1 = asyncCache.asMap();
for (Map.Entry<Object, CompletableFuture<Object>> entry : map1.entrySet()) {
System.out.println(entry.getKey() + "==>" + entry.getValue().get());
}
}五、異步自動(dòng)加載
/**
* 異步自動(dòng)加載
*/
@Test
void test4() throws ExecutionException, InterruptedException {
AtomicInteger atomicInteger = new AtomicInteger(5);
AsyncLoadingCache<String, Integer> asyncLoadingCache = Caffeine.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES)
.maximumSize(50)
.buildAsync(new CacheLoader<String, Integer>() {
@Override
public @Nullable Integer load(@NonNull String s) throws Exception {
// 緩存不存在的時(shí)候 自動(dòng)加載這個(gè)數(shù)據(jù)
return atomicInteger.get();
}
});
//getIfPresent(key): 不存在就返回null
CompletableFuture<Integer> idData = asyncLoadingCache.getIfPresent("id");
System.out.println(idData);
// get(key): 緩存不存在就使用自動(dòng)加載的數(shù)據(jù)
CompletableFuture<Integer> idData2 = asyncLoadingCache.get("id");
System.out.println(idData2.get());
// put:手動(dòng)添加緩存
asyncLoadingCache.put("age", CompletableFuture.supplyAsync(() -> 20));
//asMap:返回所有緩存的數(shù)據(jù)
ConcurrentMap<@NonNull String, @NonNull CompletableFuture<Integer>> map = asyncLoadingCache.asMap();
for (Map.Entry<String, CompletableFuture<Integer>> entry : map.entrySet()) {
System.out.println(entry.getKey() + "==>" + entry.getValue().get());
}
// synchronous().invalidateAll(): 移除所有緩存
asyncLoadingCache.synchronous().invalidateAll();
CompletableFuture<Integer> ageData = asyncLoadingCache.getIfPresent("age");
System.out.println(ageData);
}null
5
id==>5
age==>20
null
到此這篇關(guān)于SpringBoot使用Caffeine實(shí)現(xiàn)內(nèi)存緩存的文章就介紹到這了,更多相關(guān)SpringBoot內(nèi)存緩存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入理解Java class文件格式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
對(duì)于理解JVM和深入理解Java語言, 學(xué)習(xí)并了解class文件的格式都是必須要掌握的功課2017-06-06
Java用freemarker導(dǎo)出word實(shí)用示例
本篇文章主要介紹了Java用freemarker導(dǎo)出word實(shí)用示例,使用freemarker的模板來實(shí)現(xiàn)功能,有需要的可以了解一下。2016-11-11
springboot整合mqtt實(shí)現(xiàn)消息訂閱和推送功能
mica-mqtt-client-spring-boot-starter是一個(gè)方便、高效、可靠的MQTT客戶端啟動(dòng)器,適用于需要使用MQTT協(xié)議進(jìn)行消息通信的Spring Boot應(yīng)用程序,這篇文章主要介紹了springboot整合mqtt實(shí)現(xiàn)消息訂閱和推送功能,需要的朋友可以參考下2024-02-02
java中對(duì)Redis的緩存進(jìn)行操作的示例代碼
本篇文章主要介紹了java中對(duì)Redis的緩存進(jìn)行操作的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
如何解決java.net.BindException:地址已在使用問題
當(dāng)Zookeeper啟動(dòng)報(bào)錯(cuò)“java.net.BindException:地址已在使用”時(shí),通常是因?yàn)橹付ǖ亩丝谝驯黄渌M(jìn)程占用,解決這個(gè)問題需要按照以下步驟操作:首先,使用命令如lsof -i:2181找到占用該端口的進(jìn)程號(hào);其次,使用kill命令終止該進(jìn)程2024-09-09
SpringBoot中@ConfigurationProperties實(shí)現(xiàn)配置自動(dòng)綁定的方法
本文主要介紹了SpringBoot中@ConfigurationProperties實(shí)現(xiàn)配置自動(dòng)綁定的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

