Package org.apache.solr.util
Class ConcurrentLFUCache<K,V>
- java.lang.Object
-
- org.apache.solr.util.ConcurrentLFUCache<K,V>
-
- All Implemented Interfaces:
org.apache.lucene.util.Accountable
,Cache<K,V>
public class ConcurrentLFUCache<K,V> extends Object implements Cache<K,V>, org.apache.lucene.util.Accountable
A LFU cache implementation based upon ConcurrentHashMap.This is not a terribly efficient implementation. The tricks used in the LRU version were not directly usable, perhaps it might be possible to rewrite them with LFU in mind.
This API is experimental and subject to change
- Since:
- solr 1.6
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ConcurrentLFUCache.CacheEntry<K,V>
static interface
ConcurrentLFUCache.EvictionListener<K,V>
static class
ConcurrentLFUCache.Stats
-
Constructor Summary
Constructors Constructor Description ConcurrentLFUCache(int size, int lowerWatermark)
ConcurrentLFUCache(int upperWaterMark, int lowerWaterMark, int acceptableSize, int initialSize, boolean runCleanupThread, boolean runNewThreadForCleanup, ConcurrentLFUCache.EvictionListener<K,V> evictionListener, boolean timeDecay)
ConcurrentLFUCache(int upperWaterMark, int lowerWaterMark, int acceptableSize, int initialSize, boolean runCleanupThread, boolean runNewThreadForCleanup, ConcurrentLFUCache.EvictionListener<K,V> evictionListener, boolean timeDecay, int maxIdleTimeSec)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
V
computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
void
destroy()
protected void
finalize()
V
get(K key)
Map<K,V>
getLeastUsedItems(int n)
Returns 'n' number of least used entries present in this cache.Map<Object,ConcurrentLFUCache.CacheEntry<K,V>>
getMap()
Map<K,V>
getMostUsedItems(int n)
Returns 'n' number of most used entries present in this cache.ConcurrentLFUCache.Stats
getStats()
void
markAndSweep()
Removes items from the cache to bring the size down to the lowerWaterMark.V
put(K key, V val)
V
putCacheEntry(ConcurrentLFUCache.CacheEntry<K,V> e)
Visible for testing to create synthetic cache entries.long
ramBytesUsed()
V
remove(K key)
void
setAcceptableWaterMark(int acceptableWaterMark)
void
setAlive(boolean live)
void
setLowerWaterMark(int lowerWaterMark)
void
setMaxIdleTime(int maxIdleTime)
void
setNewThreadForCleanup(boolean newThreadForCleanup)
void
setRunCleanupThread(boolean runCleanupThread)
void
setTimeDecay(boolean timeDecay)
void
setUpperWaterMark(int upperWaterMark)
int
size()
-
-
-
Constructor Detail
-
ConcurrentLFUCache
public ConcurrentLFUCache(int upperWaterMark, int lowerWaterMark, int acceptableSize, int initialSize, boolean runCleanupThread, boolean runNewThreadForCleanup, ConcurrentLFUCache.EvictionListener<K,V> evictionListener, boolean timeDecay)
-
ConcurrentLFUCache
public ConcurrentLFUCache(int upperWaterMark, int lowerWaterMark, int acceptableSize, int initialSize, boolean runCleanupThread, boolean runNewThreadForCleanup, ConcurrentLFUCache.EvictionListener<K,V> evictionListener, boolean timeDecay, int maxIdleTimeSec)
-
ConcurrentLFUCache
public ConcurrentLFUCache(int size, int lowerWatermark)
-
-
Method Detail
-
setAlive
public void setAlive(boolean live)
-
setUpperWaterMark
public void setUpperWaterMark(int upperWaterMark)
-
setLowerWaterMark
public void setLowerWaterMark(int lowerWaterMark)
-
setAcceptableWaterMark
public void setAcceptableWaterMark(int acceptableWaterMark)
-
setTimeDecay
public void setTimeDecay(boolean timeDecay)
-
setMaxIdleTime
public void setMaxIdleTime(int maxIdleTime)
-
setNewThreadForCleanup
public void setNewThreadForCleanup(boolean newThreadForCleanup)
-
setRunCleanupThread
public void setRunCleanupThread(boolean runCleanupThread)
-
computeIfAbsent
public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
- Specified by:
computeIfAbsent
in interfaceCache<K,V>
-
putCacheEntry
public V putCacheEntry(ConcurrentLFUCache.CacheEntry<K,V> e)
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 the lowerWaterMark.Visible for unit testing.
- NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
-
getLeastUsedItems
public Map<K,V> getLeastUsedItems(int n)
Returns 'n' number of least used entries present in this cache.This uses a TreeSet to collect the 'n' least used items ordered by ascending hitcount and returns a LinkedHashMap containing 'n' or less than 'n' entries.
- Parameters:
n
- the number of items needed- Returns:
- a LinkedHashMap containing 'n' or less than 'n' entries
-
getMostUsedItems
public Map<K,V> getMostUsedItems(int n)
Returns 'n' number of most used entries present in this cache.This uses a TreeSet to collect the 'n' most used items ordered by descending hitcount and returns a LinkedHashMap containing 'n' or less than 'n' entries.
- Parameters:
n
- the number of items needed- Returns:
- a LinkedHashMap containing 'n' or less than 'n' entries
-
size
public int size()
-
getMap
public Map<Object,ConcurrentLFUCache.CacheEntry<K,V>> getMap()
-
ramBytesUsed
public long ramBytesUsed()
- Specified by:
ramBytesUsed
in interfaceorg.apache.lucene.util.Accountable
-
destroy
public void destroy()
-
getStats
public ConcurrentLFUCache.Stats getStats()
-
-