Search code examples
javacachingguava

Caching with Guava


What Guava classes are suitable for thread-safe caching? I use a composed key, which gets constructed on the fly, so softKeys() makes no sense, right? I saw somewhere ConcurentLinkedHashMap, is it the way to go? Is it already in the recent release? Sorry for the chaotic way of asking...

Update

This question is pretty old and looking through he answers could possible be a waste of time. Since long there's a CacheBuilder which is the way to go.


Solution

  • Sounds like you want MapMaker.makeComputingMap, but you mention softKeys so I assume you are already familiar with that class.

    You are right about softKeys - it will not work if you compose keys on-the-fly, because softKeys causes the map to use == instead of equals for key comparison. But you should be fine with softValues and expiration, as long as there is no side-effect from recreating an evicted entry.