| java.lang.Object org.ofbiz.base.util.UtilCache
All known Subclasses: com.sourcetap.sfa.ui.UICache,
UtilCache | public class UtilCache (Code) | | Generalized caching utility. Provides a number of caching features:
- Limited or unlimited element capacity
- If limited, removes elements with the LRU (Least Recently Used) algorithm
- Keeps track of when each element was loaded into the cache
- Using the expireTime can report whether a given element has expired
- Counts misses and hits
author: David E. Jones version: $Revision: 1.2 $ since: 2.0 |
Inner Class :public static class CacheLine | |
Field Summary | |
public Map | cacheLineTable A hashtable containing a CacheLine object with a value and a loadTime for each element. | protected static Map | defaultIndices An index number appended to utilCacheTable names when there are conflicts. | protected long | expireTime Specifies the amount of time since initial loading before an element will be reported as expired. | protected long | hitCount | public LinkedList | keyLRUList | protected long | maxSize The maximum number of elements in the cache. | protected long | missCount | protected String | name The name of the UtilCache instance, is also the key for the instance in utilCacheTable. | protected boolean | useSoftReference | public static Map | utilCacheTable A static Map to keep track of all of the UtilCache instances. |
Constructor Summary | |
public | UtilCache(String cacheName, long maxSize, long expireTime, boolean useSoftReference) Constructor which specifies the cacheName as well as the maxSize, expireTime and useSoftReference. | public | UtilCache(String cacheName, long maxSize, long expireTime) Constructor which specifies the cacheName as well as the maxSize and expireTime. | public | UtilCache(long maxSize, long expireTime) Constructor which specifies the maxSize and expireTime. | public | UtilCache(String cacheName) This constructor takes a name for the cache, puts itself in the utilCacheTable. | public | UtilCache() Default constructor, all members stay at default values as defined in cache.properties, or the defaults in this file if cache.properties is not found, or there are no 'default' entries in it. |
cacheLineTable | public Map cacheLineTable(Code) | | A hashtable containing a CacheLine object with a value and a loadTime for each element.
|
defaultIndices | protected static Map defaultIndices(Code) | | An index number appended to utilCacheTable names when there are conflicts.
|
expireTime | protected long expireTime(Code) | | Specifies the amount of time since initial loading before an element will be reported as expired.
If set to 0, elements will never expire.
|
hitCount | protected long hitCount(Code) | | A count of the number of cache hits
|
keyLRUList | public LinkedList keyLRUList(Code) | | A list of the elements order by Least Recent Use
|
maxSize | protected long maxSize(Code) | | The maximum number of elements in the cache.
If set to 0, there will be no limit on the number of elements in the cache.
|
missCount | protected long missCount(Code) | | A count of the number of cache misses
|
name | protected String name(Code) | | The name of the UtilCache instance, is also the key for the instance in utilCacheTable.
|
useSoftReference | protected boolean useSoftReference(Code) | | Specifies whether or not to use soft references for this cache, defaults to false
|
utilCacheTable | public static Map utilCacheTable(Code) | | A static Map to keep track of all of the UtilCache instances.
|
UtilCache | public UtilCache(String cacheName, long maxSize, long expireTime, boolean useSoftReference)(Code) | | Constructor which specifies the cacheName as well as the maxSize, expireTime and useSoftReference.
The passed maxSize, expireTime and useSoftReference will be overridden by values from cache.properties if found.
Parameters: maxSize - The maxSize member is set to this value Parameters: expireTime - The expireTime member is set to this value Parameters: cacheName - The name of the cache. Parameters: useSoftReference - Specifies whether or not to use soft references for this cache. |
UtilCache | public UtilCache(String cacheName, long maxSize, long expireTime)(Code) | | Constructor which specifies the cacheName as well as the maxSize and expireTime.
The passed maxSize and expireTime will be overridden by values from cache.properties if found.
Parameters: maxSize - The maxSize member is set to this value Parameters: expireTime - The expireTime member is set to this value Parameters: cacheName - The name of the cache. |
UtilCache | public UtilCache(long maxSize, long expireTime)(Code) | | Constructor which specifies the maxSize and expireTime.
Parameters: maxSize - The maxSize member is set to this value Parameters: expireTime - The expireTime member is set to this value |
UtilCache | public UtilCache(String cacheName)(Code) | | This constructor takes a name for the cache, puts itself in the utilCacheTable.
It also uses the cacheName to lookup the initialization parameters from cache.properties.
Parameters: cacheName - The name of the cache. |
UtilCache | public UtilCache()(Code) | | Default constructor, all members stay at default values as defined in cache.properties, or the defaults in this file if cache.properties is not found, or there are no 'default' entries in it.
|
clear | public synchronized void clear()(Code) | | Removes all elements from this cache
|
clearAllCaches | public static void clearAllCaches()(Code) | | Removes all elements from this cache
|
clearCounters | public void clearCounters()(Code) | | Clears the hit and miss counters
|
clearExpired | public void clearExpired()(Code) | | Clears all expired cache entries; also clear any cache entries where the SoftReference in the CacheLine object has been cleared by the gc
|
clearExpiredFromAllCaches | public static void clearExpiredFromAllCaches()(Code) | | Clears all expired cache entries from all caches
|
containsKey | public boolean containsKey(Object key)(Code) | | Returns a boolean specifying whether or not an element with the specified key is in the cache.
If the requested element hasExpired, it is removed before it is looked up which causes the function to return false.
Parameters: key - The key for the element, used to reference it in the hastables and LRU linked list True is the cache contains an element corresponding to the specified key, otherwise false |
get | public Object get(Object key)(Code) | | Gets an element from the cache according to the specified key.
If the requested element hasExpired, it is removed before it is looked up which causes the function to return null.
Parameters: key - The key for the element, used to reference it in the hastables and LRU linked list The value of the element specified by the key |
getExpireTime | public long getExpireTime()(Code) | | return the current expire time for the cache elements
The expire time for the cache elements |
getHitCount | public long getHitCount()(Code) | | Returns the number of successful hits on the cache
The number of successful cache hits |
getMaxSize | public long getMaxSize()(Code) | | Returns the current maximum number of elements in the cache
The maximum number of elements in the cache |
getMissCount | public long getMissCount()(Code) | | Returns the number of cache misses
The number of cache misses |
getName | public String getName()(Code) | | Getter for the name of the UtilCache instance.
The name of the instance |
getUseSoftReference | public boolean getUseSoftReference()(Code) | | Return whether or not the cache lines should use a soft reference to the data
|
hasExpired | public boolean hasExpired(Object key)(Code) | | Returns a boolean specifying whether or not the element corresponding to the key has expired.
Only returns true if element is in cache and has expired. Error conditions return false, if no expireTable entry, returns true.
Always returns false if expireTime <= 0.
Also, if SoftReference in the CacheLine object has been cleared by the gc return true.
Parameters: key - The key for the element, used to reference it in the hastables and LRU linked list True is the element corresponding to the specified key has expired, otherwise false |
put | public synchronized void put(Object key, Object value)(Code) | | Puts or loads the passed element into the cache
Parameters: key - The key for the element, used to reference it in the hastables and LRU linked list Parameters: value - The value of the element |
remove | public synchronized Object remove(Object key)(Code) | | Removes an element from the cache according to the specified key
Parameters: key - The key for the element, used to reference it in the hastables and LRU linked list The value of the removed element specified by the key |
setExpireTime | public void setExpireTime(long expireTime)(Code) | | Sets the expire time for the cache elements.
If 0, elements never expire.
Parameters: expireTime - The expire time for the cache elements |
setMaxSize | public void setMaxSize(long maxSize)(Code) | | Sets the maximum number of elements in the cache.
If 0, there is no maximum.
Parameters: maxSize - The maximum number of elements in the cache |
setPropertiesParams | protected void setPropertiesParams(String cacheName)(Code) | | |
setUseSoftReference | public void setUseSoftReference(boolean useSoftReference)(Code) | | Set whether or not the cache lines should use a soft reference to the data
|
size | public long size()(Code) | | Returns the number of elements currently in the cache
The number of elements currently in the cache |
validKey | public static boolean validKey(String cacheName, Object key)(Code) | | Checks for a non-expired key in a specific cache
|
|
|