org.castor.cache.hashbelt
Class AbstractHashbelt
public abstract
class
AbstractHashbelt
extends AbstractBaseCache
An abstract, core implementation of the hashbelt functionality; individual
implementations will differ on the underlying behavior.
A hashbelt has six important values which get set at initialization:
- containers
- The number of containers in the conveyor belt. For example: If a box
will drop off of the conveyor belt every 30 seconds, and you want a cache
that lasts for 5 minutes, you want 5 / 30 = 6 containers on the belt. Every
30 seconds, another, clean container goes on the front of the conveyor belt,
and everything in the last belt gets discarded. If not specified 10 containers
are used by default.
For systems with fine granularity, you are free to use a large number of
containers; but the system is most efficient when the user decides on a
"sweet spot" determining both the number of containers to be managed on the
whole and the optimal number of buckets in those containers for managing. This
is ultimately a performance/accuracy tradeoff with the actual discard-from-cache
time being further from the mark as the rotation time goes up. Also the number
of objects discarded at once when capacity limit is reached depends upon the
number of containers.
- capacity
- Maximum capacity of the whole cache. If there are, for example, ten
containers on the belt and the capacity has been set to 1000, each container
will hold a maximum of 1000/10 objects. Therefore if the capacity limit is
reached and the last container gets droped from the belt there are up to 100
objects discarted at once. By default the capacity is set to 0 which causes
capacity limit to be ignored so the cache can hold an undefined number of
objects.
- ttl
- The maximum time an object lifes in cache. If the are, for example, ten
containers and ttl is set to 300 seconds (5 minutes), a new container will be
put in front of the belt every 300/10 = 30 seconds while another is dropped at
the end at the same time. Due to the granularity of 30 seconds, everything just
until 5 minutes 30 seconds will also end up in this box. The default value for
ttl is 60 seconds. If ttl is set to 0 which means that objects life in cache
for unlimited time and may only discarded by a capacity limit.
- monitor
- The monitor intervall in minutes when hashbelt cache rports the current
number of containers used and objects cached. If set to 0 (default) monitoring
is disabled.
- container-class
- The implementation of org.castor.cache.hashbelt.container.Container
interface to be used for all containers of the cache. Castor provides the following
3 implementations of the Container interface.
org.castor.cache.hashbelt.container.FastIteratingContainer
org.castor.cache.hashbelt.container.MapContainer
org.castor.cache.hashbelt.container.WeakReferenceContainer
If not specified the MapContainer will be used as default.
- reaper-class
- Specific reapers yield different behaviors. The GC reaper, the default,
just dumps the contents to the garbage collector. However, custom
implementations may want to actually do something when a bucket drops off the
end; see the javadocs on other available reapers to find a reaper strategy
that meets your behavior requirements. Apart of the default
org.castor.cache.hashbelt.reaper.NullReaper we provide 3 abstract
implementations of org.castor.cahe.hashbelt.reaper.Reaper interface:
org.castor.cache.hashbelt.reaper.NotifyingReaper
org.castor.cache.hashbelt.reaper.RefreshingReaper
org.castor.cache.hashbelt.reaper.ReinsertingReaper
to be extended by your custom implementation.
Since: 1.0
Version: $Revision: 6320 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
Author: Gregory Block Ralf Joachim
public static final int DEFAULT_CAPACITY
Default capacity of cache.
public static final int DEFAULT_CONTAINERS
Default number of containers for cache.
public static final Class DEFAULT_CONTAINER_CLASS
Default container class.
public static final int DEFAULT_MONITOR
Default monitor interval of cache in minutes.
public static final Class DEFAULT_REAPER_CLASS
Default reaper class.
public static final int DEFAULT_TTL
Default ttl of cache in seconds.
public static final String PARAM_CAPACITY
Mapped initialization parameter: capacity
public static final String PARAM_CONTAINERS
Mapped initialization parameter: containers
public static final String PARAM_CONTAINER_CLASS
Mapped initialization parameter: container-class
public static final String PARAM_MONITOR
Mapped initialization parameter: monitor
public static final String PARAM_REAPER_CLASS
Mapped initialization parameter: reaper-class
public static final String PARAM_TTL
Mapped initialization parameter: ttl
public final void clear()
{@inheritDoc }
See Also: java.util.Map#clear()
public final void close()
{@inheritDoc }
See Also: close
public final boolean containsKey(Object key)
{@inheritDoc }
See Also: java.util.Map#containsKey(java.lang.Object)
public final boolean containsValue(Object value)
{@inheritDoc }
See Also: java.util.Map#containsValue(java.lang.Object)
public final Set entrySet()
{@inheritDoc }
See Also: java.util.Map#entrySet()
public final int getCapacity()
Get real capacity of this cache.
Returns: Real capacity of this cache.
protected final Object getObjectFromCache(Object key)
Get object currently associated with given key from cache. Take care to acquire a
read or write lock before calling this method and release the lock thereafter.
Parameters: key The key to return the associated object for.
Returns: The object associated with given key.
public final int getTTL()
Get real ttl of this cache.
Returns: Real ttl of this cache.
public final void initialize(Properties params)
public final boolean isEmpty()
{@inheritDoc }
See Also: java.util.Map#isEmpty()
public final Set keySet()
{@inheritDoc }
See Also: java.util.Map#keySet()
Get reference to the ReadWriteLock of this cache instance.
Returns: ReadWriteLock to synchronize access to cache.
protected final Object putObjectIntoCache(Object key, Object value)
Put given value with given key in cache. Return the object previously associated
with key. Take care to acquire a write lock before calling this method and release
the lock thereafter.
Parameters: key The key to associate the given value with. value The value to associate with given key.
Returns: The object previously associated with given key. null
will
be returned if no value has been associated with key.
protected final Object removeObjectFromCache(Object key)
Remove any available association for given key. Take care to acquire a write lock
before calling this method and release the lock thereafter.
Parameters: key The key to remove any previously associate value for.
Returns: The object previously associated with given key. null
will
be returned if no value has been associated with key.
public final int size()
{@inheritDoc }
See Also: java.util.Map#size()
public final Collection values()
{@inheritDoc }
See Also: java.util.Map#values()
Intalio Inc. (C) 1999-2006. All rights reserved http://www.intalio.com