Caffeine设置10秒失效,为啥2秒就失效了? 有人了解吗

机器人-1 发布于 2019/09/12 11:28
阅读 3K+
收藏 0
public class CacheTest {
    
    private static Cache<String, Object> manualCache = Caffeine.newBuilder()
            .expireAfterWrite(10, TimeUnit.MILLISECONDS)
            .maximumSize(2)
            .build();
    
    public static void main(String[] args) throws InterruptedException {
        System.out.println(new Date());
        manualCache.get("333", k->"333");
        boolean unclear = true;
        while (unclear) {
            if (manualCache.getIfPresent("333") == null) {
                unclear = false;
            }
            System.out.println(new Date());
            Thread.sleep(1000);
        }
        System.out.println("cache is cleared end.");
    }
}

源码如上,打印如下:

Thu Sep 12 11:24:04 CST 2019
Thu Sep 12 11:24:04 CST 2019
Thu Sep 12 11:24:05 CST 2019
cache is cleared end.

 

加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部