V - type of values stored in the Mappublic class Long2ObjectCache<V> extends Object implements Map<Long,V>
The eviction strategy is to remove the oldest in a set if the key is not found, or if found then that item. The newly inserted item becomes the youngest in the set. Sets are evicted on a first in, first out, manner unless replacing a matching key.
A good set size would be in the range of 2 to 16 so that the references/keys can fit in a cache-line (assuming references are 32-bit references and 64-byte cache lines, YMMV). A linear search within a cache line is much less costly than a cache-miss to another line.
Null values are not supported by this cache.
| Modifier and Type | Class and Description |
|---|---|
class |
Long2ObjectCache.EntryIterator
Iterator over entries which supports access to unboxed keys via
Long2ObjectCache.EntryIterator.getLongKey(). |
class |
Long2ObjectCache.EntrySet
Set of entries which supports cached iterator to avoid allocation.
|
class |
Long2ObjectCache.KeyIterator
Iterator over keys which supports access to unboxed keys via
Long2ObjectCache.KeyIterator.nextLong(). |
class |
Long2ObjectCache.KeySet
A key set implementation which supports cached iterator to avoid allocation.
|
class |
Long2ObjectCache.ValueCollection
Collection of values which supports cached iterator to avoid allocation.
|
class |
Long2ObjectCache.ValueIterator
An iterator over values.
|
| Constructor and Description |
|---|
Long2ObjectCache(int numSets,
int setSize,
Consumer<V> evictionConsumer)
Constructs cache with provided configuration.
|
| Modifier and Type | Method and Description |
|---|---|
long |
cacheHits()
The number of times a cache hit has occurred on the
get(long) method. |
long |
cacheMisses()
The number of times a cache miss has occurred on the
get(long) method. |
long |
cachePuts()
The number of items that have been put in the cache.
|
int |
capacity()
Get the total capacity for the map to which the load factor will be a fraction of.
|
void |
clear()
Clear down all items in the cache.
|
V |
compute(Long key,
BiFunction<? super Long,? super V,? extends V> remappingFunction) |
V |
compute(long key,
LongObjectToObjectFunction<? super V,? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (or
null if there is no
current mapping). |
V |
computeIfAbsent(Long key,
Function<? super Long,? extends V> mappingFunction) |
V |
computeIfAbsent(long key,
LongFunction<? extends V> mappingFunction)
Get a value for a given key, or if it does ot exist then default the value via a
LongFunction and put it in the cache. |
V |
computeIfPresent(Long key,
BiFunction<? super Long,? super V,? extends V> remappingFunction) |
V |
computeIfPresent(long key,
LongObjectToObjectFunction<? super V,? extends V> remappingFunction)
If the value for the specified key is present attempts to compute a new mapping given the key and its current
mapped value.
|
boolean |
containsKey(long key)
Overloaded version of
Map.containsKey(Object) that takes a primitive long key. |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
Long2ObjectCache.EntrySet |
entrySet() |
boolean |
equals(Object o) |
void |
forEach(BiConsumer<? super Long,? super V> action) |
void |
forEachLong(LongObjConsumer<? super V> consumer)
Implementation of the
forEach(BiConsumer) that avoids boxing of keys. |
V |
get(long key)
Overloaded version of
Map.get(Object) that takes a primitive long key. |
V |
get(Object key) |
V |
getOrDefault(long key,
V defaultValue)
Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the
key.
|
int |
hashCode() |
boolean |
isEmpty() |
Long2ObjectCache.KeySet |
keySet() |
V |
merge(long key,
V value,
BiFunction<? super V,? super V,? extends V> remappingFunction)
If the specified key is not already associated with a value or is associated with null, associates it with the
given non-null value.
|
V |
merge(Long key,
V value,
BiFunction<? super V,? super V,? extends V> remappingFunction) |
V |
put(long key,
V value)
Overloaded version of
Map.put(Object, Object) that takes a primitive long key. |
V |
put(Long key,
V value) |
void |
putAll(Long2ObjectCache<? extends V> map)
Put all values from the given map longo this one without allocation.
|
void |
putAll(Map<? extends Long,? extends V> map) |
V |
putIfAbsent(long key,
V value)
If the specified key is not already associated with a value (or is mapped
to
null) associates it with the given value and returns
null, else returns the current value. |
V |
putIfAbsent(Long key,
V value) |
V |
remove(long key)
Overloaded version of
Map.remove(Object) that takes a primitive long key. |
boolean |
remove(long key,
V value)
Removes the entry for the specified key only if it is currently mapped to the specified value.
|
V |
remove(Object key) |
boolean |
remove(Object key,
Object value) |
V |
replace(long key,
V value)
Replaces the entry for the specified key only if it is currently mapped to some value.
|
V |
replace(Long key,
V value) |
boolean |
replace(long key,
V oldValue,
V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.
|
boolean |
replace(Long key,
V oldValue,
V newValue) |
void |
replaceAll(BiFunction<? super Long,? super V,? extends V> function) |
void |
replaceAllLong(LongObjectToObjectFunction<? super V,? extends V> function)
Replaces each entry's value with the result of invoking the given function on that entry until all entries have
been processed or the function throws an exception.
|
void |
resetCounters()
Reset the cache statistics counters to zero.
|
int |
size() |
String |
toString() |
Long2ObjectCache.ValueCollection |
values() |
clone, finalize, getClass, notify, notifyAll, wait, wait, waitgetOrDefaultpublic Long2ObjectCache(int numSets,
int setSize,
Consumer<V> evictionConsumer)
numSets - number of sets, must be power or two.setSize - size of a single set, must be power or two.evictionConsumer - consumer to be notified when entry is being evicted from the cache.public long cacheHits()
get(long) method.get(long) method.public long cacheMisses()
get(long) method.get(long) method.public long cachePuts()
public void resetCounters()
public int capacity()
public boolean containsKey(Object key)
containsKey in interface Map<Long,V>public boolean containsKey(long key)
Map.containsKey(Object) that takes a primitive long key.key - for indexing the Mappublic boolean containsValue(Object value)
containsValue in interface Map<Long,V>public V get(long key)
Map.get(Object) that takes a primitive long key.key - for indexing the Mappublic V getOrDefault(long key, V defaultValue)
key - whose associated value is to be returned.defaultValue - the default mapping of the key.defaultValue if this map contains no mapping for the key.public void forEach(BiConsumer<? super Long,? super V> action)
public void forEachLong(LongObjConsumer<? super V> consumer)
forEach(BiConsumer) that avoids boxing of keys.consumer - to be called for each key/value pair.public V computeIfAbsent(Long key, Function<? super Long,? extends V> mappingFunction)
computeIfAbsent in interface Map<Long,V>public V computeIfAbsent(long key, LongFunction<? extends V> mappingFunction)
LongFunction and put it in the cache.
Primitive specialized version of Map.computeIfAbsent(K, java.util.function.Function<? super K, ? extends V>).
key - to search on.mappingFunction - to provide a value if the get returns null.public V computeIfPresent(Long key, BiFunction<? super Long,? super V,? extends V> remappingFunction)
computeIfPresent in interface Map<Long,V>public V computeIfPresent(long key, LongObjectToObjectFunction<? super V,? extends V> remappingFunction)
Primitive specialized version of Map.computeIfPresent(Object, BiFunction).
key - with which the specified value is to be associated.remappingFunction - the function to compute a value.public V compute(Long key, BiFunction<? super Long,? super V,? extends V> remappingFunction)
public V compute(long key, LongObjectToObjectFunction<? super V,? extends V> remappingFunction)
null if there is no
current mapping).
Primitive specialized version of Map.compute(Object, BiFunction).
key - with which the specified value is to be associated.remappingFunction - the function to compute a value.public V merge(Long key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
public V merge(long key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
null.
Primitive specialized version of Map.merge(Object, Object, BiFunction).
key - with which the resulting value is to be associated.value - the non-null value to be merged with the existing value associated with the key or,
if no existing value or a null value is associated with the key, to be associated with
the key.remappingFunction - the function to recompute a value if present.public V putIfAbsent(Long key, V value)
putIfAbsent in interface Map<Long,V>public V putIfAbsent(long key, V value)
null) associates it with the given value and returns
null, else returns the current value.
Primitive specialized version of Map.putIfAbsent(Object, Object).
key - with which the specified value is to be associated.value - to be associated with the specified key.null if there was no mapping for the key.public V put(long key, V value)
Map.put(Object, Object) that takes a primitive long key.public boolean remove(long key,
V value)
Primitive specialized version of Map.remove(Object, Object).
key - key with which the specified value is associated.value - expected to be associated with the specified key.true if the value was removed.public V remove(long key)
Map.remove(Object) that takes a primitive long key.key - for indexing the Mappublic boolean replace(long key,
V oldValue,
V newValue)
Primitive specialized version of Map.replace(Object, Object, Object).
key - with which the specified value is associated.oldValue - expected to be associated with the specified key.newValue - to be associated with the specified key.true if the value was replaced.public V replace(long key, V value)
Primitive specialized version of Map.replace(Object, Object).
key - with which the specified value is associated.value - to be associated with the specified key.public void replaceAll(BiFunction<? super Long,? super V,? extends V> function)
replaceAll in interface Map<Long,V>public void replaceAllLong(LongObjectToObjectFunction<? super V,? extends V> function)
Primitive specialized version of Map.replaceAll(BiFunction).
NB: Renamed from forEach to avoid overloading on parameter types of lambda expression, which doesn't play well with type inference in lambda expressions.
function - the function to apply to each entry.public void clear()
If an exception occurs during the eviction function callback then clear may need to be called again to complete.
If an exception occurs the cache should only be used when size() reports zero.
public void putAll(Long2ObjectCache<? extends V> map)
map - whose value are to be added.public Long2ObjectCache.KeySet keySet()
public Long2ObjectCache.ValueCollection values()
public Long2ObjectCache.EntrySet entrySet()
public boolean equals(Object o)
Copyright © 2014-2022 Real Logic Limited. All Rights Reserved.