org.jasig.portal.utils
Class SmartCache

java.lang.Object
  extended by java.util.AbstractMap
      extended by java.util.HashMap
          extended by org.jasig.portal.utils.SmartCache
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map

public class SmartCache
extends java.util.HashMap

The SmartCache class is used to store objects in memory for a specified amount of time. The time should be specified in seconds. If the time is specified as a negative value, it will be cahced indefinitely.

Version:
$Revision: 1.9 $
Author:
Ken Weiner, kweiner@unicon.net
See Also:
Serialized Form

Nested Class Summary
private  class SmartCache.ValueWrapper
           
 
Field Summary
protected  int iExpirationTimeout
           
 
Constructor Summary
SmartCache()
          Instantiate SmartCache with a default expiration timeout of one hour.
SmartCache(int iExpirationTimeout)
          Instantiate a new SmartCache.
 
Method Summary
 java.lang.Object get(java.lang.Object key)
          Get an object from the cache.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Add a new value to the cache.
 java.lang.Object put(java.lang.Object key, java.lang.Object value, long lCacheInterval)
          Add a new value to the cache
protected  void sweepCache()
          Removes from the cache values which have expired.
 
Methods inherited from class java.util.HashMap
clear, clone, containsKey, containsValue, entrySet, isEmpty, keySet, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

iExpirationTimeout

protected int iExpirationTimeout
Constructor Detail

SmartCache

public SmartCache(int iExpirationTimeout)
Instantiate a new SmartCache. Usually instances of SmartCache are declared as static. When retrieving a value from SmartCache, it will be null if the value has expired. It is up to the client to then retrieve the value and put it in the cache again. Example: import org.jasig.portal.utils.SmartCache; public class CacheClient { private static SmartCache cache = new SmartCache(3600); // This cache's values will expire in one hour public static void main (String[] args) { // Try to get a value from the cache String aKey = "exampleKey"; String aValue = (String)cache.get(aKey); if (aValue == null) { // If we are here, the value has either expired or not in the cache // so we will get the value and stuff it in the cache String freshValue = someMethodWhichReturnsAString(); // Make sure it isn't null before putting it into the cache if (freshValue != null) { cache.put(aKey, freshValue); aValue = freshValue; } } System.out.println ("Got the value: " + aValue); } }

Parameters:
iExpirationTimeout - specified in seconds

SmartCache

public SmartCache()
Instantiate SmartCache with a default expiration timeout of one hour.

Method Detail

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Add a new value to the cache. The value will expire in accordance with the cache's expiration timeout value which was set when the cache was created.

Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.HashMap
Parameters:
key - the key, typically a String
value - the value
Returns:
the previous value of the specified key in this hashtable, or null if it did not have one.

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value,
                            long lCacheInterval)
Add a new value to the cache

Parameters:
key - the key, typically a String
value - the value
lCacheInterval - an expiration timeout value, in seconds, which will override the default cache value just for this item. If a negative timeout value is specified, the value will be cached indefinitely.
Returns:
the cached object

get

public java.lang.Object get(java.lang.Object key)
Get an object from the cache.

Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.HashMap
Parameters:
key - the key, typically a String
Returns:
the value to which the key is mapped in this cache; null if the key is not mapped to any value in this cache.

sweepCache

protected void sweepCache()
Removes from the cache values which have expired.