Class MemoryCache<K extends java.io.Serializable,​V>

  • All Implemented Interfaces:
    SimpleCache<K,​V>

    public class MemoryCache<K extends java.io.Serializable,​V>
    extends java.lang.Object
    implements SimpleCache<K,​V>
    A cache backed by a simple ConcurrentHashMap.

    Note: This cache is not transaction-safe. Use it for tests or wrap it appropriately.

    Since:
    3.2
    Author:
    Derek Hulley
    • Constructor Summary

      Constructors 
      Constructor Description
      MemoryCache()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      boolean contains​(K key)  
      V get​(K key)  
      java.util.Collection<K> getKeys()  
      void put​(K key, V value)
      Set the value to store for a given key.
      void remove​(K key)
      Removes the cache entry whether or not the value stored against it is null.
      • Methods inherited from class java.lang.Object

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

      • MemoryCache

        public MemoryCache()
    • Method Detail

      • contains

        public boolean contains​(K key)
        Specified by:
        contains in interface SimpleCache<K extends java.io.Serializable,​V>
        Parameters:
        key - the cache key to check up on
        Returns:
        Returns true if there is a cache entry, regardless of whether the value itself is null
      • getKeys

        public java.util.Collection<K> getKeys()
        Specified by:
        getKeys in interface SimpleCache<K extends java.io.Serializable,​V>
      • get

        public V get​(K key)
        Specified by:
        get in interface SimpleCache<K extends java.io.Serializable,​V>
        Parameters:
        key - K
        Returns:
        Returns the value associated with the key. It will be null if the value is null or if the cache doesn't have an entry.
      • put

        public void put​(K key,
                        V value)
        Description copied from interface: SimpleCache
        Set the value to store for a given key.

        Be sure to use remove if cache entries need to be removed as the cache implementations must treat a null value as a first class object in exactly the same way as a Map will allow storage and retrieval of null values.

        Specified by:
        put in interface SimpleCache<K extends java.io.Serializable,​V>
        Parameters:
        key - the key against which to store the value
        value - the value to store. null is allowed.
      • remove

        public void remove​(K key)
        Description copied from interface: SimpleCache
        Removes the cache entry whether or not the value stored against it is null.
        Specified by:
        remove in interface SimpleCache<K extends java.io.Serializable,​V>
        Parameters:
        key - the key value to remove
      • clear

        public void clear()
        Specified by:
        clear in interface SimpleCache<K extends java.io.Serializable,​V>