org.jasig.portal.utils
Class SmartCache
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap
org.jasig.portal.utils.SmartCache
- All Implemented Interfaces:
- Serializable, Cloneable, Map
public class SmartCache
- extends 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: 19776 $
- Author:
- Ken Weiner, kweiner@unicon.net
- See Also:
- Serialized Form
|
Constructor Summary |
SmartCache()
Instantiate SmartCache with a default expiration timeout of one hour. |
SmartCache(int iExpirationTimeout)
Instantiate a new SmartCache. |
| Methods inherited from class java.util.HashMap |
clear, clone, containsKey, containsValue, entrySet, isEmpty, keySet, putAll, remove, size, values |
iExpirationTimeout
protected int iExpirationTimeout
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.
put
public Object put(Object key,
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 Map- Overrides:
put in class HashMap
- Parameters:
key - the key, typically a Stringvalue - the value
- Returns:
- the previous value of the specified key in this hashtable, or null if it did not have one.
put
public Object put(Object key,
Object value,
long lCacheInterval)
- Add a new value to the cache
- Parameters:
key - the key, typically a Stringvalue - the valuelCacheInterval - 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 Object get(Object key)
- Get an object from the cache.
- Specified by:
get in interface Map- Overrides:
get in class 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.
Copyright © 2010 Jasig. All Rights Reserved.