org.apache.activemq.util
Class LFUCache<Key,Value>
java.lang.Object
org.apache.activemq.util.LFUCache<Key,Value>
- All Implemented Interfaces:
- Map<Key,Value>
public class LFUCache<Key,Value>
- extends Object
- implements Map<Key,Value>
LFU cache implementation based on http://dhruvbird.com/lfu.pdf, with some notable differences:
-
Frequency list is stored as an array with no next/prev pointers between nodes: looping over the array should be faster and more CPU-cache friendly than
using an ad-hoc linked-pointers structure.
-
The max frequency is capped at the cache size to avoid creating more and more frequency list entries, and all elements residing in the max frequency entry
are re-positioned in the frequency entry linked set in order to put most recently accessed elements ahead of less recently ones,
which will be collected sooner.
-
The eviction factor determines how many elements (more specifically, the percentage of) will be evicted.
As a consequence, this cache runs in *amortized* O(1) time (considering the worst case of having the lowest frequency at 0 and having to evict all
elements).
- Author:
- Sergio Bossa
| Nested classes/interfaces inherited from interface java.util.Map |
Map.Entry<K,V> |
|
Constructor Summary |
LFUCache(int maxCacheSize,
float evictionFactor)
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
LFUCache
public LFUCache(int maxCacheSize,
float evictionFactor)
put
public Value put(Key k,
Value v)
- Specified by:
put in interface Map<Key,Value>
putAll
public void putAll(Map<? extends Key,? extends Value> map)
- Specified by:
putAll in interface Map<Key,Value>
get
public Value get(Object k)
- Specified by:
get in interface Map<Key,Value>
remove
public Value remove(Object k)
- Specified by:
remove in interface Map<Key,Value>
frequencyOf
public int frequencyOf(Key k)
clear
public void clear()
- Specified by:
clear in interface Map<Key,Value>
keySet
public Set<Key> keySet()
- Specified by:
keySet in interface Map<Key,Value>
values
public Collection<Value> values()
- Specified by:
values in interface Map<Key,Value>
entrySet
public Set<Map.Entry<Key,Value>> entrySet()
- Specified by:
entrySet in interface Map<Key,Value>
size
public int size()
- Specified by:
size in interface Map<Key,Value>
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty in interface Map<Key,Value>
containsKey
public boolean containsKey(Object o)
- Specified by:
containsKey in interface Map<Key,Value>
containsValue
public boolean containsValue(Object o)
- Specified by:
containsValue in interface Map<Key,Value>
Copyright © 2005-2013 The Apache Software Foundation. All Rights Reserved.