com.thinkaurelius.titan.graphdb.util

## Class ConcurrentLRUCache<V>

- java.lang.Object
- - com.thinkaurelius.titan.graphdb.util.ConcurrentLRUCache<V>

-     public class ConcurrentLRUCache<V>
      extends Object

  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.

  ADDED COMMENT: This class has been copied from the Apache Solr project (see license above). New method has been added "putIfAbsent" which has the same behaviour as normal CHM.putIfAbsent but cache maintenance operations are only done in context of a winner thread, in other words, whoever puts absent item to the map would run cache maintenance ops, everybody else would be just newly added item returned.
  Since:
    solr 1.4

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `ConcurrentLRUCache.EvictionListener<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<V> evictionListener)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `clear()`
    `boolean` | `containsKey(Long key)`
    `void` | `destroy()`
    `protected void` | `finalize()`
    `V` | `get(Long key)`
    `Map<Long,V>` | `getLatestAccessedItems(int n)`
    `Map<Long,com.thinkaurelius.titan.graphdb.util.ConcurrentLRUCache.CacheEntry<Long,V>>` | `getMap()`
    `Map<Long,V>` | `getOldestAccessedItems(int n)` Returns 'n' number of oldest accessed entries present in this cache.
    `ConcurrentLRUCache.Stats` | `getStats()`
    `V` | `put(Long key, V val)`
    `V` | `putIfAbsent(Long key, V val)`
    `V` | `remove(Long key)`
    `void` | `setAlive(boolean live)`
    `int` | `size()`

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

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

- - ### Constructor Detail

    - #### ConcurrentLRUCache

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

    - #### ConcurrentLRUCache

          public ConcurrentLRUCache(int size,
                            int lowerWatermark)

  - ### Method Detail

    - #### setAlive

          public void setAlive(boolean live)

    - #### get

          public V get(Long key)

    - #### containsKey

          public boolean containsKey(Long key)

    - #### remove

          public V remove(Long key)

    - #### putIfAbsent

          public V putIfAbsent(Long key,
                      V val)

    - #### put

          public V put(Long key,
              V val)

    - #### getOldestAccessedItems

          public Map<Long,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<Long,V> getLatestAccessedItems(int n)

    - #### size

          public int size()

    - #### clear

          public void clear()

    - #### getMap

          public Map<Long,com.thinkaurelius.titan.graphdb.util.ConcurrentLRUCache.CacheEntry<Long,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 © 2012–2014. All rights reserved.