Class ConcurrentLruCache<K,V>

java.lang.Object
org.camunda.commons.utils.cache.ConcurrentLruCache<K,V>
Type Parameters:
K - the type of keys
V - the type of mapped values
All Implemented Interfaces:
Cache<K,V>

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

*Note*: The consistency of the keys queue with the keys in the cache is not ensured! This means, the keys queue can contain duplicates of the same key and not all the keys of the queue are necessarily in the cache. However, all the keys of the cache are at least once contained in the keys queue.

  • Constructor Summary

    Constructors
    Constructor
    Description
    ConcurrentLruCache(int capacity)
    Creates the cache with a fixed capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears the contents of the 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
    put(K key, V value)
    Associates the specified value with the specified key in the cache.
    void
    remove(K key)
    Removes an entry from the cache.
    protected void
    removeAll(K key)
    Removes all instances of the given key within the keys queue.
    int
     

    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)
      Creates the cache with a fixed capacity.
      Parameters:
      capacity - max number of cache entries
      Throws:
      IllegalArgumentException - if capacity is negative
  • 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.
    • put

      public void put(K key, V value)
      Description copied from interface: Cache
      Associates the specified value with the specified key in the cache.
      Specified by:
      put in interface Cache<K,V>
      Parameters:
      key - key with which the specified value is to be associated
      value - value to be associated with the specified key
    • remove

      public void remove(K key)
      Description copied from interface: Cache
      Removes an entry from the cache.
      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
      Clears the contents of the cache.
      Specified by:
      clear in interface Cache<K,V>
    • 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>
    • 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>
    • size

      public int size()
      Specified by:
      size in interface Cache<K,V>
      Returns:
      the current size of the cache
    • removeAll

      protected void removeAll(K key)
      Removes all instances of the given key within the keys queue.