K
- the type of the key used for cache retrievalV
- the type of the cached valuespublic class ConcurrentLruCache<K,V> extends Object
This implementation is backed by a ConcurrentHashMap
for storing
the cached values and a ConcurrentLinkedDeque
for ordering the keys
and choosing the least recently used key when the cache is at full capacity.
get(K)
Constructor and Description |
---|
ConcurrentLruCache(int sizeLimit,
Function<K,V> generator)
Create a new cache instance with the given limit and generator function.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Immediately remove all entries from this cache.
|
boolean |
contains(K key)
Determine whether the given key is present in this cache.
|
V |
get(K key)
Retrieve an entry from the cache, potentially triggering generation
of the value.
|
boolean |
remove(K key)
Immediately remove the given key and any associated value.
|
int |
size()
Return the current size of the cache.
|
int |
sizeLimit()
Return the maximum number of entries in the cache
(0 indicates no caching, always generating a new value).
|
public ConcurrentLruCache(int sizeLimit, Function<K,V> generator)
sizeLimit
- the maximum number of entries in the cache
(0 indicates no caching, always generating a new value)generator
- a function to generate a new value for a given keypublic V get(K key)
key
- the key to retrieve the entry forpublic boolean contains(K key)
key
- the key to check fortrue
if the key is present,
false
if there was no matching keypublic boolean remove(K key)
key
- the key to evict the entry fortrue
if the key was present before,
false
if there was no matching keypublic void clear()
public int size()
sizeLimit()
public int sizeLimit()
size()