Class Cache<K,​V>

  • Type Parameters:
    K - the type of keys.
    V - the type of values.
    All Implemented Interfaces:
    Disposable

    public class Cache<K,​V>
    extends Object
    implements Disposable
    Provides a cache facility that allows to store key/pair values with an expiration option. When a pair is expired, i.e. the number of second the pair has not been requested is greater than the desired expiration setting, the pair is automatically removed from the cache.

    Clients may subclass this class.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static long NO_EXPIRATION
      A negative integer to be used as expiration setting when no expiration is desired, i.e.
    • Constructor Summary

      Constructors 
      Constructor Description
      Cache​(long expiration)
      Instantiates a cache that uses the specified default expiration setting.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clean()
      Removes the cache entries that are expired.
      boolean containsKey​(K key)
      Determines whether the cache contains an entry matching the specified key.
      void dispose()
      Disposes the cache, which also disposes each cached value that implements Disposable.
      V get​(K key)
      Returns the value assigned to the specified key in the cache.
      long getExpiration()
      Returns the default cache expiration, i.e.
      long getExpiration​(K key)
      Returns the cache expiration for the specified key, i.e.
      void put​(K key, V object)
      Changes the value assigned to the specified key in the cache.
      void remove​(K key)
      Removes the cache entry matching the specified key.
      void removeExpiration​(K key)
      Remove the specific expiration assigned to the cache entry identified by the specified key.
      void setExpiration​(long expiration)
      Changes the default cache expiration, i.e.
      void setExpiration​(K key, long expiration)
      Changes the cache expiration for the specified key, i.e.
      Map<K,​V> toMap()
      Copies the content of this cache into a Map instance.
    • Field Detail

      • NO_EXPIRATION

        public static final long NO_EXPIRATION
        A negative integer to be used as expiration setting when no expiration is desired, i.e. no cache entry shall be automatically removed.
        See Also:
        setExpiration(long), Constant Field Values
    • Constructor Detail

      • Cache

        public Cache​(long expiration)
        Instantiates a cache that uses the specified default expiration setting.
        Parameters:
        expiration - the number of milliseconds after which a cache entry is considered expired. A negative number disables the expiration monitoring.
    • Method Detail

      • getExpiration

        public long getExpiration()
        Returns the default cache expiration, i.e. the number of milliseconds after which a cache entry is considered expired.
        Returns:
        the default number of milliseconds after which a cache entry is considered expired.
        See Also:
        getExpiration(Object)
      • setExpiration

        public void setExpiration​(long expiration)
        Changes the default cache expiration, i.e. the default number of milliseconds after which a cache entry is considered expired.
        Parameters:
        expiration - the new number of milliseconds after which a cache entry is considered expired. A negative number disables the expiration monitoring.
        See Also:
        setExpiration(Object, long)
      • getExpiration

        public long getExpiration​(K key)
        Returns the cache expiration for the specified key, i.e. the number of milliseconds after which this cache entry is considered expired.
        Parameters:
        key - the key identifying the cache entry for which the expiration is to determine.
        Returns:
        the number of milliseconds after which the cache entry is considered expired.
        See Also:
        getExpiration()
      • setExpiration

        public void setExpiration​(K key,
                                  long expiration)
        Changes the cache expiration for the specified key, i.e. the number of milliseconds after which this cache entry is considered expired.
        Parameters:
        key - the key identifying the cache entry for which the expiration is to change.
        expiration - the new number of milliseconds after which this cache entry is considered expired. A negative number disables the expiration monitoring.
        See Also:
        setExpiration(long)
      • removeExpiration

        public void removeExpiration​(K key)
        Remove the specific expiration assigned to the cache entry identified by the specified key. As a result, the default expiration is going to be used for this cache entry.
        Parameters:
        key - the key identifying the cache entry for which the specific expiration is to remove.
        See Also:
        getExpiration()
      • containsKey

        public boolean containsKey​(K key)
        Determines whether the cache contains an entry matching the specified key.
        Parameters:
        key - the cache entry to look for.
        Returns:
        true if the key is found in the cache, false otherwise.
      • get

        public V get​(K key)
        Returns the value assigned to the specified key in the cache.
        Parameters:
        key - the cache entry to look for.
        Returns:
        the cached value, null if none.
      • put

        public void put​(K key,
                        V object)
        Changes the value assigned to the specified key in the cache.
        Parameters:
        key - the cache entry to look for.
        object - the value to put in the cache.
      • remove

        public void remove​(K key)
        Removes the cache entry matching the specified key.
        Parameters:
        key - the cache entry to look for.
      • clean

        public void clean()
        Removes the cache entries that are expired. MDWorkbench Server periodically performs a cleanup of all known cache instances, therefore client generally doesn't need to call this method on his own.
        See Also:
        getExpiration()
      • dispose

        public void dispose()
        Disposes the cache, which also disposes each cached value that implements Disposable.
        Specified by:
        dispose in interface Disposable
      • toMap

        public Map<K,​V> toMap()
        Copies the content of this cache into a Map instance.
        Returns:
        a non-modifiable Map instance which contains all cache entries.