Package org.apache.solr.util
Class ConcurrentLRUCache<K,V>
java.lang.Object
org.apache.solr.util.ConcurrentLRUCache<K,V>
- All Implemented Interfaces:
org.apache.lucene.util.Accountable,org.apache.solr.common.util.Cache<K,V>
public class ConcurrentLRUCache<K,V>
extends Object
implements org.apache.solr.common.util.Cache<K,V>, org.apache.lucene.util.Accountable
A LRU cache implementation based upon ConcurrentHashMap and other techniques to reduce contention
and synchronization overhead to utilize multiple CPU cores more effectively.
Note that the implementation does not follow a true LRU (least-recently-used) eviction strategy. Instead it strives to remove least recently used items but when the initial cleanup does not remove enough items to reach the 'acceptableWaterMark' limit, it can remove more items forcefully regardless of access order.
- Since:
- solr 1.4
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic interfacestatic class -
Field Summary
Fields inherited from interface org.apache.lucene.util.Accountable
NULL_ACCOUNTABLE -
Constructor Summary
ConstructorsConstructorDescriptionConcurrentLRUCache(int size, int lowerWaterMark) ConcurrentLRUCache(int upperWaterMark, int lowerWaterMark, int acceptableWaterMark, int initialSize, boolean runCleanupThread, boolean runNewThreadForCleanup, ConcurrentLRUCache.EvictionListener<K, V> evictionListener) ConcurrentLRUCache(int upperWaterMark, int lowerWaterMark, int acceptableWaterMark, int initialSize, boolean runCleanupThread, boolean runNewThreadForCleanup, ConcurrentLRUCache.EvictionListener<K, V> evictionListener, int maxIdleTimeSec) ConcurrentLRUCache(long ramLowerWatermark, long ramUpperWatermark, boolean runCleanupThread, ConcurrentLRUCache.EvictionListener<K, V> evictionListener) ConcurrentLRUCache(long ramLowerWatermark, long ramUpperWatermark, boolean runCleanupThread, ConcurrentLRUCache.EvictionListener<K, V> evictionListener, int maxIdleTimeSec) -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) voiddestroy()Collection<org.apache.lucene.util.Accountable> getLatestAccessedItems(int n) getMap()getOldestAccessedItems(int n) Returns 'n' number of oldest accessed entries present in this cache.getStats()voidRemoves items from the cache to bring the size down to an acceptable value.Visible for testing to create synthetic cache entries.longvoidsetAcceptableWaterMark(int acceptableWaterMark) voidsetAlive(boolean live) voidsetLowerWaterMark(int lowerWaterMark) voidsetMaxIdleTime(int maxIdleTime) voidsetRamLowerWatermark(long ramLowerWatermark) voidsetRamUpperWatermark(long ramUpperWatermark) voidsetRunCleanupThread(boolean runCleanupThread) voidsetUpperWaterMark(int upperWaterMark) intsize()
-
Constructor Details
-
ConcurrentLRUCache
public ConcurrentLRUCache(long ramLowerWatermark, long ramUpperWatermark, boolean runCleanupThread, ConcurrentLRUCache.EvictionListener<K, V> evictionListener) -
ConcurrentLRUCache
public ConcurrentLRUCache(long ramLowerWatermark, long ramUpperWatermark, boolean runCleanupThread, ConcurrentLRUCache.EvictionListener<K, V> evictionListener, int maxIdleTimeSec) -
ConcurrentLRUCache
public ConcurrentLRUCache(int upperWaterMark, int lowerWaterMark, int acceptableWaterMark, int initialSize, boolean runCleanupThread, boolean runNewThreadForCleanup, ConcurrentLRUCache.EvictionListener<K, V> evictionListener) -
ConcurrentLRUCache
public ConcurrentLRUCache(int upperWaterMark, int lowerWaterMark, int acceptableWaterMark, int initialSize, boolean runCleanupThread, boolean runNewThreadForCleanup, ConcurrentLRUCache.EvictionListener<K, V> evictionListener, int maxIdleTimeSec) -
ConcurrentLRUCache
public ConcurrentLRUCache(int size, int lowerWaterMark)
-
-
Method Details
-
setAlive
public void setAlive(boolean live) -
setUpperWaterMark
public void setUpperWaterMark(int upperWaterMark) -
setLowerWaterMark
public void setLowerWaterMark(int lowerWaterMark) -
setAcceptableWaterMark
public void setAcceptableWaterMark(int acceptableWaterMark) -
setRamUpperWatermark
public void setRamUpperWatermark(long ramUpperWatermark) -
setRamLowerWatermark
public void setRamLowerWatermark(long ramLowerWatermark) -
setMaxIdleTime
public void setMaxIdleTime(int maxIdleTime) -
setRunCleanupThread
public void setRunCleanupThread(boolean runCleanupThread) -
get
-
remove
-
computeIfAbsent
-
put
-
putCacheEntry
Visible for testing to create synthetic cache entries.- NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
-
markAndSweep
public void markAndSweep()Removes items from the cache to bring the size down to an acceptable value.Visible for unit testing.
- NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
-
getOldestAccessedItems
Returns 'n' number of oldest accessed entries present in this cache.This uses a TreeSet to collect the 'n' oldest items ordered by ascending last access time and returns a LinkedHashMap containing 'n' or less than 'n' entries.
- Parameters:
n- the number of oldest items needed- Returns:
- a LinkedHashMap containing 'n' or less than 'n' entries
-
getLatestAccessedItems
-
size
public int size() -
clear
public void clear() -
getMap
-
destroy
public void destroy() -
getStats
-
ramBytesUsed
public long ramBytesUsed()- Specified by:
ramBytesUsedin interfaceorg.apache.lucene.util.Accountable
-
getChildResources
- Specified by:
getChildResourcesin interfaceorg.apache.lucene.util.Accountable
-