SpringBoot使用Caffeine实现内存缓存示例详解

 更新时间:2023年06月10日 11:04:17   作者:qinxun2008081  
caffeine提供了四种缓存策略:分别为手动加载、自动加载、异步手动加载、异步自动加载,这篇文章主要介绍了SpringBoot使用Caffeine实现内存缓存,需要的朋友可以参考下

一、引入依赖

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

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<Object, Object> 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<String, Integer> cache = Caffeine.newBuilder()
                // 设置数据写入2分钟后过期
                .expireAfterWrite(2, TimeUnit.MINUTES)
                // 设置最多缓存数量
                .maximumSize(50)
                .build(new CacheLoader<String, Integer>() {
                    @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<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<>): 第二个参数表示加载数据的逻辑
  • put(key, CompletableFuture<>): 手动加入缓存,注意这里也不是直接加一个具体的value到缓存
  • synchronous().invalidate() : 同步清除缓存
  • getAll: 一次获取多个缓存,同样的是在缓存的取缓存,不在的根据第二个传参进行加载
/**
     * 异步手动加载
     */
    @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)->{}):第二个参数表示当不存在时,初始化一个,并写入缓存中
        CompletableFuture<Object> 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<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 {
                        // 缓存不存在的时候 自动加载这个数据
                        return atomicInteger.get();
                    }
                });
        //getIfPresent(key): 不存在就返回null
        CompletableFuture<Integer> idData = asyncLoadingCache.getIfPresent("id");
        System.out.println(idData);
        // get(key): 缓存不存在就使用自动加载的数据
        CompletableFuture<Integer> idData2 = asyncLoadingCache.get("id");
        System.out.println(idData2.get());
        // put:手动添加缓存
        asyncLoadingCache.put("age", CompletableFuture.supplyAsync(() -> 20));
        //asMap:返回所有缓存的数据
        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实现内存缓存的文章就介绍到这了,更多相关SpringBoot内存缓存内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 基于Java8并行流(parallelStream)的注意点

    基于Java8并行流(parallelStream)的注意点

    这篇文章主要介绍了Java8并行流(parallelStream)的注意点,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • SpringBoot后端接口的实现(看这一篇就够了)

    SpringBoot后端接口的实现(看这一篇就够了)

    这篇文章主要介绍了SpringBoot后端接口的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • java 字符串分割的三种方法(总结)

    java 字符串分割的三种方法(总结)

    下面小编就为大家带来一篇java 字符串分割的三种方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • Java 利用递归实现链表的归并排序

    Java 利用递归实现链表的归并排序

    这篇文章主要介绍了Java 如何利用递归实现链表的归并排序,帮助大家更好的理解和使用Java,感兴趣的朋友可以了解下
    2020-09-09
  • 解决	Spring RestTemplate post传递参数时报错问题

    解决 Spring RestTemplate post传递参数时报错问题

    本文详解说明了RestTemplate post传递参数时报错的问题及其原由,需要的朋友可以参考下
    2020-02-02
  • 利用SpringBoot实现多数据源的两种方式总结

    利用SpringBoot实现多数据源的两种方式总结

    关于动态数据源的切换的方案有很多,核心只有两种,一种是构建多套环境,另一种是基于spring原生的AbstractRoutingDataSource切换,这篇文章主要给大家介绍了关于利用SpringBoot实现多数据源的两种方式,需要的朋友可以参考下
    2021-10-10
  • icePDF去水印的方法(推荐)

    icePDF去水印的方法(推荐)

    下面小编就为大家带来一篇icePDF去水印的方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • java数据结构与算法之奇偶排序算法完整示例

    java数据结构与算法之奇偶排序算法完整示例

    这篇文章主要介绍了java数据结构与算法之奇偶排序算法,较为详细的分析了奇偶算法的原理并结合完整示例形式给出了实现技巧,需要的朋友可以参考下
    2016-08-08
  • Java 超详细讲解hashCode方法

    Java 超详细讲解hashCode方法

    在 Java 应用程序执行期间,在同一对象上多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是对象上 equals 比较中所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致
    2022-04-04
  • 解决在启动eclipse的tomcat进行访问时出现404问题的方法

    解决在启动eclipse的tomcat进行访问时出现404问题的方法

    这篇文章主要介绍了解决在启动eclipse的tomcat进行访问时出现404问题的方法,感兴趣的小伙伴们可以参考一下
    2016-04-04

最新评论