|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.AbstractMap<K,V>
org.h2.dev.cache.CacheLIRS<K,V>
K
- the key typeV
- the value typepublic class CacheLIRS<K,V>
A scan resistant cache. It is meant to cache objects that are relatively costly to acquire, for example file content.
This implementation is multi-threading safe and supports concurrent access. Null keys or null values are not allowed. The map fill factor is at most 75%.
Each entry is assigned a distinct memory size, and the cache will try to use at most the specified amount of memory. The memory unit is not relevant, however it is suggested to use bytes as the unit.
This class implements an approximation of the the LIRS replacement algorithm invented by Xiaodong Zhang and Song Jiang as described in http://www.cse.ohio-state.edu/~zhang/lirs-sigmetrics-02.html with a few smaller changes: An additional queue for non-resident entries is used, to prevent unbound memory usage. The maximum size of this queue is at most the size of the rest of the stack. About 6.25% of the mapped entries are cold.
Internally, the cache is split into a number of segments, and each segment is an individual LIRS cache.
Accessed entries are only moved to the top of the stack if at least a number of other entries have been moved to the front (1% by default). Write access and moving entries to the top of the stack is synchronized per segment.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class java.util.AbstractMap |
---|
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V> |
Constructor Summary | |
---|---|
CacheLIRS(int maxEntries)
Create a new cache with the given number of entries, and the default settings (an average size of 1 per entry, 16 segments, and stack move distance equals to the maximum number of entries divided by 100). |
|
CacheLIRS(long maxMemory,
int averageMemory,
int segmentCount,
int stackMoveDistance)
Create a new cache with the given memory size. |
Method Summary | |
---|---|
void |
clear()
Remove all entries. |
boolean |
containsKey(java.lang.Object key)
Check whether there is a resident entry for the given key. |
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet()
Get the entry set for all resident entries. |
V |
get(java.lang.Object key)
Get the value for the given key if the entry is cached. |
int |
getAverageMemory()
Get the average memory used per entry. |
long |
getMaxMemory()
Get the maximum memory to use. |
int |
getMemory(K key)
Get the memory used for the given key. |
long |
getUsedMemory()
Get the currently used memory. |
java.util.List<K> |
keys(boolean cold,
boolean nonResident)
Get the list of keys. |
java.util.Set<K> |
keySet()
Get the set of keys for resident entries. |
protected void |
onRemove(K key)
This method is called after the value for the given key was removed. |
V |
peek(K key)
Get the value for the given key if the entry is cached. |
V |
put(K key,
V value)
Add an entry to the cache using the average memory size. |
V |
put(K key,
V value,
int memory)
Add an entry to the cache. |
V |
remove(java.lang.Object key)
Remove an entry. |
void |
setAverageMemory(int averageMemory)
Set the average memory used per entry. |
void |
setMaxMemory(long maxMemory)
Set the maximum memory this cache should use. |
int |
size()
Get the number of resident entries. |
int |
sizeHot()
Get the number of hot entries in the cache. |
int |
sizeMapArray()
Get the length of the internal map array. |
int |
sizeNonResident()
Get the number of non-resident entries in the cache. |
protected int |
sizeOf(K key,
V value)
Get the size of the given value. |
Methods inherited from class java.util.AbstractMap |
---|
clone, containsValue, equals, hashCode, isEmpty, putAll, toString, values |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public CacheLIRS(int maxEntries)
maxEntries
- the maximum number of entriespublic CacheLIRS(long maxMemory, int averageMemory, int segmentCount, int stackMoveDistance)
maxMemory
- the maximum memory to use (1 or larger)averageMemory
- the average memory (1 or larger)segmentCount
- the number of cache segments (must be a power of 2)stackMoveDistance
- how many other item are to be moved to the top
of the stack before the current item is movedMethod Detail |
---|
public void clear()
clear
in interface java.util.Map<K,V>
clear
in class java.util.AbstractMap<K,V>
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map<K,V>
containsKey
in class java.util.AbstractMap<K,V>
key
- the key (may not be null)
public V peek(K key)
key
- the key (may not be null)
public V put(K key, V value, int memory)
key
- the key (may not be null)value
- the value (may not be null)memory
- the memory used for the given entry
public V put(K key, V value)
put
in interface java.util.Map<K,V>
put
in class java.util.AbstractMap<K,V>
key
- the key (may not be null)value
- the value (may not be null)
protected int sizeOf(K key, V value)
key
- the keyvalue
- the value
protected void onRemove(K key)
key
- the keypublic V remove(java.lang.Object key)
remove
in interface java.util.Map<K,V>
remove
in class java.util.AbstractMap<K,V>
key
- the key (may not be null)
public int getMemory(K key)
key
- the key (may not be null)
public V get(java.lang.Object key)
get
in interface java.util.Map<K,V>
get
in class java.util.AbstractMap<K,V>
key
- the key (may not be null)
public long getUsedMemory()
public void setMaxMemory(long maxMemory)
maxMemory
- the maximum size (1 or larger)public void setAverageMemory(int averageMemory)
averageMemory
- the average memory used (1 or larger)public int getAverageMemory()
public long getMaxMemory()
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
entrySet
in interface java.util.Map<K,V>
entrySet
in class java.util.AbstractMap<K,V>
public java.util.Set<K> keySet()
keySet
in interface java.util.Map<K,V>
keySet
in class java.util.AbstractMap<K,V>
public int sizeNonResident()
public int sizeMapArray()
public int sizeHot()
public int size()
size
in interface java.util.Map<K,V>
size
in class java.util.AbstractMap<K,V>
public java.util.List<K> keys(boolean cold, boolean nonResident)
cold
- if true, only keys for the cold entries are returnednonResident
- true for non-resident entries
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |