Class LRUCache<K,​V>


  • public class LRUCache<K,​V>
    extends java.lang.Object
    An LRU cache, based on LinkedHashMap. Synthesized and simplified from various published examples of the genre. The methods are not synchronized.
    • Constructor Summary

      Constructors 
      Constructor Description
      LRUCache​(int cacheSize)
      Creates a new LRU cache.
      LRUCache​(int cacheSize, boolean concurrent)
      Creates a new LRU cache, with the option of making it thread-safe
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clear the cache
      V get​(K key)
      Retrieves an entry from the cache.
      The retrieved entry becomes the most recently used entry.
      void put​(K key, V value)
      Adds an entry to this cache.
      int size()
      Get the number of entries in the cache
      • Methods inherited from class java.lang.Object

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

      • LRUCache

        public LRUCache​(int cacheSize)
        Creates a new LRU cache.
        Parameters:
        cacheSize - the maximum number of entries that will be kept in this cache.
      • LRUCache

        public LRUCache​(int cacheSize,
                        boolean concurrent)
        Creates a new LRU cache, with the option of making it thread-safe
        Parameters:
        cacheSize - the maximum number of entries that will be kept in this cache.
        concurrent - set to true if concurrent access is required, so that access will be synchronized
    • Method Detail

      • get

        public V get​(K key)
        Retrieves an entry from the cache.
        The retrieved entry becomes the most recently used entry.
        Parameters:
        key - the key whose associated value is to be returned.
        Returns:
        the value associated to this key, or null if no value with this key exists in the cache.
      • put

        public void put​(K key,
                        V value)
        Adds an entry to this cache. If the cache is full, the LRU (least recently used) entry is dropped.
        Parameters:
        key - the key with which the specified value is to be associated.
        value - a value to be associated with the specified key.
      • clear

        public void clear()
        Clear the cache
      • size

        public int size()
        Get the number of entries in the cache
        Returns:
        the number of entries