net.sourceforge.rcache
Class WeakCache<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by net.sourceforge.rcache.BaseCache<K,V>
          extended by net.sourceforge.rcache.WeakCache<K,V>
Type Parameters:
K - the type of keys maintained by this cache
V - the type of cached values
All Implemented Interfaces:
java.util.Map<K,V>, Cache<K,V>

public class WeakCache<K,V>
extends BaseCache<K,V>

Memory-Sensitive Cache Based on WeakReferences

Entries in a WeakCache will be garbage collected as soon as there are no hard references left.

For certain applications, this garbage collection policy may be too aggressive, and it may be preferable to keep objects in memory until there is really a need to free space. For those cases, SoftCache will be a more sensible option.

Author:
Rodrigo Ruiz
See Also:
WeakReference

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface net.sourceforge.rcache.Cache
Cache.Operation
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
 
Fields inherited from interface net.sourceforge.rcache.Cache
DEFAULT_CONCURRENCY_LEVEL, DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR
 
Constructor Summary
WeakCache()
          Default constructor.
WeakCache(int initialCapacity)
          Creates an instance with a custom initial capacity.
WeakCache(int initialCapacity, float loadFactor)
          Creates an instance with a custom initial capacity and load factor.
WeakCache(int initialCapacity, float loadFactor, int concurrencyLevel)
          Creates an instance with custom capacity and concurrency level.
 
Method Summary
protected  KeyedReference<K,V> createRef(K key, V value, java.lang.ref.ReferenceQueue<V> queue)
          Factory method for creating a new reference instance.
 
Methods inherited from class net.sourceforge.rcache.BaseCache
clear, entrySet, equals, get, hashCode, purge, put, remove, size, values
 
Methods inherited from class java.util.AbstractMap
clone, containsKey, containsValue, isEmpty, keySet, putAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

WeakCache

public WeakCache()

Default constructor.

Instances created with this constructor will have a small initial capacity and a highly restricted concurrency level.

For concurrent maps, resizing is a costly operation. Whenever possible, it is advised to specify the expected maximum size, in order to minimise the number of resizes.


WeakCache

public WeakCache(int initialCapacity)

Creates an instance with a custom initial capacity.

Parameters:
initialCapacity - The map initial capacity

WeakCache

public WeakCache(int initialCapacity,
                 float loadFactor)

Creates an instance with a custom initial capacity and load factor.

Parameters:
initialCapacity - The map initial capacity
loadFactor - The map load factor

WeakCache

public WeakCache(int initialCapacity,
                 float loadFactor,
                 int concurrencyLevel)

Creates an instance with custom capacity and concurrency level.

The concurrency level is used as a hint to select the internal Map implementation:

Parameters:
initialCapacity - The map initial capacity
loadFactor - The map load factor
concurrencyLevel - The concurrency level.
Method Detail

createRef

protected final KeyedReference<K,V> createRef(K key,
                                              V value,
                                              java.lang.ref.ReferenceQueue<V> queue)
Factory method for creating a new reference instance.

Specified by:
createRef in class BaseCache<K,V>
Parameters:
key - The key
value - The value
queue - A reference queue
Returns:
A reference instance containing the key and value


© 2007-2009 Rodrigo Ruiz
This site is hosted by