K
- key-type used in the cacheV
- value-type used in the cachepublic class LRUCache<K,V> extends Object
The discard strategy comes from Guava's size-based eviction's defaults.
It's thread-safe.
Constructor and Description |
---|
LRUCache(int cacheSize,
CheckedFunction<K,V,E> calculator)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
V |
get(K key)
Get a value, calculating if necessary, and caching the result.
|
Optional<V> |
getIfPresent(K key)
Gets an value, if present, but doesn't create any new entry if it's absent.
|
boolean |
has(K key)
Is a particular key present already in the cache?
|
void |
put(K key,
V value)
Puts a key-value pair irrespective of whether its already present or not.
|
long |
sizeCurrentLoad()
Number of items currently in the cache.
|
public LRUCache(int cacheSize, CheckedFunction<K,V,E> calculator)
E
- type of an exception that may be thrown by calculator
.cacheSize
- maximum-size of cache.calculator
- calculates the value for a given key if it's not already in the cache.public V get(K key) throws GetOperationFailedException
key
- the key whose value will be either calculated freshly or retrieved from the cache.calculator
(see constructor argument) on value key
.GetOperationFailedException
- if the key doesn't exist.public boolean has(K key)
key
- they key to check.public long sizeCurrentLoad()
public Optional<V> getIfPresent(K key)
key
- the key.Optional.empty()
otherwise.Copyright © 2010–2023 Owen Feehan, ETH Zurich, University of Zurich, Hoffmann-La Roche. All rights reserved.