Caffeine缓存的使用

1.springboot集成Caffeine

    <!-- caffeine缓存依赖包-->
        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
            <version>2.9.3</version>
        </dependency>

2.Cache 模式

1)普通使用

  // 普通缓存
        cache = Caffeine.newBuilder()
                .expireAfterWrite(10, TimeUnit.MINUTES)
                .maximumSize(1000)  // 设置缓存的最大容量
                .build();
// 查找一个缓存元素, 没有查找到的时候返回null
MyObject graph = cache.getIfPresent(key);
// 查找缓存,如果缓存不存在则生成缓存元素,  如果无法生成则返回null
graph = cache.get(key, k -> userService.getUserCache((Integer) key));
// 添加或者更新一个缓存元素
cache.put(key, graph);
// 移除一个缓存元素
cache.invalidate(key);

2)同步缓存

  // 同步自动加载缓存
        autoCache = Caffeine.newBuilder()
                .expireAfterWrite(10, TimeUnit.MINUTES)  // 设置写入后的过期时间
                .maximumSize(1000)  // 设置缓存的最大容量
                .build(key -> userService.getUserCache((Integer) key));
            //缓存未命中会自动调用当前方法,将数据库获取到的数据缓存到Caffeine

graph = cache.get(key);
// 添加或者更新一个缓存元素
cache.put(key, graph);
// 移除一个缓存元素
cache.invalidate(key);

3)异步缓存

// 异步加载缓存
        asyncCache = Caffeine.newBuilder()
                .expireAfterWrite(10, TimeUnit.MINUTES)  // 设置写入后的过期时间
                .maximumSize(1000)  // 设置缓存的最大容量
                .buildAsync().synchronous();

// 查找缓存,如果缓存不存在则生成缓存元素,  如果无法生成则返回null
graph = cache.get(key, k -> userService.getUserCache((Integer) key));
// 添加或者更新一个缓存元素
cache.put(key, graph);
// 移除一个缓存元素
cache.invalidate(key);

4.异步自动缓存

    // 异步自动加载缓存
        autoAsyncCache = Caffeine.newBuilder()
                .expireAfterWrite(10, TimeUnit.MINUTES)  // 设置写入后的过期时间
                .maximumSize(1000)  // 设置缓存的最大容量
                .buildAsync(key ->
                    // 异步加载缓存逻辑
                    userService.getUserCache((Integer)key)
                );
graph = cache.get(key);
// 添加或者更新一个缓存元素
cache.put(key, graph);
// 移除一个缓存元素
cache.invalidate(key);

5.自定义每个缓存过期时间

  //首先自定义过期时间
        expireAfterCache = Caffeine.newBuilder().expireAfter(new Expiry<Object, Object>() {
            @Override
            public long expireAfterCreate(@NonNull Object key, @NonNull Object value, long currentTime) {
                return currentTime;
            }

            @Override
            public long expireAfterUpdate(@NonNull Object key, @NonNull Object value, long currentTime, @NonNegative long currentDuration) {
                return currentDuration;
            }

            @Override
            public long expireAfterRead(@NonNull Object key, @NonNull Object value, long currentTime, @NonNegative long currentDuration) {
                return currentDuration;
            }
        }).maximumSize(1000).build();

// 添加或者更新一个缓存元素
 expireAfterCache.policy().expireVariably().ifPresent(p->{
//为每个key设置不同的过期时间,可自行封装下
            p.put("cs","123",5,TimeUnit.SECONDS);
        });

// 查找一个缓存元素, 没有查找到的时候返回null
MyObject graph = expireAfterCache.getIfPresent(key);

// 移除一个缓存元素
expireAfterCache.invalidate(key);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值