K
- the key typeV
- the value type@Internal(since="1.1.1") public class CacheLIRS<K,V> extends java.lang.Object implements com.google.common.cache.LoadingCache<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.
Modifier and Type | Class and Description |
---|---|
static class |
CacheLIRS.Builder<K,V>
A builder for the cache.
|
static interface |
CacheLIRS.EvictionCallback<K,V>
Listener for items that are evicted from the cache.
|
Constructor and Description |
---|
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).
|
Modifier and Type | Method and Description |
---|---|
V |
apply(K key) |
java.util.concurrent.ConcurrentMap<K,V> |
asMap() |
void |
cleanUp() |
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(K key)
Get the value, loading it if needed.
|
V |
get(K key,
java.util.concurrent.Callable<? extends V> valueLoader) |
com.google.common.collect.ImmutableMap<K,V> |
getAll(java.lang.Iterable<? extends K> keys) |
com.google.common.collect.ImmutableMap<K,V> |
getAllPresent(java.lang.Iterable<?> keys) |
int |
getAverageMemory()
Get the average memory used per entry.
|
V |
getIfPresent(java.lang.Object key)
Get the value for the given key if the entry is cached.
|
long |
getMaxMemory()
Get the maximum memory to use.
|
int |
getMemory(K key)
Get the memory used for the given key.
|
V |
getUnchecked(K key)
Get the value, loading it if needed.
|
long |
getUsedMemory()
Get the currently used memory.
|
void |
invalidate(java.lang.Object key)
Remove an entry.
|
void |
invalidateAll()
Remove all entries.
|
void |
invalidateAll(java.lang.Iterable<?> keys) |
boolean |
isEmpty() |
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.
|
static <K,V> CacheLIRS.Builder<K,V> |
newBuilder()
Create a builder.
|
V |
peek(K key)
Get the value for the given key if the entry is cached.
|
void |
put(K key,
V value)
Add an entry to the cache.
|
V |
put(K key,
V value,
int memory)
Add an entry to the cache.
|
void |
putAll(java.util.Map<? extends K,? extends V> m) |
void |
refresh(K key)
Re-load the value for the given key.
|
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.
|
long |
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.
|
com.google.common.cache.CacheStats |
stats() |
public CacheLIRS(int maxEntries)
maxEntries
- the maximum number of entriespublic void invalidateAll()
public boolean containsKey(java.lang.Object key)
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 entrypublic void put(K key, V value)
public V get(K key, java.util.concurrent.Callable<? extends V> valueLoader) throws java.util.concurrent.ExecutionException
public V getUnchecked(K key)
If there is an exception loading, an UncheckedExecutionException is thrown.
public V get(K key) throws java.util.concurrent.ExecutionException
public void refresh(K key)
If there is an exception while loading, it is logged and ignored. This method calls CacheLoader.reload, but synchronously replaces the old value.
@Nullable public V getIfPresent(java.lang.Object key)
public void invalidate(java.lang.Object key)
public V remove(java.lang.Object key)
key
- the key (may not be null)public void invalidateAll(java.lang.Iterable<?> keys)
public int getMemory(K key)
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()
public java.util.Set<K> keySet()
public int sizeNonResident()
public int sizeMapArray()
public int sizeHot()
public long size()
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 entriespublic com.google.common.cache.CacheStats stats()
public static <K,V> CacheLIRS.Builder<K,V> newBuilder()
public com.google.common.collect.ImmutableMap<K,V> getAllPresent(java.lang.Iterable<?> keys)
public com.google.common.collect.ImmutableMap<K,V> getAll(java.lang.Iterable<? extends K> keys) throws java.util.concurrent.ExecutionException
public boolean isEmpty()
Copyright © 2010 - 2020 Adobe. All Rights Reserved