Class ConcurrentLruCache<K,V>

java.lang.Object
org.springframework.util.ConcurrentLruCache<K,V>
Type Parameters:
K - the type of the key used for cache retrieval
V - the type of the cached values, does not allow null values

public final class ConcurrentLruCache<K,V> extends Object
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.

Since:
5.3
Author:
Brian Clozel, Ben Manes
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    ConcurrentLruCache(int capacity, Function<K,V> generator)
    Create a new cache instance with the given capacity and generator function.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Return the maximum number of entries in the cache.
    void
    Immediately remove all entries from this cache.
    boolean
    contains(K key)
    Determine whether the given key is present in this cache.
    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
    Return the current size of the cache.
    int
    Deprecated.
    in favor of capacity() as of 6.0.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ConcurrentLruCache

      public ConcurrentLruCache(int capacity, Function<K,V> generator)
      Create a new cache instance with the given capacity and generator function.
      Parameters:
      capacity - 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

      public V get(K key)
      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
    • capacity

      public int capacity()
      Return the maximum number of entries in the cache.
      See Also:
    • sizeLimit

      @Deprecated(since="6.0") public int sizeLimit()
      Deprecated.
      in favor of capacity() as of 6.0.
      Return the maximum number of entries in the cache.
    • size

      public int size()
      Return the current size of the cache.
      See Also:
    • clear

      public void clear()
      Immediately remove all entries from this cache.
    • contains

      public boolean contains(K key)
      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

      @Nullable public boolean remove(K key)
      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