Package org.springframework.util
Class ConcurrentLruCache<K,V>
java.lang.Object
org.springframework.util.ConcurrentLruCache<K,V>
- Type Parameters:
K
- the type of the key used for cache retrievalV
- the type of the cached values
Simple LRU (Least Recently Used) cache, bounded by a specified cache limit.
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.
- Since:
- 5.3
- Author:
- Brian Clozel, Juergen Hoeller
- See Also:
-
Constructor Summary
ConstructorDescriptionConcurrentLruCache
(int sizeLimit, Function<K, V> generator) Create a new cache instance with the given limit and generator function. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Immediately remove all entries from this cache.boolean
Determine whether the given key is present in this cache.Retrieve an entry from the cache, potentially triggering generation of the value.boolean
Immediately remove the given key and any associated value.int
size()
Return the current size of the cache.int
Return the maximum number of entries in the cache (0 indicates no caching, always generating a new value).
-
Constructor Details
-
ConcurrentLruCache
Create a new cache instance with the given limit and generator function.- Parameters:
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 key
-
-
Method Details
-
get
Retrieve an entry from the cache, potentially triggering generation of the value.- Parameters:
key
- the key to retrieve the entry for- Returns:
- the cached or newly generated value
-
contains
Determine whether the given key is present in this cache.- Parameters:
key
- the key to check for- Returns:
true
if the key is present,false
if there was no matching key
-
remove
Immediately remove the given key and any associated value.- Parameters:
key
- the key to evict the entry for- Returns:
true
if the key was present before,false
if there was no matching key
-
clear
public void clear()Immediately remove all entries from this cache. -
size
public int size()Return the current size of the cache.- See Also:
-
sizeLimit
public int sizeLimit()Return the maximum number of entries in the cache (0 indicates no caching, always generating a new value).- See Also:
-