net.sourceforge.rcache
Class SoftCache<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.SoftCache<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 SoftCache<K,V>
extends BaseCache<K,V>

Memory-Sensitive Cache Based on SoftReferences

Entries in a SoftCache can be garbage collected if, and only if, the JVM considers that more memory is needed. This means that all SoftCache instances will be effectively cleared before an OutOfMemory error is thrown.

For certain applications, this class may be too much memory-consuming. If the programmer only needs to enforce that a single instance is used for a given key, and it is not important to keep this instance afterwards, WeakCache may be a better option.

Author:
Rodrigo Ruiz
See Also:
SoftReference

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
SoftCache()
          Default constructor.
SoftCache(int initialCapacity)
          Creates an instance with a custom initial capacity.
SoftCache(int initialCapacity, float loadFactor)
          Creates an instance with a custom initial capacity and load factor.
SoftCache(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

SoftCache

public SoftCache()

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.


SoftCache

public SoftCache(int initialCapacity)

Creates an instance with a custom initial capacity.

Parameters:
initialCapacity - The map initial capacity

SoftCache

public SoftCache(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

SoftCache

public SoftCache(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