public class ConcurrentLFUCache<K,V> extends Object implements Cache<K,V>, Accountable
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
Modifier and Type | Class and Description |
---|---|
static class |
ConcurrentLFUCache.CacheEntry<K,V> |
static interface |
ConcurrentLFUCache.EvictionListener<K,V> |
static class |
ConcurrentLFUCache.Stats |
Constructor and 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) |
Modifier and Type | Method and 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() |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getChildResources
public ConcurrentLFUCache(int upperWaterMark, int lowerWaterMark, int acceptableSize, int initialSize, boolean runCleanupThread, boolean runNewThreadForCleanup, ConcurrentLFUCache.EvictionListener<K,V> evictionListener, boolean timeDecay)
public ConcurrentLFUCache(int upperWaterMark, int lowerWaterMark, int acceptableSize, int initialSize, boolean runCleanupThread, boolean runNewThreadForCleanup, ConcurrentLFUCache.EvictionListener<K,V> evictionListener, boolean timeDecay, int maxIdleTimeSec)
public ConcurrentLFUCache(int size, int lowerWatermark)
public void setAlive(boolean live)
public void setUpperWaterMark(int upperWaterMark)
public void setLowerWaterMark(int lowerWaterMark)
public void setAcceptableWaterMark(int acceptableWaterMark)
public void setTimeDecay(boolean timeDecay)
public void setMaxIdleTime(int maxIdleTime)
public void setNewThreadForCleanup(boolean newThreadForCleanup)
public void setRunCleanupThread(boolean runCleanupThread)
public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
computeIfAbsent
in interface Cache<K,V>
public V putCacheEntry(ConcurrentLFUCache.CacheEntry<K,V> e)
public void markAndSweep()
Visible for unit testing.
public Map<K,V> getLeastUsedItems(int n)
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.
n
- the number of items neededpublic Map<K,V> getMostUsedItems(int n)
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.
n
- the number of items neededpublic int size()
public Map<Object,ConcurrentLFUCache.CacheEntry<K,V>> getMap()
public long ramBytesUsed()
ramBytesUsed
in interface Accountable
public void destroy()
public ConcurrentLFUCache.Stats getStats()
Copyright © 2000-2020 Apache Software Foundation. All Rights Reserved.