开发者

caching and computing map

correlation use case:

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; 
} 

Constraints: - The number of input records can go till 500K so doesn't want to use strong references. - Doesn't want to generate one-way hashes as of now (i know that if we use one-way hash then there is开发者_JAVA百科 no need to cache)

Can someone tell me how to use ComputingMap for this. I am asking this because there is a note in the javadoc that says "it uses a identity equality for weak/soft keys".


With Google Guava/Collection classes and soft or weak keys or values your keys needs to be strong references for the map to use equals() and not == to lookup cached values. If you have weak/soft keys then the lookup is done with identity so you'll always get a cache miss. So if you want the garbage collector to GC items from your cache then you'll need to make the values soft or weak.

I understand Google will add an Equivalence feature in the future so you could state if you want equals() or == and not have this choice made for you by picking strong, weak or soft references.

Since your Tuple object implements equals() and hashCode(), then you just do

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜