Top Description Inners Fields Constructors Methods
org.springframework.util

public final Class ConcurrentLruCache<K, V>

extends Object
Class Inheritance
Annotations
@SuppressWarnings:unchecked
Type Parameters
<K>
the type of the key used for cache retrieval
<V>
the type of the cached values, does not allow null values
Imports
java.util.Queue, java.util.concurrent.ConcurrentHashMap, .ConcurrentLinkedQueue, .ConcurrentMap, java.util.concurrent.atomic.AtomicInteger, .AtomicLongArray, .AtomicReference, .AtomicReferenceArray, java.util.concurrent.locks.Lock, .ReentrantLock, java.util.function.Function, org.springframework.lang.Nullable

Simple LRU (Least Recently Used) cache, bounded by a specified cache capacity.

This is a simplified, opinionated implementation of an LRU cache for internal use in Spring Framework. It is inspired from ConcurrentLinkedHashMap.

Read and write operations are internally recorded in dedicated buffers, then drained at chosen times to avoid contention.

Authors
Brian Clozel, Ben Manes
Since
5.3
See Also
get(Object)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
ConcurrentLruCache.AddTask

Write operation recorded when a new entry is added to the cache.

private static record
private static enum
private static enum
private static class
private static class
private static class
private class
ConcurrentLruCache.RemovalTask

Write operation recorded when an entry is removed to the cache.

private static class

Field Summary

Modifier and TypeField and Description
private final ConcurrentMap<K, ConcurrentLruCache.Node<K, V>>
private final int
private final AtomicInteger
private final AtomicReference<ConcurrentLruCache.DrainStatus>
private final Lock
private final ConcurrentLruCache.EvictionQueue<K, V>
private final Function<K, V>
private final ConcurrentLruCache.ReadOperations<K, V>
private final ConcurrentLruCache.WriteOperations

Constructor Summary

AccessConstructor and Description
public
ConcurrentLruCache(int
the maximum number of entries in the cache (0 indicates no caching, always generating a new value)
capacity
,
Function<K, V>
a function to generate a new value for a given key
generator
)

Create a new cache instance with the given capacity and generator function.

private
ConcurrentLruCache(int capacity, Function<K, V> generator, int concurrencyLevel)

Method Summary

Modifier and TypeMethod and Description
public int
capacity()

Return the maximum number of entries in the cache.

public void
clear()

Immediately remove all entries from this cache.

public boolean

Returns:

true if the key is present, false if there was no matching key
contains
(K
the key to check for
key
)

Determine whether the given key is present in this cache.

private void
public V

Returns:

the cached or newly generated value
get
(K
the key to retrieve the entry for
key
)

Retrieve an entry from the cache, potentially triggering generation of the value.

private void
private void
private void
private void
private void
put(K key, V value)

public boolean

Returns:

true if the key was present before, false if there was no matching key
remove
(K
the key to evict the entry for
key
)

Immediately remove the given key and any associated value.

public int
size()

Return the current size of the cache.

public int
sizeLimit()
Deprecated since 6.0. in favor of capacity() as of 6.0.

Return the maximum number of entries in the cache.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait