| java.lang.Object jdbm.helper.SoftCache
SoftCache | public class SoftCache implements CachePolicy(Code) | | Wraps a deterministic cache policy with a Level-2 cache based on
J2SE's
SoftReference soft references . Soft references allow
this cache to keep references to objects until the memory they occupy
is required elsewhere.
Since the
CachePolicy interface requires an event be fired
when an object is evicted, and the event contains the actual object,
this class cannot be a stand-alone implementation of
CachePolicy . This limitation arises because Java References
does not support notification before references are cleared; nor do
they support reaching soft referents. Therefore, this wrapper cache
aggressively notifies evictions: events are fired when the objects are
evicted from the internal cache. Consequently, the soft cache may return
a non-null object when get( ) is called, even if that
object was said to have been evicted.
The current implementation uses a hash structure for its internal key
to value mappings.
Note: this component's publicly exposed methods are not threadsafe;
potentially concurrent code should synchronize on the cache instance.
author: Dilum Ranatunga version: $Id: SoftCache.java,v 1.1 2003/11/01 13:29:27 dranatunga Exp $ |
Constructor Summary | |
public | SoftCache() Creates a soft-reference based L2 cache with a
MRU cache as
the internal (L1) cache. | public | SoftCache(CachePolicy internal) Creates a soft-reference based L2 cache wrapping the specified
L1 cache. | public | SoftCache(float loadFactor, CachePolicy internal) Creates a soft-reference based L2 cache wrapping the specified
L1 cache. |
Method Summary | |
public void | addListener(CachePolicyListener listener) Adds the specified listener to this cache. | public Enumeration | elements() Gets all the objects stored by the internal (L1) cache. | public Object | get(Object key) Gets the object cached under the specified key.
The cache is looked up in the following manner:
- The internal (L1) cache is checked.
| public void | put(Object key, Object value) Adds the specified value to the cache under the specified key. | public void | remove(Object key) Removes any object stored under the key specified. | public void | removeAll() Removes all objects in this (L2) and its internal (L1) cache. | public void | removeListener(CachePolicyListener listener) Removes a listener that was added earlier. |
SoftCache | public SoftCache()(Code) | | Creates a soft-reference based L2 cache with a
MRU cache as
the internal (L1) cache. The soft reference cache uses the
default load capacity of 1.5f, which is intended to sacrifice some
performance for space. This compromise is reasonable, since all
SoftCache.get(Object) get( )s first try the L1 cache anyway. The
internal MRU is given a capacity of 128 elements.
|
elements | public Enumeration elements()(Code) | | Gets all the objects stored by the internal (L1) cache.
an enumeration of objects in internal cache. |
get | public Object get(Object key)(Code) | | Gets the object cached under the specified key.
The cache is looked up in the following manner:
- The internal (L1) cache is checked. If the object is found, it is
returned.
- This (L2) cache is checked. If the object is not found, then
the caller is informed that the object is inaccessible.
- Since the object exists in L2, but not in L1, the object is
readded to L1 using
CachePolicy.put(ObjectObject) .
- If the readding succeeds, the value is returned to caller.
- If a cache eviction exception is encountered instead, we
remove the object from L2 and behave as if the object was
inaccessible.
Parameters: key - the key that the object was stored under. the object stored under the key specified; null if theobject is not (nolonger) accessible via this cache. |
put | public void put(Object key, Object value) throws CacheEvictionException(Code) | | Adds the specified value to the cache under the specified key. Note
that the object is added to both this and the internal cache.
Parameters: key - the (non-null) key to store the object under Parameters: value - the (non-null) object to place in the cache throws: CacheEvictionException - exception that the internal cachewould have experienced while evicting an object it currentlycached. |
remove | public void remove(Object key)(Code) | | Removes any object stored under the key specified. Note that the
object is removed from both this (L2) and the internal (L1)
cache.
Parameters: key - the key whose object should be removed |
removeAll | public void removeAll()(Code) | | Removes all objects in this (L2) and its internal (L1) cache.
|
removeListener | public void removeListener(CachePolicyListener listener)(Code) | | Removes a listener that was added earlier.
Parameters: listener - the listener to remove. |
|
|