我有一个像这样的加载缓存:
MyCacheLoader loader=new MyCacheLoader();
MyRemovalListener listener=new MyRemovalListener();
LoadingCache<String, String> myCache = CacheBuilder.newBuilder()
.concurrencyLevel(10)
.weakKeys()
.maximumSize(2)
.expireAfterWrite(120, TimeUnit.SECONDS)
.removalListener(listener)
.build(loader);
但是当我做这样的事情来测试时:
System.out.println(myCache.get("100"));
System.out.println(myCache.get("103"));
System.out.println(myCache.get("110"));
System.out.println(myCache).get("111"));
Thread.sleep(230000);
System.out.println(myCache.size());
我仍然得到 2 而不是零。为什么 ?超过 120 秒后,如果没有错,我应该得到零尺寸?
MyCacheLoader
public class MyCacheLoader extends CacheLoader<String,String> {
@Override
public String load(String key) throws Exception {
return key;
}
}
MyRemovalListener
public class MyRemovalListener implements RemovalListener<String,String> {
Logger logger = LoggerFactory.getLogger(MyRemovalListener.class);
@Override
public void onRemoval(RemovalNotification<String, String> removalNotification) {
logger.info("Message with message Id ("+
removalNotification.getKey()+ ") is removed.");
System.out.println("Message with message Id ("+
removalNotification.getKey()+ ") is removed.");
}
}