org.apache.solr.util

## Class ConcurrentLRUCache<K,V>

- java.lang.Object
- - org.apache.solr.util.ConcurrentLRUCache<K,V>

- All Implemented Interfaces:
    Cache<K,V>

      public class ConcurrentLRUCache<K,V>
      extends Object
      implements Cache<K,V>

  A LRU cache implementation based upon ConcurrentHashMap and other techniques to reduce contention and synchronization overhead to utilize multiple CPU cores more effectively.

  Note that the implementation does not follow a true LRU (least-recently-used) eviction strategy. Instead it strives to remove least recently used items but when the initial cleanup does not remove enough items to reach the 'acceptableWaterMark' limit, it can remove more items forcefully regardless of access order.

  Since:
    solr 1.4

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `ConcurrentLRUCache.CacheEntry<K,V>`
    `static interface` | `ConcurrentLRUCache.EvictionListener<K,V>`
    `static class` | `ConcurrentLRUCache.Stats`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ConcurrentLRUCache(int size, int lowerWatermark)`
    `ConcurrentLRUCache(int upperWaterMark, int lowerWaterMark, int acceptableWatermark, int initialSize, boolean runCleanupThread, boolean runNewThreadForCleanup, ConcurrentLRUCache.EvictionListener<K,V> evictionListener)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `clear()`
    `void` | `destroy()`
    `protected void` | `finalize()`
    `V` | `get(K key)`
    `Map<K,V>` | `getLatestAccessedItems(int n)`
    `Map<Object,ConcurrentLRUCache.CacheEntry<K,V>>` | `getMap()`
    `Map<K,V>` | `getOldestAccessedItems(int n)` Returns 'n' number of oldest accessed entries present in this cache.
    `ConcurrentLRUCache.Stats` | `getStats()`
    `V` | `put(K key, V val)`
    `V` | `remove(K key)`
    `void` | `setAlive(boolean live)`
    `int` | `size()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.apache.solr.common.util.Cache

      `computeIfAbsent`

- - ### Constructor Detail

    - #### ConcurrentLRUCache

          public ConcurrentLRUCache(int upperWaterMark,
                                    int lowerWaterMark,
                                    int acceptableWatermark,
                                    int initialSize,
                                    boolean runCleanupThread,
                                    boolean runNewThreadForCleanup,
                                    ConcurrentLRUCache.EvictionListener<K,V> evictionListener)

    - #### ConcurrentLRUCache

          public ConcurrentLRUCache(int size,
                                    int lowerWatermark)

  - ### Method Detail

    - #### setAlive

          public void setAlive(boolean live)

    - #### get

          public V get(K key)

      Specified by:
        `get` in interface `Cache<K,V>`

    - #### remove

          public V remove(K key)

      Specified by:
        `remove` in interface `Cache<K,V>`

    - #### put

          public V put(K key,
                       V val)

      Specified by:
        `put` in interface `Cache<K,V>`

    - #### getOldestAccessedItems

          public Map<K,V> getOldestAccessedItems(int n)

      Returns 'n' number of oldest accessed entries present in this cache. This uses a TreeSet to collect the 'n' oldest items ordered by ascending last access time and returns a LinkedHashMap containing 'n' or less than 'n' entries.
      Parameters:
        `n` - the number of oldest items needed
      Returns:
        a LinkedHashMap containing 'n' or less than 'n' entries

    - #### getLatestAccessedItems

          public Map<K,V> getLatestAccessedItems(int n)

    - #### size

          public int size()

    - #### clear

          public void clear()

      Specified by:
        `clear` in interface `Cache<K,V>`

    - #### getMap

          public Map<Object,ConcurrentLRUCache.CacheEntry<K,V>> getMap()

    - #### destroy

          public void destroy()

    - #### getStats

          public ConcurrentLRUCache.Stats getStats()

    - #### finalize

          protected void finalize()
                           throws Throwable

      Overrides:
        `finalize` in class `Object`
      Throws:
        `Throwable`

Copyright © 2000-2016 Apache Software Foundation. All Rights Reserved.