Package org.anchoranalysis.core.cache
Class LRUCache<K,V>
Object
LRUCache<K,V>
- Type Parameters:
K
- key-type used in the cacheV
- value-type used in the cache
A cache that discards items that haven't being used recently or frequently.
The discard strategy comes from Guava's size-based eviction's defaults.
It's thread-safe.
- Author:
- Owen Feehan
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGet a value, calculating if necessary, and caching the result.getIfPresent
(K key) Gets an value, if present, but doesn't create any new entry if it's absent.boolean
Is a particular key present already in the cache?void
Puts a key-value pair irrespective of whether its already present or not.long
Number of items currently in the cache.
-
Constructor Details
-
LRUCache
Constructor.- Type Parameters:
E
- type of an exception that may be thrown bycalculator
.- Parameters:
cacheSize
- maximum-size of cache.calculator
- calculates the value for a given key if it's not already in the cache.
-
-
Method Details
-
get
Get a value, calculating if necessary, and caching the result.- Parameters:
key
- the key whose value will be either calculated freshly or retrieved from the cache.- Returns:
- the value of applying
calculator
(see constructor argument) on valuekey
. - Throws:
GetOperationFailedException
- if the key doesn't exist.
-
has
Is a particular key present already in the cache?- Parameters:
key
- they key to check.- Returns:
- true iff the key already exists in the cache.
-
sizeCurrentLoad
public long sizeCurrentLoad()Number of items currently in the cache.- Returns:
- the number of items.
-
getIfPresent
Gets an value, if present, but doesn't create any new entry if it's absent.- Parameters:
key
- the key.- Returns:
- an existing element if present or
Optional.empty()
otherwise.
-
put
Puts a key-value pair irrespective of whether its already present or not.- Parameters:
key
- the key.value
- the value.
-