Class SimpleLookupCache<K,V>

java.lang.Object
tools.jackson.databind.util.SimpleLookupCache<K,V>
All Implemented Interfaces:
Serializable, Snapshottable<LookupCache<K,V>>, LookupCache<K,V>

public class SimpleLookupCache<K,V> extends Object implements LookupCache<K,V>, Serializable
Synchronized cache with bounded size: used for reusing lookup values and lazily instantiated reusable items.

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 initial and maximum size of the contents.

The implementation evicts the least recently used entry when max size is reached; this is implemented by the backing PrivateMaxEntriesMap implementation. Implementation is thread-safe and does NOT require external synchronization

NOTE: in Jackson 2.x this class was named com.fasterxml.jackson.databind.util.LRUMap

See Also:
  • Field Details

    • _initialEntries

      protected final int _initialEntries
    • _maxEntries

      protected final int _maxEntries
    • _map

      protected final transient tools.jackson.databind.util.internal.PrivateMaxEntriesMap<K,V> _map
  • Constructor Details

    • SimpleLookupCache

      public SimpleLookupCache(int initialEntries, int maxEntries)
  • Method Details

    • emptyCopy

      public LookupCache<K,V> emptyCopy()
      Description copied from interface: LookupCache
      Method needed for creating clones but without contents.
      Specified by:
      emptyCopy in interface LookupCache<K,V>
    • snapshot

      public SimpleLookupCache<K,V> snapshot()
      Specified by:
      snapshot in interface Snapshottable<K>
    • put

      public V put(K key, V value)
      Specified by:
      put in interface LookupCache<K,V>
    • putIfAbsent

      public V putIfAbsent(K key, V value)
      Specified by:
      putIfAbsent in interface LookupCache<K,V>
    • get

      public V get(K key)
      Specified by:
      get in interface LookupCache<K,V>
      Returns:
      value associated with key (can return null)
    • clear

      public void clear()
      Description copied from interface: LookupCache
      Method for removing all contents this cache has.
      Specified by:
      clear in interface LookupCache<K,V>
    • size

      public int size()
      Specified by:
      size in interface LookupCache<K,V>
      Returns:
      Number of entries currently in cache: may be approximate, only to be used for diagnostics, metrics reporting
    • contents

      public void contents(BiConsumer<K,V> consumer)
      Description copied from interface: LookupCache
      Method to apply operation on cache contents without exposing them.

      Default implementation throws UnsupportedOperationException. Implementations are required to override this method.

      Specified by:
      contents in interface LookupCache<K,V>
      Parameters:
      consumer - Operation to apply on cache contents.
    • readResolve

      protected Object readResolve()