gpt4 book ai didi

guava - 缓存和计算图

转载 作者:行者123 更新时间:2023-12-02 00:42:50 27 4
gpt4 key购买 nike

关联用例:

read input 

if (correlation-id is already generated for this input)
{
lookup the correlation-id from the cache;
return correlation-id;
}

else
{
generate the correlation-id;
cache it;
return correlation-id;
}

约束:- 输入记录的数量可以达到 500K 所以不想使用强大的引用。- 目前不想生成单向哈希(我知道如果我们使用单向哈希就不需要缓存了)

谁能告诉我如何为此使用 ComputingMap。我在问这个因为 javadoc 中有一条注释说“它使用身份弱键/软键的相等性”。

最佳答案

对于 Google Guava/Collection 类和软键或弱键或值,您的键需要是映射的强引用才能使用 equals() 而不是 == 来查找缓存值。如果你有弱/软 key ,那么查找是用身份完成的,所以你总是会遇到缓存未命中。因此,如果您希望垃圾收集器从您的缓存中对项目进行 GC,那么您需要将这些值设置为 soft 或 weak。

我知道 Google 将来会添加等效功能,这样您就可以声明您是想要 equals() 还是 ==,而不是通过选择强引用、弱引用或软引用来为您做出这个选择。

既然你的 Tuple 对象实现了 equals() 和 hashCode(),那么你就可以了

new MapMaker()
.softValues()
.makeComputingMap(new Function<Tuple,String>() {
public String apply(Tuple t) {
// generate the correlation-id
}
});

关于guava - 缓存和计算图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2068256/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com