-
Since garbage collection depends only on identity equality, this causes the whole cache to use identity (==) equality to compare keys, instead of equals ( ) . why can't use equals(). Multiple object corresponding one keys data. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This is a carry over from Guava, where @kevinb9n explained the rational. There can be confusions because when the key that created the entry is GC'd then the entry is discarded. If you expect to find the entry by another equivalent key then your application may not hold the key and will observe more cache misses. In these cases, it may make more sense to use The primary case where #344 does propose offering pluggable equivalences in Caffeine. That will likely happen someday as we can't target a single company's code base and instead should more strongly favor not blocking developers from getting their work done. That feature hasn't been implemented yet and is not being actively worked on. |
Beta Was this translation helpful? Give feedback.
This is a carry over from Guava, where @kevinb9n explained the rational. There can be confusions because when the key that created the entry is GC'd then the entry is discarded. If you expect to find the entry by another equivalent key then your application may not hold the key and will observe more cache misses. In these cases, it may make more sense to use
weakValues
.The primary case where
weakKeys
with equality may make sense is for a weak interner. This is what Guava does inInterners.weak()
. The explicit contract of this interface is to discard duplicates and reuse a single instance from the cache (Flyweight pattern). In this case Guava uses a package-privatekeyEquivalence
feature.…