Class SimpleLRUCache<K,​V>


  • public class SimpleLRUCache<K,​V>
    extends java.lang.Object

    Simple LRU cache for any type of object, based on a LinkedHashMap with a maximum size.

    Author:
    Holger Rehn
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<K,​V> _Map
      map backing the LRU cache
    • Constructor Summary

      Constructors 
      Constructor Description
      SimpleLRUCache​(int limit)
      Constructs a new LRU cache with a limited capacity.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      V get​(K key)
      Get the value associated with the given key, if any.
      V put​(K key, V value)
      Updates the LRU cache by adding a new entry.
      • Methods inherited from class java.lang.Object

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

      • _Map

        private final java.util.Map<K,​V> _Map
        map backing the LRU cache
    • Constructor Detail

      • SimpleLRUCache

        public SimpleLRUCache​(int limit)

        Constructs a new LRU cache with a limited capacity.

        Parameters:
        limit - maximum number of entries in this cache
    • Method Detail

      • put

        public V put​(K key,
                     V value)

        Updates the LRU cache by adding a new entry.

        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
        Returns:
        previous value associated with key or null if there was no mapping for key; a null value can also indicate that the cache previously associated null with the specified key
        See Also:
        Map.put(Object,Object)
      • get

        public V get​(K key)

        Get the value associated with the given key, if any.

        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        the value to which the specified key is mapped, or null if this map contains no mapping for the key
        See Also:
        Map.get(Object)