| java.util.HashMap org.jasig.portal.utils.SmartCache
SmartCache | public class SmartCache extends HashMap (Code) | | 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.
author: Ken Weiner, kweiner@unicon.net version: $Revision: 34797 $ |
Constructor Summary | |
public | SmartCache(int iExpirationTimeout) Instantiate a new SmartCache. | public | SmartCache() Instantiate SmartCache with a default expiration timeout of one hour. |
Method Summary | |
public synchronized Object | get(Object key) Get an object from the cache. | public synchronized Object | put(Object key, Object value) Add a new value to the cache. | public synchronized Object | put(Object key, Object value, long lCacheInterval) Add a new value to the cache
Parameters: key - the key, typically a String Parameters: value - the value Parameters: lCacheInterval - an expiration timeout value, in seconds, which will override the default cache value just for this item. | protected void | sweepCache() Removes from the cache values which have expired. |
iExpirationTimeout | protected int iExpirationTimeout(Code) | | |
SmartCache | public SmartCache(int iExpirationTimeout)(Code) | | 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()(Code) | | Instantiate SmartCache with a default expiration timeout of one hour.
|
get | public synchronized Object get(Object key)(Code) | | Get an object from the cache.
Parameters: key - the key, typically a String the value to which the key is mapped in this cache; null if the key is not mapped to any value in this cache. |
put | public synchronized Object put(Object key, Object value)(Code) | | 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.
Parameters: key - the key, typically a String Parameters: value - the value the previous value of the specified key in this hashtable, or null if it did not have one. |
put | public synchronized Object put(Object key, Object value, long lCacheInterval)(Code) | | Add a new value to the cache
Parameters: key - the key, typically a String Parameters: value - the value Parameters: lCacheInterval - an expiration timeout value, in seconds, which will override the default cache value just for this item. If a negative timeoutvalue is specified, the value will be cached indefinitely. the cached object |
sweepCache | protected void sweepCache()(Code) | | Removes from the cache values which have expired.
|
|
|