Class SoftMapCache


  • public class SoftMapCache
    extends java.lang.Object
    Provides a simple cache using soft references and storing the values in a Map. The keys into the Map are hard references, the values are referenced through soft references. The collected values are cleaned up through a ReferenceQueue.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static org.apache.commons.logging.Log log
      logger
      private java.util.Map map  
      private java.lang.ref.ReferenceQueue refQueue  
    • Constructor Summary

      Constructors 
      Constructor Description
      SoftMapCache​(boolean synched)
      Creates a new soft cache.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void checkReferenceQueue()
      Checks the reference queue if any references have been cleared and removes them from the cache.
      void clear()
      Clears the cache.
      void doHouseKeeping()
      Triggers some house-keeping, i.e.
      java.lang.Object get​(java.lang.Object key)
      Returns the value associated with the given key.
      private java.lang.Object getReference​(java.lang.Object key, java.lang.ref.Reference ref)  
      void put​(java.lang.Object key, java.lang.Object value)
      Put a new value in the cache overwriting any existing value with the same key.
      java.lang.Object remove​(java.lang.Object key)
      Removed the value associated with the given key.
      private java.lang.ref.Reference wrapInReference​(java.lang.Object obj, java.lang.Object key)  
      • Methods inherited from class java.lang.Object

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

      • log

        private static org.apache.commons.logging.Log log
        logger
      • map

        private java.util.Map map
      • refQueue

        private java.lang.ref.ReferenceQueue refQueue
    • Constructor Detail

      • SoftMapCache

        public SoftMapCache​(boolean synched)
        Creates a new soft cache.
        Parameters:
        synched - true if the Map containing the values should by synchronized
    • Method Detail

      • get

        public java.lang.Object get​(java.lang.Object key)
        Returns the value associated with the given key. If the value is not found or the value has been collected, null is returned.
        Parameters:
        key - the key
        Returns:
        the requested value or null
      • remove

        public java.lang.Object remove​(java.lang.Object key)
        Removed the value associated with the given key. The value that is removed is returned as the methods result. If the value is not found or the value has been collected, null is returned.
        Parameters:
        key - the key
        Returns:
        the requested value or null
      • getReference

        private java.lang.Object getReference​(java.lang.Object key,
                                              java.lang.ref.Reference ref)
      • put

        public void put​(java.lang.Object key,
                        java.lang.Object value)
        Put a new value in the cache overwriting any existing value with the same key.
        Parameters:
        key - The key
        value - the value
      • clear

        public void clear()
        Clears the cache.
      • doHouseKeeping

        public void doHouseKeeping()
        Triggers some house-keeping, i.e. processes any pending objects in the reference queue.
      • wrapInReference

        private java.lang.ref.Reference wrapInReference​(java.lang.Object obj,
                                                        java.lang.Object key)
      • checkReferenceQueue

        private void checkReferenceQueue()
        Checks the reference queue if any references have been cleared and removes them from the cache.