Top Description Fields Constructors Methods
org.glassfish.pfl.basic.concurrent

public abstract Class WeakCache<K, V>

extends Object
Class Inheritance
Imports
java.util.Map, java.util.concurrent.locks.ReadWriteLock, .ReentrantReadWriteLock

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

Field Summary

Modifier and TypeField and Description
private final ReadWriteLock
private final Map<K, V>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Remove all entries from the cache.

public V

Returns:

Value associated with the key.
get
(K key)

Return the value (if any) associated with key.

protected abstract V

Returns:

The resulting value.
lookup
(K
Key value for which a value must be computed.
key
)

Must be implemented in a subclass.

public V

Returns:

value from the cache, or null if none.
remove
(K
Key to value that may be in cache.
key
)

Remove any value associated with the key.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

lockback to summary
private final ReadWriteLock lock
mapback to summary
private final Map<K, V> map

Constructor Detail

WeakCacheback to summary
public WeakCache()

Method Detail

clearback to summary
public void clear()

Remove all entries from the cache.

getback to summary
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.

Returns:V

Value associated with the key.

lookupback to summary
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:K

Key value for which a value must be computed.

Returns:V

The resulting value.

removeback to summary
public V remove(K key)

Remove any value associated with the key.

Parameters
key:K

Key to value that may be in cache.

Returns:V

value from the cache, or null if none.