亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot使用Caffeine實現(xiàn)內(nèi)存緩存示例詳解

 更新時間:2023年06月10日 11:04:17   作者:qinxun2008081  
caffeine提供了四種緩存策略:分別為手動加載、自動加載、異步手動加載、異步自動加載,這篇文章主要介紹了SpringBoot使用Caffeine實現(xiàn)內(nèi)存緩存,需要的朋友可以參考下

一、引入依賴

<!--caffeine-->
<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
</dependency>

caffeine提供了四種緩存策略:分別為手動加載、自動加載、異步手動加載、異步自動加載。

二、手動加載

時間:

expireAfterWrite: 基于寫入時間

expireAfterAccess:基于訪問時間

expireAfter:可以根據(jù)讀更新寫入來調(diào)整有效期

權(quán)重:

maximumWeight:基于權(quán)重的容量策略,主要應用于緩存中的元素存在不同的權(quán)重場景。

maximumSize:基于容量策略,當緩存內(nèi)元素個數(shù)超過時,通過基于就近度和頻率的算法來驅(qū)逐掉不會再被使用到的元素

查詢緩存&添加緩存:

asMap: 獲取所有緩存數(shù)據(jù)

getIfPresent(key):如果存在這個key的緩存數(shù)據(jù)就返回,如果不存在就返回null

get(key,(key)->{緩存初始化}):如果指定key的緩存不存在,就設置緩存的初始化數(shù)據(jù)并返回。

添加緩存:

put(key, val):添加緩存

清空緩存:

invalidate(key): 移除指定key的緩存

invalidateAll:移除所有緩存

測試

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 {
    /**
     * 手動加載
     */
    @Test
    void test1() throws InterruptedException {
        Cache<Object, Object> cache = Caffeine.newBuilder()
                // 設置寫入2分鐘后失效
                .expireAfterWrite(2, TimeUnit.MINUTES)
                // 設置最多緩存數(shù)量
                .maximumSize(50)
                .build();
        // getIfPresent:獲取指定key的數(shù)據(jù) 不存在返回null
        Object id = cache.getIfPresent("id");
        System.out.println(id);
        // get: 如果數(shù)據(jù)不存在就使用lambda表達式返回的數(shù)據(jù)設置緩存 并返回緩存數(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}
{}

三、自動加載

    /**
     * 自動加載
     */
    @Test
    void test2(){
        AtomicInteger atomicInteger = new AtomicInteger(1);
        LoadingCache<String, Integer> cache = Caffeine.newBuilder()
                // 設置數(shù)據(jù)寫入2分鐘后過期
                .expireAfterWrite(2, TimeUnit.MINUTES)
                // 設置最多緩存數(shù)量
                .maximumSize(50)
                .build(new CacheLoader<String, Integer>() {
                    @Override
                    public @Nullable Integer load(@NonNull String s) throws Exception {
                        // 如果緩存的數(shù)據(jù)不存在 就返回這個自動加載的數(shù)據(jù)
                        return atomicInteger.get();
                    }
                });
        // get:如果緩存的數(shù)據(jù)不存在 就使用自動加載的數(shù)據(jù)
        Integer data1 = cache.get("id");
        System.out.println(data1);
        cache.put("age",2);
        //getAll:和get類似 如果緩存的key不存在就自動加載默認數(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}

四、異步手動加載

  • getIfPresent: 存在時返回CompletableFuture,不存在時返回null,因此注意npe的問題
  • get(key, Function<>): 第二個參數(shù)表示加載數(shù)據(jù)的邏輯
  • put(key, CompletableFuture<>): 手動加入緩存,注意這里也不是直接加一個具體的value到緩存
  • synchronous().invalidate() : 同步清除緩存
  • getAll: 一次獲取多個緩存,同樣的是在緩存的取緩存,不在的根據(jù)第二個傳參進行加載
/**
     * 異步手動加載
     */
    @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)->{}):第二個參數(shù)表示當不存在時,初始化一個,并寫入緩存中
        CompletableFuture<Object> idData2 = asyncCache.get("id", (key) -> 5);
        System.out.println(idData2.get());
        //put(key,CompletableFuture): 手動寫入一個緩存
        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());
        }
    }

五、異步自動加載

   /**
     * 異步自動加載
     */
    @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ù)據(jù)
                        return atomicInteger.get();
                    }
                });
        //getIfPresent(key): 不存在就返回null
        CompletableFuture<Integer> idData = asyncLoadingCache.getIfPresent("id");
        System.out.println(idData);
        // get(key): 緩存不存在就使用自動加載的數(shù)據(jù)
        CompletableFuture<Integer> idData2 = asyncLoadingCache.get("id");
        System.out.println(idData2.get());
        // put:手動添加緩存
        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

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

相關文章

最新評論