Class ConcurrentCache<K,​V>

  • Type Parameters:
    K - the key type
    V - the value type

    public final class ConcurrentCache<K,​V>
    extends java.lang.Object
    A thread-safe cache that uses an eden space backed by a ConcurrentHashMap and a long-term space backed by a WeakHashMap. When the eden space reaches the configured size, its contents are promoted to the long-term space.
    • Constructor Summary

      Constructors 
      Constructor Description
      ConcurrentCache​(int size)
      Creates a new cache with the specified eden space size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      V get​(K k)
      Retrieves a value from the cache.
      void put​(K k, V v)
      Stores a value in the cache.
      • Methods inherited from class java.lang.Object

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

      • ConcurrentCache

        public ConcurrentCache​(int size)
        Creates a new cache with the specified eden space size.
        Parameters:
        size - the maximum size of the eden space before promotion to long-term storage
    • Method Detail

      • get

        public V get​(K k)
        Retrieves a value from the cache. If found in the long-term store, it is promoted to the eden space for faster subsequent access. As for put(Object, Object), if the eden space is full, its contents are first moved to the long-term store so that the eden space never grows beyond the configured size.
        Parameters:
        k - the key to look up
        Returns:
        the cached value, or null if not found
      • put

        public void put​(K k,
                        V v)
        Stores a value in the cache. If the eden space is full, all eden entries are promoted to the long-term store before adding the new entry.
        Parameters:
        k - the key
        v - the value to cache