Java8 Caffeine 本地缓存

3 篇文章 0 订阅

官宣:Caffeine is a high performance, near optimal caching library.

Caffeine 是一个高性能的本地缓存组件,脱胎与Google的Guava Cache(Guava Cache ),常用来搭配 redis 搭配使用。

git地址/中文文档

https://github.com/ben-manes/caffeine

https://github.com/ben-manes/caffeine/wiki/Population-zh-CN

一、简单实现

1.1 pom依赖

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

1.2 同步加载

1.2.1 手动

    private final Cache<Long, User> cache1 = Caffeine.newBuilder().build();

    public void get1(Long id) {
        // 当且仅当key已经缓存过才可以查询到数据,不存在返回null
        User user1 = cache1.getIfPresent(id);
        // 查找缓存元素存在直接返回查询结果,不存在尝试数据库加载数据,当数据库中也不存在时返回null
        User user2 = cache1.get(id,queryId-> userMapper.selectById(queryId));
    }

1.2.2 自动

    private final LoadingCache<Long, User> cache2 = Caffeine.newBuilder()
            .build(userMapper::selectById);

    public void get2(Long id) {
        // 查找缓存元素存在直接返回查询结果,不存在尝试数据库加载数据,当数据库中也不存在时返回null
        User user = cache2.get(id);
    }

1.2 异步加载

1.2.1 手动

    private final AsyncCache<Long, User> cache3 = Caffeine.newBuilder()
            .buildAsync();

    public void get3(Long id) {
        // 当且仅当key已经缓存过才可以查询到数据,不存在返回null
        CompletableFuture<User> userFuture1 = cache3.getIfPresent(id);
        User user1= userFuture1.get();
        // 查找缓存元素存在直接返回查询结果,不存在尝试数据库加载数据,当数据库中也不存在时返回null
        CompletableFuture<User> user2 = cache3.get(id, queryId -> userMapper.selectById(queryId));
    }

1.2.2 自动

    private final AsyncLoadingCache<Long, User> cache4 = Caffeine.newBuilder()
            // 异步封装同步方法
            .buildAsync(userMapper::selectById);
            // 异步封装异步方法
            //.buildAsync((id, executor)->{//异步方法 });

    public void get4(Long id) {
        // 查找缓存元素存在直接返回查询结果,不存在尝试数据库加载数据,当数据库中也不存在时返回null
        CompletableFuture<User> user = cache4.get(id);
    }

二、详细配置

    private final Cache<Long, User> cache = Caffeine.newBuilder()
            // 软引用 弱引用配置
            .weakKeys()     // 弱引用key   【弱引用垃圾回收是直接回收,软引用垃圾回收时内存空间不足时回收】
            .weakValues()   // 弱引用value
            .softValues()   // 软引用value
            // 容量配置
            .initialCapacity(1) // 初始化容量
            .maximumSize(100)   // 最大容量
            // 生命周期配置
            .expireAfterAccess(5, TimeUnit.MINUTES)     // 5分钟内无写入和查询缓存失效
            .expireAfterWrite(5, TimeUnit.MINUTES)      // 缓存写入后过期时间 5分钟
            .refreshAfterWrite(10, TimeUnit.MINUTES)    // 缓存写入后刷新时间 1分钟

            .build();          // 同步缓存容器
            // .executor()     // 自定义异步处理器(线程池)
            // .buildAsync();  // 异步缓存容器

    public void get(Long id) {
        // 增
        cache.put(id,new User(id,"zhangsan"));

        // 删
        cache.invalidate(id);  // 失效制定key
        cache.invalidateAll(); // 失效所有key
        cache.cleanUp();       // 快速清除失效key   

        // 改
        cache.put(id, new User(id,"lisi"));

        // 查
        cache.getIfPresent(id);                                        // 只查询
        cache.get(id,queryid-> doctorMapper.selectById(queryid));      // 查询新增(缓存无时新增)
        cache.asMap();
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值