番石榴缓存魔术小姐

Guava cache magic miss

请帮助我在这里观察到这个魔法:

Map<String, RenditionMeta> map = cache.asMap();
        System.out.println("Before iterating: " + map.containsKey(objectId));
        for(String s : map.keySet()) {
            if(s.equals(objectId)) {
                System.out.println(s + " equals " + objectId + ":" + s.equals(objectId) + "-" + map.containsKey(objectId));

                System.out.println(objectId.hashCode());
                System.out.println(s.hashCode());
            }
        }

输出:

Before iterating: false
09009e5d805f6b0b equals 09009e5d805f6b0b:true-false
1453886923
1453886923

有人可以解释一下上面的 "true-false" 是如何实现的吗?

缓存定义为

this.cache =  CacheBuilder.newBuilder()
                .concurrencyLevel(4)
                .weakKeys()
                .maximumSize(10000)
                .expireAfterWrite(10, TimeUnit.MINUTES)
                .build();

当您使用弱键选项时,Guava 缓存对键使用身份相等性,因此您会观察到行为。 CacheBuilder#weakKeys