V
- the value typepublic class CacheLongKeyLIRS<V> extends Object
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 (8 per segment by default). Write access and moving entries to the top of the stack is synchronized per segment.
Modifier and Type | Class and Description |
---|---|
static class |
CacheLongKeyLIRS.Config
The cache configuration.
|
Constructor and Description |
---|
CacheLongKeyLIRS(CacheLongKeyLIRS.Config config)
Create a new cache with the given memory size.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Remove all entries.
|
boolean |
containsKey(long key)
Check whether there is a resident entry for the given key.
|
boolean |
containsValue(Object value)
Check whether the given value is stored.
|
Set<Map.Entry<Long,V>> |
entrySet()
Get the entry set for all resident entries.
|
V |
get(long key)
Get the value for the given key if the entry is cached.
|
long |
getHits()
Get the number of cache hits.
|
Map<Long,V> |
getMap()
Convert this cache to a map.
|
long |
getMaxMemory()
Get the maximum memory to use.
|
int |
getMemory(long key)
Get the memory used for the given key.
|
long |
getMisses()
Get the number of cache misses.
|
long |
getUsedMemory()
Get the currently used memory.
|
boolean |
isEmpty()
Check whether the cache is empty.
|
List<Long> |
keys(boolean cold,
boolean nonResident)
Get the list of keys.
|
Set<Long> |
keySet()
Get the set of keys for resident entries.
|
V |
peek(long key)
Get the value for the given key if the entry is cached.
|
V |
put(long key,
V value)
Add an entry to the cache using the average memory size.
|
V |
put(long key,
V value,
int memory)
Add an entry to the cache.
|
void |
putAll(Map<Long,? extends V> m)
Add all elements of the map to this cache.
|
V |
remove(long key)
Remove an 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(V value)
Get the size of the given value.
|
List<V> |
values()
Get the values for all resident entries.
|
public CacheLongKeyLIRS(CacheLongKeyLIRS.Config config)
config
- the configurationpublic void clear()
public boolean containsKey(long key)
key
- the key (may not be null)public V peek(long key)
key
- the key (may not be null)public V put(long key, V value)
key
- the key (may not be null)value
- the value (may not be null)public V put(long 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 entryprotected int sizeOf(V value)
value
- the valuepublic V remove(long key)
key
- the key (may not be null)public int getMemory(long key)
key
- the key (may not be null)public V get(long key)
key
- the key (may not be null)public long getUsedMemory()
public void setMaxMemory(long maxMemory)
maxMemory
- the maximum size (1 or larger) in bytespublic long getMaxMemory()
public Set<Map.Entry<Long,V>> entrySet()
public int sizeNonResident()
public int sizeMapArray()
public int sizeHot()
public long getHits()
public long getMisses()
public int size()
public List<Long> keys(boolean cold, boolean nonResident)
cold
- if true, only keys for the cold entries are returnednonResident
- true for non-resident entriespublic boolean isEmpty()
public boolean containsValue(Object value)
value
- the valueCopyright © 2017 JBoss by Red Hat. All rights reserved.