1.基础用法:
导入依赖:
<dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
            <version>2.9.3</version>
</dependency>
使用:
 @Test
    public void testCaffeine(){
        Cache<String, String> cache = Caffeine.newBuilder().build();

        //存入数据
        cache.put("name","zhangsan");

        //获取数据
        String name = cache.getIfPresent("name"); //取出存在的数据,不存在就返回null
        System.out.println(name);

        //取数据,未取到就查询数据库
        String name1 = cache.get("name1", key -> {
            //根据key查询数据库
            return "haha";//这里应该返回查询数据库的逻辑,这里简略直接返回固定值
        });
        System.out.println(name1);
    }
-------------
2.caffeine三种缓存驱逐策略:
1.基于缓存数量上限:
Cache<String, String> cache = Caffeine.newBuilder()
                .maximumSize(10) //设置缓存最大数量为10
                .build();
2.基于缓存时间:
Cache<String, String> cache = Caffeine.newBuilder()
                .expireAfterWrite(10, TimeUnit.SECONDS) //缓存超过10秒没被访问就过期
                .build();
3.基于GC功能(性能较差,不建议使用)
--------------