Package wt.util

Class Cache

All Implemented Interfaces:
Map

public class Cache extends AbstractMap
A fixed size, most recently used object cache. The caches size is set at construction, and only the most recently used entries are maintained. Each get or put operation makes that entry the most recently used. When putting new entries, if the cache is full, the least recently used entry is aged out of the cache.

Supported API: true
Extendable: false
  • Constructor Details

    • Cache

      public Cache(int size)
      Construct a new fixed-size, most-recently-used cache. Sparse arrays of ints are used to maintain linked lists in a memory efficient manner - the maximum size supported by this implementation is 715827882.

      Supported API: true
      Parameters:
      size - the maximum number of entries stored in the cache.
  • Method Details

    • get

      public Object get(Object key)
      Get an entry from the cache. Hit and miss statistics are updated.

      Supported API: true
      Specified by:
      get in interface Map
      Overrides:
      get in class AbstractMap
      Parameters:
      key - the entry key
      Returns:
      the cached object or null if not found
    • put

      public Object put(Object key, Object value)
      Put an entry in the cache. If an entry for the given key already exists, the previous value is discarded. This entry is considered the most recently used.

      Supported API: true
      Specified by:
      put in interface Map
      Overrides:
      put in class AbstractMap
      Parameters:
      key - the entry key
      value - the value to associate with the given key
      Returns:
      The previous value that mapped to the key, or null if there was no previous value
    • getAndReplace

      public Object getAndReplace(Object key, Object value)
      Deprecated.
      Replaced by put(Object,Object)

      Supported API: true
      Get an entry from the cache and replace it with a new entry. The new entry is considered the most recently used. Hit and miss statistics are updated.
      Parameters:
      key - the entry key
      value - the value to associate with the given key
      Returns:
      previous value
    • remove

      public Object remove(Object key)
      Remove an entry from the cache. Does nothing if the entry is not currently in the cache.

      Supported API: true
      Specified by:
      remove in interface Map
      Overrides:
      remove in class AbstractMap
      Parameters:
      key - the entry key
    • getAndRemove

      public Object getAndRemove(Object key)
      Deprecated.
      Replaced by remove(Object)

      Supported API: true
      Get an entry from the cache and remove it at the same time. Does nothing if the entry is not currently in the cache. Hit and miss statistics are updated.
      Parameters:
      key - the entry key
      Returns:
      previous value
    • clear

      public void clear()
      Clears the cache. All entries are removed.

      Supported API: true
      Specified by:
      clear in interface Map
      Overrides:
      clear in class AbstractMap
    • entries

      public Enumeration entries()
      Deprecated.
      Replaced by values().iterator()

      Supported API: true
      Returns an Enumeration of the values in the cache.
    • toString

      public String toString()
      Returns string representation of the cache. The string contains the default string representation of the object plus a summary of the current cache size, hits and misses counts, and aged out entry count.

      Supported API: true
      Overrides:
      toString in class AbstractMap