Empty constructor that uses default values for initial capacity, concurrency level, and load factor
Empty constructor that uses default values for initial capacity, concurrency level, and load factor
Overloaded constructor that creates the cache with initial capacity, concurrency level, and load factor read from config
Overloaded constructor that creates the cache with initial capacity, concurrency level, and load factor read from config
Remove all keys and values from the cache
Returns true if this key is associated with a value in the cache and false otherwise.
Returns true if this key is associated with a value in the cache and false otherwise.
Optionally return the value associated with the given key
Optionally return the value associated with the given key
Returns all elements of the cache.
Returns all elements of the cache. Use this method only if you really need all of the elements. Calling it will cause all lazy values to be calculated.
Get the value associated with the given key.
Get the value associated with the given key. If no value is already associated, then associate the given value with the key and use it as the return value.
Like Scala's ConcurrentMap, the value parameter will be lazily evaluated: that is, it'll only be evaluated if there wasn't already a value associated with the given key. However, unlike Scala's ConcurrentMap, this method is a thread safe (atomic) operation.
Associate the given key with the given value.
Associate the given key with the given value. Optionally return any value previously associated with the key.
If the given key is already associated with a value, return that value.
If the given key is already associated with a value, return that value. Otherwise, associate the key with the given value and return None.
Remove the key and any associated value from the cache.
Remove the key and any associated value from the cache. Optionally return any previously associated value.
Return how many elements are in the cache
Return how many elements are in the cache
A Scala wrapper for a Java's ConcurrentHashMap (CHM). Exposes the basic underlying methods of CHM and adds a getOrElseUpdate(key, value) method that lazily evaluates the value parameter only if the key is not already present in the cache.
You may be asking, why not just use Scala's ConcurrentMap interface, which already has a getOrElseUpdate method?
val cache = new ConcurrentHashMap().asScala cache.getOrElseUpdate("foo", "bar") // BAD idea
The answer is because this method is inherited from the MapLike trait, and is NOT a thread safe (atomic) operation!
The strategy used in the class below is to wrap all values with a LazyWrapper class that only evaluates the value when explicitly accessed. In the getOrElseUpdate method, we avoid accessing the passed in value unless we know it was the one actually inserted into the cache.
For more info, see: http://boundary.com/blog/2011/05/
TODO: investigate if boundary's NonBlockingHashMap is as good as they say it is (and what tests they have to prove it).
TODO: Java-friendly API