Class ConcurrentLruCache<K,V>

java.lang.Object
com.aspectran.utils.cache.ConcurrentLruCache<K,V>
Type Parameters:
K - the type of the key used for caching
V - the type of the cached values
All Implemented Interfaces:
Cache<K,V>

public class ConcurrentLruCache<K,V> extends Object implements Cache<K,V>
A thread-safe LRU Cache with a fixed capacity. If the cache reaches the capacity, it discards the least recently used entry first.

This implementation is backed by a ConcurrentHashMap for storing the cached values and a ConcurrentLinkedQueue for ordering the keys and choosing the least recently used key when the cache is at full capacity.

Since:
6.6.7
  • Constructor Summary

    Constructors
    Constructor
    Description
    ConcurrentLruCache(int capacity, Function<K,V> generator)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all of the elements from this cache.
    get(K key)
    Gets an entry from the cache.
    boolean
    Returns true if this cache contains no key-value mappings.
    Returns a Set view of the keys contained in this cache.
    void
    remove(K key)
    Removes the specified element from this cache if it is present.
    int
    Returns the number of elements in this cache.

    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)
  • Method Details

    • get

      public V get(K key)
      Description copied from interface: Cache
      Gets an entry from the cache.
      Specified by:
      get in interface Cache<K,V>
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      the element, or null, if it does not exist
    • remove

      public void remove(K key)
      Description copied from interface: Cache
      Removes the specified element from this cache if it is present.
      Specified by:
      remove in interface Cache<K,V>
      Parameters:
      key - key with which the specified value is to be associated
    • clear

      public void clear()
      Description copied from interface: Cache
      Removes all of the elements from this cache.
      Specified by:
      clear in interface Cache<K,V>
    • keySet

      public Set<K> keySet()
      Description copied from interface: Cache
      Returns a Set view of the keys contained in this cache.
      Specified by:
      keySet in interface Cache<K,V>
      Returns:
      the set view
    • size

      public int size()
      Description copied from interface: Cache
      Returns the number of elements in this cache.
      Specified by:
      size in interface Cache<K,V>
      Returns:
      the number of elements in this cache
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: Cache
      Returns true if this cache contains no key-value mappings.
      Specified by:
      isEmpty in interface Cache<K,V>
      Returns:
      true if this cache contains no key-value mappings