public class UtilCache<K,V> extends Object
Generalized caching utility. Provides a number of caching features:
| Modifier and Type | Class and Description |
|---|---|
static class |
UtilCache.CacheLine<T> |
| Modifier and Type | Field and Description |
|---|---|
Map<K,UtilCache.CacheLine<V>> |
cacheLineTable
A hashtable containing a CacheLine object with a value and a loadTime for each element.
|
protected static Map<String,Integer> |
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
A count of the number of cache hits
|
LinkedList<K> |
keyLRUList
A list of the elements order by Least Recent Use
|
protected long |
maxSize
The maximum number of elements in the cache.
|
protected long |
missCount
A count of the number of cache misses
|
protected String |
name
The name of the UtilCache instance, is also the key for the instance in utilCacheTable.
|
protected boolean |
useSoftReference
Specifies whether or not to use soft references for this cache, defaults to false
|
static Map<String,UtilCache<?,?>> |
utilCacheTable
A static Map to keep track of all of the UtilCache instances.
|
| Constructor and Description |
|---|
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.
|
UtilCache(long maxSize,
long expireTime)
Constructor which specifies the maxSize and expireTime.
|
UtilCache(String cacheName)
This constructor takes a name for the cache, puts itself in the utilCacheTable.
|
UtilCache(String cacheName,
long maxSize,
long expireTime)
Constructor which specifies the cacheName as well as the maxSize and expireTime.
|
UtilCache(String cacheName,
long maxSize,
long expireTime,
boolean useSoftReference)
Constructor which specifies the cacheName as well as the maxSize, expireTime and useSoftReference.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Removes all elements from this cache
|
static void |
clearAllCaches()
Removes all elements from this cache
|
void |
clearCounters()
Clears the hit and miss counters
|
void |
clearExpired()
Clears all expired cache entries; also clear any cache entries where the SoftReference in the CacheLine object has been cleared by the gc
|
static void |
clearExpiredFromAllCaches()
Clears all expired cache entries from all caches
|
boolean |
containsKey(Object key)
Returns a boolean specifying whether or not an element with the specified key is in the cache.
|
V |
get(K key)
Gets an element from the cache according to the specified key.
|
long |
getExpireTime()
return the current expire time for the cache elements
|
long |
getHitCount()
Returns the number of successful hits on the cache
|
long |
getMaxSize()
Returns the current maximum number of elements in the cache
|
long |
getMissCount()
Returns the number of cache misses
|
String |
getName()
Getter for the name of the UtilCache instance.
|
protected String |
getNextDefaultIndex(String cacheName) |
boolean |
getUseSoftReference()
Return whether or not the cache lines should use a soft reference to the data
|
boolean |
hasExpired(K key)
Returns a boolean specifying whether or not the element corresponding to the key has expired.
|
protected boolean |
hasExpired(UtilCache.CacheLine<V> line) |
void |
put(K key,
V value)
Puts or loads the passed element into the cache
|
Object |
remove(Object key)
Removes an element from the cache according to the specified key
|
void |
setExpireTime(long expireTime)
Sets the expire time for the cache elements.
|
void |
setMaxSize(long maxSize)
Sets the maximum number of elements in the cache.
|
protected void |
setPropertiesParams(String cacheName) |
void |
setUseSoftReference(boolean useSoftReference)
Set whether or not the cache lines should use a soft reference to the data
|
long |
size()
Returns the number of elements currently in the cache
|
static boolean |
validKey(String cacheName,
Object key)
Checks for a non-expired key in a specific cache
|
public static Map<String,UtilCache<?,?>> utilCacheTable
protected static Map<String,Integer> defaultIndices
protected String name
public LinkedList<K> keyLRUList
public Map<K,UtilCache.CacheLine<V>> cacheLineTable
protected long hitCount
protected long missCount
protected long maxSize
protected long expireTime
protected boolean useSoftReference
public UtilCache(String cacheName, long maxSize, long expireTime, boolean useSoftReference)
maxSize - The maxSize member is set to this valueexpireTime - The expireTime member is set to this valuecacheName - The name of the cache.useSoftReference - Specifies whether or not to use soft references for this cache.public UtilCache(String cacheName, long maxSize, long expireTime)
maxSize - The maxSize member is set to this valueexpireTime - The expireTime member is set to this valuecacheName - The name of the cache.public UtilCache(long maxSize,
long expireTime)
maxSize - The maxSize member is set to this valueexpireTime - The expireTime member is set to this valuepublic UtilCache(String cacheName)
cacheName - The name of the cache.public UtilCache()
protected void setPropertiesParams(String cacheName)
public void put(K key, V value)
key - The key for the element, used to reference it in the hastables and LRU linked listvalue - The value of the elementpublic V get(K key)
key - The key for the element, used to reference it in the hastables and LRU linked listpublic Object remove(Object key)
key - The key for the element, used to reference it in the hastables and LRU linked listpublic void clear()
public static void clearAllCaches()
public String getName()
public long getHitCount()
public long getMissCount()
public void clearCounters()
public void setMaxSize(long maxSize)
maxSize - The maximum number of elements in the cachepublic long getMaxSize()
public void setExpireTime(long expireTime)
expireTime - The expire time for the cache elementspublic long getExpireTime()
public void setUseSoftReference(boolean useSoftReference)
public boolean getUseSoftReference()
public long size()
public boolean containsKey(Object key)
key - The key for the element, used to reference it in the hastables and LRU linked listpublic boolean hasExpired(K key)
key - The key for the element, used to reference it in the hastables and LRU linked listprotected boolean hasExpired(UtilCache.CacheLine<V> line)
public void clearExpired()
public static void clearExpiredFromAllCaches()
Copyright © 2015 Atlassian. All rights reserved.