Class LRUMap<K,​V>

  • All Implemented Interfaces:
    java.io.Serializable

    public class LRUMap<K,​V>
    extends java.lang.Object
    implements java.io.Serializable
    Helper for simple bounded maps used for reusing lookup values.

    Note that serialization behavior is such that contents are NOT serialized, on assumption that all use cases are for caching where persistence does not make sense. The only thing serialized is the cache size of Map.

    NOTE: since version 2.4.2, this is NOT an LRU-based at all; reason being that it is not possible to use JDK components that do LRU _AND_ perform well wrt synchronization on multi-core systems. So we choose efficient synchronization over potentially more efficient handling of entries.

    And yes, there are efficient LRU implementations such as concurrentlinkedhashmap; but at this point we really try to keep external deps to minimum. But perhaps a shaded variant may be used one day.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int _jdkSerializeMaxEntries
      Ugly hack, to work through the requirement that _value is indeed final, and that JDK serialization won't call ctor(s) if Serializable is implemented.
      protected java.util.concurrent.ConcurrentHashMap<K,​V> _map  
      protected int _maxEntries  
      private static long serialVersionUID  
    • Constructor Summary

      Constructors 
      Constructor Description
      LRUMap​(int initialEntries, int maxEntries)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      V get​(java.lang.Object key)  
      V put​(K key, V value)  
      V putIfAbsent​(K key, V value)  
      private void readObject​(java.io.ObjectInputStream in)  
      protected java.lang.Object readResolve()  
      int size()  
      private void writeObject​(java.io.ObjectOutputStream out)  
      • Methods inherited from class java.lang.Object

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

      • _maxEntries

        protected final transient int _maxEntries
      • _map

        protected final transient java.util.concurrent.ConcurrentHashMap<K,​V> _map
      • _jdkSerializeMaxEntries

        protected transient int _jdkSerializeMaxEntries
        Ugly hack, to work through the requirement that _value is indeed final, and that JDK serialization won't call ctor(s) if Serializable is implemented.
        Since:
        2.1
    • Constructor Detail

      • LRUMap

        public LRUMap​(int initialEntries,
                      int maxEntries)
    • Method Detail

      • put

        public V put​(K key,
                     V value)
      • putIfAbsent

        public V putIfAbsent​(K key,
                             V value)
        Since:
        2.5
      • get

        public V get​(java.lang.Object key)
      • clear

        public void clear()
      • size

        public int size()
      • readObject

        private void readObject​(java.io.ObjectInputStream in)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • writeObject

        private void writeObject​(java.io.ObjectOutputStream out)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • readResolve

        protected java.lang.Object readResolve()