SpringBoot使用Caffeine实现内存缓存

一、引入依赖



    com.github.ben-manes.caffeine
    caffeine

caffeine提供了四种缓存策略:分别为手动加载、自动加载、异步手动加载、异步自动加载。

二、手动加载

时间:

expireAfterWrite: 基于写入时间

expireAfterAccess:基于访问时间

expireAfter:可以根据读更新写入来调整有效期

权重:

maximumWeight:基于权重的容量策略,主要应用于缓存中的元素存在不同的权重场景。

maximumSize:基于容量策略,当缓存内元素个数超过时,通过基于就近度和频率的算法来驱逐掉不会再被使用到的元素

查询缓存&添加缓存:

asMap: 获取所有缓存数据

getIfPresent(key):如果存在这个key的缓存数据就返回,如果不存在就返回null

get(key,(key)->{缓存初始化}):如果指定key的缓存不存在,就设置缓存的初始化数据并返回。

添加缓存:

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 cache = Caffeine.newBuilder()
                // 设置写入2分钟后失效
                .expireAfterWrite(2, TimeUnit.MINUTES)
                // 设置最多缓存数量
                .maximumSize(50)
                .build();
        // getIfPresent:获取指定key的数据 不存在返回null
        Object id = cache.getIfPresent("id");
        System.out.println(id);


        // get: 如果数据不存在就使用lambda表达式返回的数据设置缓存 并返回缓存数据
        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: 获取缓存所有数据
        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 cache = Caffeine.newBuilder()
                // 设置数据写入2分钟后过期
                .expireAfterWrite(2, TimeUnit.MINUTES)
                // 设置最多缓存数量
                .maximumSize(50)
                .build(new CacheLoader() {
                    @Override
                    public @Nullable Integer load(@NonNull String s) throws Exception {
                        // 如果缓存的数据不存在 就返回这个自动加载的数据
                        return atomicInteger.get();
                    }
                });

        // get:如果缓存的数据不存在 就使用自动加载的数据
        Integer data1 = cache.get("id");
        System.out.println(data1);

        cache.put("age",2);


        //getAll:和get类似 如果缓存的key不存在就自动加载默认数据 否则返回缓存数据
        List 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<>): 第二个参数表示加载数据的逻辑
  • put(key, CompletableFuture<>): 手动加入缓存,注意这里也不是直接加一个具体的value到缓存
  • synchronous().invalidate() : 同步清除缓存
  • getAll: 一次获取多个缓存,同样的是在缓存的取缓存,不在的根据第二个传参进行加载
/**
     * 异步手动加载
     */
    @Test
    void test3() throws ExecutionException, InterruptedException {
        AsyncCache asyncCache = Caffeine.newBuilder()
                .expireAfterWrite(2, TimeUnit.MINUTES)
                .maximumSize(50)
                .buildAsync();
        // getIfPresent(key): 不存在就返回null
        CompletableFuture idData = asyncCache.getIfPresent("id");
        System.out.println(idData);

        //get(key,(key)->{}):第二个参数表示当不存在时,初始化一个,并写入缓存中
        CompletableFuture idData2 = asyncCache.get("id", (key) -> 5);
        System.out.println(idData2.get());

        //put(key,CompletableFuture): 手动写入一个缓存
        asyncCache.put("id", CompletableFuture.supplyAsync(() -> 10));

        //asMap:返回所有缓存的数据
        ConcurrentMap<@NonNull Object, @NonNull CompletableFuture> map = asyncCache.asMap();
        for (Map.Entry> 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> map1 = asyncCache.asMap();
        for (Map.Entry> entry : map1.entrySet()) {
            System.out.println(entry.getKey() + "==>" + entry.getValue().get());
        }

    }

五、异步自动加载

   /**
     * 异步自动加载
     */
    @Test
    void test4() throws ExecutionException, InterruptedException {
        AtomicInteger atomicInteger = new AtomicInteger(5);
        AsyncLoadingCache asyncLoadingCache = Caffeine.newBuilder()
                .expireAfterWrite(2, TimeUnit.MINUTES)
                .maximumSize(50)
                .buildAsync(new CacheLoader() {
                    @Override
                    public @Nullable Integer load(@NonNull String s) throws Exception {
                        // 缓存不存在的时候 自动加载这个数据
                        return atomicInteger.get();
                    }
                });
        //getIfPresent(key): 不存在就返回null
        CompletableFuture idData = asyncLoadingCache.getIfPresent("id");
        System.out.println(idData);

        // get(key): 缓存不存在就使用自动加载的数据
        CompletableFuture idData2 = asyncLoadingCache.get("id");
        System.out.println(idData2.get());

        // put:手动添加缓存
        asyncLoadingCache.put("age", CompletableFuture.supplyAsync(() -> 20));

        //asMap:返回所有缓存的数据
        ConcurrentMap<@NonNull String, @NonNull CompletableFuture> map = asyncLoadingCache.asMap();
        for (Map.Entry> entry : map.entrySet()) {
            System.out.println(entry.getKey() + "==>" + entry.getValue().get());
        }

        // synchronous().invalidateAll(): 移除所有缓存
        asyncLoadingCache.synchronous().invalidateAll();

        CompletableFuture ageData = asyncLoadingCache.getIfPresent("age");
        System.out.println(ageData);
    }
null
5
id==>5
age==>20
null

你可能感兴趣的:(SpringBoot,spring,boot,缓存,java)