K
- key type of map, as always keys are expected to not mutate during the lifetime of this objectV
- value type of mappublic class LeastRecentlyUsedCache<K,V>
extends java.lang.Object
Usage here means that the latest value which is either add by put(Object, Object)
or retrieved by get(Object)
is the least recently used one.
The implemented map basically keeps only soft references to its values, but not its keys. Therefore keys and values must be of different types, otherwise store values will not be freed, ever.
This does deliberately not implement Map
as that has too many methods
mot making much sense here.
Attention: null
values are not allowed for this map, as null
is used to
mark valeus which are removed by the garbage collector
See RecreatingLeastRecentlyUsedCache
for a way to include an automatic recreation
of garbage-collected values.
Modifier and Type | Field and Description |
---|---|
protected java.util.Map<K,java.lang.ref.SoftReference<V>> |
map |
Constructor and Description |
---|
LeastRecentlyUsedCache(int leastRecentLimit)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
cleanup()
Remove all key-value pairs which are no longer available.
|
void |
clear()
Remove all entries from this cache.
|
V |
computeIfAbsent(K key,
java.util.function.Function<? super K,? extends V> creator)
Get the value for the given key, or compute a new value and add it for
the given key.
|
V |
get(K key)
Get the value for the given key.
|
int |
getLeastRecentCacheDepth()
Get the maximum number of values kept in the LRU cache.
|
java.util.List<V> |
getLeastRecentlyUsed()
Get the values in the LRU cache in order.
|
int |
getNumGarbageCollected()
Get the number of times an entry was garbage collected.
|
int |
getNumHits()
Get the number of times a cached value was successfully retrieved.
|
int |
getNumMisses()
Get the number of times an entry was not found in the cache.
|
boolean |
isEmpty()
Is this mapping empty?
|
java.util.Set<K> |
keys()
Get the keys currently valid in this cache.
|
void |
put(K key,
V value)
Put a new key-value pair into this cache.
|
V |
remove(K key)
Remove a key-value pair from this cache.
|
int |
size()
Get the size of this mapping.
|
public LeastRecentlyUsedCache(int leastRecentLimit)
leastRecentLimit
values cached.
It does not define a recreator, therefore values removed by
garbage collection are gone, and have to be recreated and
readded to this maps elsewhere.leastRecentLimit
- maximal number of cached values, non-negativepublic void cleanup()
public int size()
cleanup()
before returning the size,
but as garbage collection can happen any time users should not depend
too much on the returned value.public int getLeastRecentCacheDepth()
public boolean isEmpty()
cleanup()
before returning the answer
but as garbage collection can happen any time users should not depend
too much on the returned value.@Nullable public V get(@NotNull K key)
key
- key associated with the requested valuenull
when the required value was never added to this cache
or when it was garbage collected in the meantime, otherwise the cached object
associated with the given keypublic void put(K key, @NotNull V value)
key
- key for retrieving the cached valuevalue
- value to cachepublic V computeIfAbsent(K key, @NotNull java.util.function.Function<? super K,? extends V> creator)
key
- key of requested valuecreator
- creator for new value if key is (no longer) presentnull
only if creator returns null
@NotNull public java.util.Set<K> keys()
cleanup()
before the keys are returned.
This is just a snapshot, don't depend too much on the returned keys.@Nullable public V remove(K key)
key
- key of the key-value pairnull
if the object was either never cached at all
or has been garbage collectedpublic void clear()
@NotNull public java.util.List<V> getLeastRecentlyUsed()
public int getNumHits()
public int getNumGarbageCollected()
public int getNumMisses()