Package org.apache.ofbiz.base.util.cache
Class UtilCache<K,V>
java.lang.Object
org.apache.ofbiz.base.util.cache.UtilCache<K,V>
- All Implemented Interfaces:
com.googlecode.concurrentlinkedhashmap.EvictionListener<Object,
,CacheLine<V>> Serializable
public final class UtilCache<K,V>
extends Object
implements Serializable, com.googlecode.concurrentlinkedhashmap.EvictionListener<Object,CacheLine<V>>
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
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(CacheListener<K, V> listener) Adds an event listener for key removalsvoid
clear()
static void
Removes all elements from this cachestatic void
clearCache
(String cacheName) static void
clearCachesThatStartWith
(String startsWith) void
Clears the hit and miss countersboolean
containsKey
(Object key) Returns a boolean specifying whether or not an element with the specified key is in the cache.static <K,
V> UtilCache<K, V> static <K,
V> UtilCache<K, V> createUtilCache
(int sizeLimit, long expireTime) static <K,
V> UtilCache<K, V> createUtilCache
(String name) static <K,
V> UtilCache<K, V> createUtilCache
(String name, boolean useSoftReference) static <K,
V> UtilCache<K, V> createUtilCache
(String name, int sizeLimit, int maxInMemory, long expireTime, boolean useSoftReference) static <K,
V> UtilCache<K, V> createUtilCache
(String name, int sizeLimit, int maxInMemory, long expireTime, boolean useSoftReference, String... names) static <K,
V> UtilCache<K, V> createUtilCache
(String name, int sizeLimit, long expireTime) static <K,
V> UtilCache<K, V> createUtilCache
(String name, int sizeLimit, long expireTime, boolean useSoftReference) void
erase()
Removes all elements from this cachestatic <K,
V> UtilCache<K, V> Gets an element from the cache according to the specified key.NOTE: this returns an unmodifiable copy of the keySet, so removing from here won't have an effect, and calling a remove while iterating through the set will not cause a concurrent modification exception.Collection<? extends CacheLine<V>>
long
return the current expire time for the cache elementslong
Returns the number of successful hits on the cacheCollection<? extends Map<String,
Object>> int
long
Returns the number of cache misses from entries that are expiredlong
Returns the number of cache misses from entries that are not found in the cachelong
Returns the number of cache misses from entries that are have had the soft reference cleared out (by garbage collector and such)long
Returns the number of cache misses caused by any reasongetName()
Getter for the name of the UtilCache instance.static <K,
V> UtilCache<K, V> getOrCreateUtilCache
(String name, int sizeLimit, int maxInMemory, long expireTime, boolean useSoftReference, String... names) long
long
long
int
boolean
Return whether or not the cache lines should use a soft reference to the databoolean
isEmpty()
protected void
noteAddition
(K key, V newValue) Send a key addition event to all registered listenersprotected void
noteRemoval
(K key, V oldValue) Send a key removal event to all registered listenersprotected void
noteUpdate
(K key, V newValue, V oldValue) Send a key update event to all registered listenersvoid
onEviction
(Object key, CacheLine<V> value) Puts or loads the passed element into the cachePuts or loads the passed element into the cacheputIfAbsent
(K key, V value) putIfAbsent
(K key, V value, long expireTimeMillis) putIfAbsentAndGet
(K key, V value) Removes an element from the cache according to the specified keyprotected V
removeInternal
(Object key, boolean countRemove) This is used for internal remove calls because we only want to count external callsprotected void
removeInternal
(Object key, CacheLine<V> existingCacheLine) void
removeListener
(CacheListener<K, V> listener) Removes an event listener for key removalsvoid
setExpireTime
(long expireTimeMillis) Sets the expire time for the cache elements.void
setMaxInMemory
(int newInMemory) protected void
setPropertiesParams
(String cacheName) void
setPropertiesParams
(String[] propNames) void
setPropertiesParams
(String settingsResourceName, String[] propNames) void
setSizeLimit
(int newSizeLimit) void
setUseSoftReference
(boolean useSoftReference) Set whether or not the cache lines should use a soft reference to the dataint
size()
Returns the number of elements currently in the cachestatic boolean
Checks for a non-expired key in a specific cachevalues()
-
Method Details
-
setPropertiesParams
-
setPropertiesParams
-
setPropertiesParams
-
getCacheLineTable
-
isEmpty
public boolean isEmpty() -
put
Puts or loads the passed element into the cache- Parameters:
key
- The key for the element, used to reference it in the hashtables and LRU linked listvalue
- The value of the element
-
putIfAbsent
-
putIfAbsentAndGet
-
put
Puts or loads the passed element into the cache- Parameters:
key
- The key for the element, used to reference it in the hashtables and LRU linked listvalue
- The value of the elementexpireTimeMillis
- how long to keep this key in the cache
-
putIfAbsent
-
get
Gets an element from the cache according to the specified key.- Parameters:
key
- The key for the element, used to reference it in the hashtables and LRU linked list- Returns:
- The value of the element specified by the key
-
values
-
getSizeInBytes
public long getSizeInBytes() -
remove
Removes an element from the cache according to the specified key- Parameters:
key
- The key for the element, used to reference it in the hashtables and LRU linked list- Returns:
- The value of the removed element specified by the key
-
removeInternal
This is used for internal remove calls because we only want to count external calls -
removeInternal
-
erase
public void erase()Removes all elements from this cache -
clear
public void clear() -
clearAllCaches
public static void clearAllCaches()Removes all elements from this cache -
getUtilCacheTableKeySet
-
getName
Getter for the name of the UtilCache instance.- Returns:
- The name of the instance
-
getHitCount
public long getHitCount()Returns the number of successful hits on the cache- Returns:
- The number of successful cache hits
-
getMissCountNotFound
public long getMissCountNotFound()Returns the number of cache misses from entries that are not found in the cache- Returns:
- The number of cache misses
-
getMissCountExpired
public long getMissCountExpired()Returns the number of cache misses from entries that are expired- Returns:
- The number of cache misses
-
getMissCountSoftRef
public long getMissCountSoftRef()Returns the number of cache misses from entries that are have had the soft reference cleared out (by garbage collector and such)- Returns:
- The number of cache misses
-
getMissCountTotal
public long getMissCountTotal()Returns the number of cache misses caused by any reason- Returns:
- The number of cache misses
-
getRemoveHitCount
public long getRemoveHitCount() -
getRemoveMissCount
public long getRemoveMissCount() -
clearCounters
public void clearCounters()Clears the hit and miss counters -
setMaxInMemory
public void setMaxInMemory(int newInMemory) -
getMaxInMemory
public int getMaxInMemory() -
setSizeLimit
public void setSizeLimit(int newSizeLimit) -
getSizeLimit
public int getSizeLimit() -
setExpireTime
public void setExpireTime(long expireTimeMillis) Sets the expire time for the cache elements. If 0, elements never expire.- Parameters:
expireTimeMillis
- The expire time for the cache elements
-
getExpireTime
public long getExpireTime()return the current expire time for the cache elements- Returns:
- The expire time for the cache elements
-
setUseSoftReference
public void setUseSoftReference(boolean useSoftReference) Set whether or not the cache lines should use a soft reference to the data -
getUseSoftReference
public boolean getUseSoftReference()Return whether or not the cache lines should use a soft reference to the data -
size
public int size()Returns the number of elements currently in the cache- Returns:
- The number of elements currently in the cache
-
containsKey
Returns a boolean specifying whether or not an element with the specified key is in the cache.- Parameters:
key
- The key for the element, used to reference it in the hashtables and LRU linked list- Returns:
- True is the cache contains an element corresponding to the specified key, otherwise false
-
getCacheLineKeys
NOTE: this returns an unmodifiable copy of the keySet, so removing from here won't have an effect, and calling a remove while iterating through the set will not cause a concurrent modification exception. This behavior is necessary for now for the persisted cache feature. -
getCacheLineValues
-
getLineInfos
-
noteAddition
Send a key addition event to all registered listeners -
noteRemoval
Send a key removal event to all registered listeners -
noteUpdate
Send a key update event to all registered listeners -
addListener
Adds an event listener for key removals -
removeListener
Removes an event listener for key removals -
validKey
Checks for a non-expired key in a specific cache -
clearCachesThatStartWith
-
clearCache
-
getOrCreateUtilCache
-
createUtilCache
-
createUtilCache
-
createUtilCache
-
createUtilCache
-
createUtilCache
-
createUtilCache
-
createUtilCache
-
createUtilCache
-
findCache
-
onEviction
-