Class WeakCache<K,​V>


  • public abstract class WeakCache<K,​V>
    extends java.lang.Object
    A simple cache with weak keys. get may be called safely with good concurrency by multiple threads. In order to use this, some reasonable properties are expected:
    • The value is a function of only the key, so it may be safely cached.
    • get operations are very common on the same key.
    • Values may occasionally disappear from the cache, in which case they will just be recomputed on the next get() call.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.locks.ReadWriteLock lock  
      private java.util.Map<K,​V> map  
    • Constructor Summary

      Constructors 
      Constructor Description
      WeakCache()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Remove all entries from the cache.
      V get​(K key)
      Return the value (if any) associated with key.
      protected abstract V lookup​(K key)
      Must be implemented in a subclass.
      V remove​(K key)
      Remove any value associated with the key.
      • Methods inherited from class java.lang.Object

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

      • lock

        private final java.util.concurrent.locks.ReadWriteLock lock
      • map

        private final java.util.Map<K,​V> map
    • Constructor Detail

      • WeakCache

        public WeakCache()
    • Method Detail

      • lookup

        protected abstract V lookup​(K key)
        Must be implemented in a subclass. Must compute a value corresponding to a key. The computation may be fairly expensive. Note that no lock is held during this computation.
        Parameters:
        key - Key value for which a value must be computed.
        Returns:
        The resulting value.
      • remove

        public V remove​(K key)
        Remove any value associated with the key.
        Parameters:
        key - Key to value that may be in cache.
        Returns:
        value from the cache, or null if none.
      • get

        public V get​(K key)
        Return the value (if any) associated with key. If the value is already in the cache, only a read lock is held, so many threads can concurrently call get. If no value is in the cache corresponding to key, a new value will be computed and cached, in which case a write lock is held long enough to update the map. Note that the write lock is NOT held while the value is computed by calling the lookup method. Because of this, it is possible for redundant computation to occur when two or more thread concurrently call get on the same key which is not (yet) in the cache.
        Parameters:
        key -
        Returns:
        Value associated with the key.
      • clear

        public void clear()
        Remove all entries from the cache.