org.glassfish.pfl.basic.concurrent
Class WeakCache<K,V>

java.lang.Object
  extended by org.glassfish.pfl.basic.concurrent.WeakCache<K,V>

public abstract class WeakCache<K,V>
extends Object

A simple cache with weak keys. get may be called safely with good concurrency by multiple threads. In order to use this, some reasonable properties are expected:

Author:
ken_admin

Constructor Summary
WeakCache()
           
 
Method Summary
 void clear()
          Remove all entries from the cache.
 V get(K key)
          Return the value (if any) associated with key.
protected abstract  V lookup(K key)
          Must be implemented in a subclass.
 V remove(K key)
          Remove any value associated with the key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WeakCache

public WeakCache()
Method Detail

lookup

protected abstract V lookup(K key)
Must be implemented in a subclass. Must compute a value corresponding to a key. The computation may be fairly expensive. Note that no lock is held during this computation.

Parameters:
key - Key value for which a value must be computed.
Returns:
The resulting value.

remove

public V remove(K key)
Remove any value associated with the key.

Parameters:
key - Key to value that may be in cache.
Returns:
value from the cache, or null if none.

get

public V get(K key)
Return the value (if any) associated with key. If the value is already in the cache, only a read lock is held, so many threads can concurrently call get. If no value is in the cache corresponding to key, a new value will be computed and cached, in which case a write lock is held long enough to update the map. Note that the write lock is NOT held while the value is computed by calling the lookup method. Because of this, it is possible for redundant computation to occur when two or more thread concurrently call get on the same key which is not (yet) in the cache.

Parameters:
key -
Returns:
Value associated with the key.

clear

public void clear()
Remove all entries from the cache.



Copyright © 2013 Oracle. All Rights Reserved.