具有弱/软使用的Guava CacheBuilder或MapMaker [英] Guava CacheBuilder or MapMaker with Weak/Soft usage

查看:229
本文介绍了具有弱/软使用的Guava CacheBuilder或MapMaker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不习惯在Java中处理Soft和Weak引用,但我理解这个原理,因为我习惯于处理像Gemfire这样的数据网格,它在内存已满时为hdd功能提供溢出,可能使用软引用或类似的东西我想。

I'm not used to deal with Soft and Weak references in Java but i understand the principle since i'm used to deal with datagrids like Gemfire, which provide overflow to hdd features when memory is full, probably using soft references or something similar i guess.

我不理解的是,它在Guava中提供的方法是使键变软/弱,值为软/弱。

What i don't understand in Guava is that it provides methods to make the keys soft/weak, and the values soft/weak.

我只是想知道用非软值创建软键的例子是什么意思?
我的意思是,当开始收集软引用时,我们无法通过其键找到条目,那么我们为什么要将这些值保留在地图中?

I just wonder what's the point of creating soft keys with non-soft values for exemple? I mean, when the soft references start to be collected, we can't find anymore the entry by its key, so why would we like the values to stay in the map?

有人可以给我们一些用例:

Can someone give us some usecases with:


  • 弱键/软值

  • 弱键/正常值

  • 软键/弱值

  • 软键/正常值

  • Weak key / soft values
  • Weak key / normal values
  • Soft key / weak values
  • Soft key / normal values

谢谢

编辑
我不确定我的问题是否足够精确,所以我想知道的是:

Edit I'm not sure my question is precise enough so what i'd like to know is:


  • 收集密钥时(弱) / soft),当收集一个值(弱/软)时,值(非弱/软)

  • 会发生什么,键会发生什么?

  • 缺少部分(键或值)的条目是否保留在缓存中?

  • 如果您希望此类条目保留在缓存中,是否有任何用例。

  • when a key is collected (weak/soft), what happens to the value (non weak/soft)
  • when a value is collected (weak/soft), what happens to the key
  • Are entries with a missing part (key or value) kept in the cache?
  • And is there any usecase when you want such entries to stay in the cache.

编辑:
正如Kevin Bourillon的回答所讨论的,最后我想我明白为什么使用软键并不意味着什么。
原因如下:

As discussed on Kevin Bourillon's answer, finally i think i understand why using soft keys doesn't mean anything. Here's why:

static class KeyHolder {
    final private String key;
    public KeyHolder(String key) {
        this.key = key;
    }
    public String getKey() {
        return key;
    }
    @Override
    public boolean equals(Object o) {
        KeyHolder that = (KeyHolder)o;
        boolean equality = this.getKey().equals(that.getKey());
        return equality;
    }
    @Override
    public int hashCode() {
        return key != null ? key.hashCode() : 0;
    }
    @Override
    public String toString() {
        return "KeyHolder{" +
                "key='" + key + '\'' +
                '}';
    }
}

public static void main(String[] args) {
    System.out.println("TESTING WEAK KEYS");
    testMap( new MapMaker().weakKeys().<KeyHolder,String>makeMap() );


    System.out.println("\n\n");
    System.out.println("TESTING SOFT KEYS");
    testMap(new MapMaker().softKeys().<KeyHolder, String>makeMap());


    System.out.println("\n\n");
    System.out.println("TESTING SOFT REFERENCES");
    KeyHolder key1 = new KeyHolder("toto");
    KeyHolder key2 = new KeyHolder("toto");
    SoftReference<KeyHolder> softRef1 = new SoftReference<KeyHolder>(key1);
    SoftReference<KeyHolder> softRef2 = new SoftReference<KeyHolder>(key2);
    System.out.println( "equals keys? " + key1.equals(key2) );
    System.out.println( "equals ref? " + softRef1.equals(softRef2) );
}

private static void testMap(Map<KeyHolder,String> map) {
    KeyHolder strongRefKey = new KeyHolder("toto");
    KeyHolder noStrongRefKey = new KeyHolder("tata");
    map.put(strongRefKey,"strongRef");
    map.put(noStrongRefKey,"noStrongRefKey");
    // we replace the strong reference by another key instance which is equals
    // this could happen for exemple in case of serialization/deserialization of the key
    noStrongRefKey = new KeyHolder("tata");
    System.gc();
    System.out.println( "strongRefKey = " + map.get(strongRefKey) );
    System.out.println( "noStrongRefKey = " + map.get(noStrongRefKey) );
    System.out.println( "keyset = " + map.keySet() );
}

此代码生成输出:

TESTING WEAK KEYS
strongRefKey = strongRef
noStrongRefKey = null
keyset = [KeyHolder{key='toto'}]



TESTING SOFT KEYS
strongRefKey = strongRef
noStrongRefKey = null
keyset = [KeyHolder{key='tata'}, KeyHolder{key='toto'}]



TESTING SOFT REFERENCES
toto == toto -> true
equals keys? true
equals ref? false

如您所见,使用(已弃用)软键映射,KeyHolder包含tata仍然存在于地图中。
但请注意,我仍然无法使用新创建的密钥找到我的条目 new KeyHolder(tata);
这是因为,我的键有意义等于,但是它们周围的引用包装器不等于因为它们的等号方法在Guava中没有被覆盖!
在这种情况下,是的,softKeys并不意味着任何事情,因为你绝对需要保持对该密钥的身份引用才能检索它。

As you can see, with the (deprecated) soft keys map, the KeyHolder containing "tata" still exists in the map. But notice that i'm still not able to find my entry with a newly created key "new KeyHolder("tata");" This is because, my keys are meaningfully equals, but the reference wrappers around them are not equals because their equals method is not overriden in Guava! In this case, yes, softKeys doesn't mean anything since you absolutly need to keep an identity reference to that key to be able to retrieve it.

推荐答案

softKeys 永远不会有意义,所以我们删除了该方法。 softValues 是软refs有意义的唯一方法,假设值实例在缓存之外的其他方式也不可达。

softKeys never makes sense, so we removed the method. softValues is the only way soft refs make sense, assuming the value instances aren't also reachable in other ways outside the cache.

然后使用 weakKeys 基本上归结为你是否想要密钥的身份相等。如果密钥重写等于,并且您需要 等式行为,则无法使用它。如果你想要身份,那么 weakKeys 就是你如何得到它的,这也是有道理的,因为一旦所有其他对密钥的引用都被GC了,就没有办法了无论如何要查找该条目,所以它也可能被删除。

Then usage of weakKeys basically boils down to whether you want identity equality for keys. If the key overrides equals and you need that equality behavior, you can't use it. If you want identity, then weakKeys is how you get that, and it also makes sense because once all other references to the key have been GC'd, there would be no way to look up that entry anyway, so it might as well be removed.

我实际上并不完全清楚何时 weakValues 很有用,并且会调查它。可能是 weakKeys 不是一个选项(例如,整数键),以及值的位置通常通过其他方式强烈引用,比如某种会话对象,但当 对象消失时,它表示没有人会在缓存中再找这个。但是,当我这样说时,它似乎有点牵强。

I actually am not entirely clear on when weakValues is useful, and was going to look into it. It would probably be a case where weakKeys is not an option (say, Integer keys), and where the values are ordinarily strongly referenced through other means, like some sort of session object, but when that object goes away, it signifies that no one will be looking for this in the cache anymore. It seems slightly farfetched when I put it that way, though.

这篇关于具有弱/软使用的Guava CacheBuilder或MapMaker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆