com.ls.util.cache
Class CacheTimer

java.lang.Object
  |
  +--com.ls.util.cache.CacheTimer

public class CacheTimer
extends Object

This class is used for caching data that will be used in different class.

There is an internal static map defined to store all cache types. User can define his/her own type in the constructor. If this type "cacheId" is already defined in the cache list then this will be used, else a new cache for the required "cacheId" will be created. All cache types are synchronized. User can access any data which is inserted by another one.

All information are stored with a time stamp. When user wants to retrieve these data, the age of the data is checked. If it is an old data than system returns null. If user does not want to use this property then has to set the maxDuration variable to NONTERMINATING_LIFETIME in the constructor or use setMaxDuration(long) method.

Version:
$Revision: 1.8 $
Author:
Last modified by $Author: MFehrenbach $

Field Summary
static long DEFAULT_MAX_LIFETIME
          The default maximum lifetime for an object in the hash (60 seconds)
static long NONTERMINATING_LIFETIME
          If users sets the maxDuration to this value then the the age of the variable will not be checked while retrieving data
 
Constructor Summary
CacheTimer(String cacheId)
          Constructor, that creates the hash table for a specific "cacheId".
CacheTimer(String cacheId, long newMaxDuration)
          Constructor, that creates the hash table and sets the life time of a variable.
 
Method Summary
 void clear()
          Clears all data in the hash
 void clearOldData()
          Clears all data which are old (duration > maxDuration)
 Object get(Object key)
          Retrieves a data from the internal hash
 StringBuffer getCacheStatus()
          Returns the cache status and some statistics
 void put(Object key, Object value)
          Inserts a data to the hash.
 Object remove(Object key)
          Removes a data from the internal hash
 void setMaxDuration(long newMaxDuration)
          Sets the maximum duration time to a new value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_MAX_LIFETIME

public static final long DEFAULT_MAX_LIFETIME
The default maximum lifetime for an object in the hash (60 seconds)

NONTERMINATING_LIFETIME

public static final long NONTERMINATING_LIFETIME
If users sets the maxDuration to this value then the the age of the variable will not be checked while retrieving data
Constructor Detail

CacheTimer

public CacheTimer(String cacheId)
Constructor, that creates the hash table for a specific "cacheId". If the this cache specified by "cacheId" parameter is already created then this will be used. Else a new cache for this "cacheId" will be created. This constructor also sets the default max time value for the values to DEFAULT_MAX_LIFETIME.
Parameters:
cacheId - The ID of the cache.

CacheTimer

public CacheTimer(String cacheId,
                  long newMaxDuration)
Constructor, that creates the hash table and sets the life time of a variable. The unit of the newMaxDuration parameter is in miliseconds. If user specifies a negativ value or NONTERMINATING_LIFETIME then the maxduration values will be set to NONTERMINATING_LIFETIME.In this case the age of the variable will not be checked while retrieving data
Parameters:
cacheId - The ID of the cache.
newMaxDuration - The maximum duration time for the variables in the cache
Method Detail

setMaxDuration

public void setMaxDuration(long newMaxDuration)
Sets the maximum duration time to a new value. The default value for this parameter is DEFAULT_MAX_LIFETIME. The unit of the newMaxDuration parameter is miliseconds. If user specifies a negativ value then the maxduration values will be set to NONTERMINATING_LIFETIME. In this case the age of the variable will not be checked while retrieving data.
Parameters:
newMaxDuration - The maximum duration time for the variables in the cache

remove

public Object remove(Object key)
Removes a data from the internal hash
Parameters:
key - The key of the data that will be deleted
Returns:
the deleted value. In any error (key not found, etc.) null.

get

public Object get(Object key)
Retrieves a data from the internal hash
Parameters:
key - The key of the data that will be retrieved
Returns:
the requested data. In any error, null.

clear

public void clear()
Clears all data in the hash

clearOldData

public void clearOldData()
Clears all data which are old (duration > maxDuration)

put

public void put(Object key,
                Object value)
Inserts a data to the hash. The current time stamp is also attached to the data.
Parameters:
key - The key of the data
value - The vaue of the data

getCacheStatus

public StringBuffer getCacheStatus()
Returns the cache status and some statistics
Returns:
the cache status and some statistics