返回

提问 添加收藏

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

机器人-1 发布于 2019年09月12日 (共有1个回帖)

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.

 

Caffeine$tag.name()$tag.name()
文明上网,理性发言
客户端 Android iPhone WP7