Class CacheMap

  • All Implemented Interfaces:
    java.util.Map

    public class CacheMap
    extends java.lang.Object
    implements java.util.Map
    Fixed-size map that has ability to pin/unpin entries and move overflow to a backing soft map.
    Author:
    Patrick Linskey, Abe White
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected SizedMap cacheMap
      The map for non-expired and non-pinned references.
      protected java.util.Map pinnedMap
      The set of objects pinned into the cache.
      protected SizedMap softMap
      The map for expired references.
    • Constructor Summary

      Constructors 
      Constructor Description
      CacheMap()
      Create a non-LRU (and therefore highly concurrent) cache map with a size of 1000.
      CacheMap​(boolean lru)
      Create a cache map with a size of 1000.
      CacheMap​(boolean lru, int max)
      Create a cache map with the given properties.
      CacheMap​(boolean lru, int max, int size, float load)
      Deprecated.
      CacheMap​(boolean lru, int max, int size, float load, int concurrencyLevel)
      Create a cache map with the given properties.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void cacheMapOverflowRemoved​(java.lang.Object key, java.lang.Object value)
      void clear()
      Removes pinned objects as well as unpinned ones.
      boolean containsKey​(java.lang.Object key)  
      boolean containsValue​(java.lang.Object val)  
      protected void entryAdded​(java.lang.Object key, java.lang.Object value)
      Invoked when an entry is added to the cache.
      protected void entryRemoved​(java.lang.Object key, java.lang.Object value, boolean expired)
      Invoked when a key-value pair is evicted from this data structure.
      java.util.Set entrySet()  
      java.lang.Object get​(java.lang.Object key)  
      int getCacheSize()
      The maximum number of hard references to maintain, or -1 for no limit.
      java.util.Set getPinnedKeys()
      The keys pinned into the map.
      int getSoftReferenceSize()
      The maximum number of soft references to maintain, or -1 for no limit.
      boolean isEmpty()  
      boolean isLRU()
      Whether this cache map uses LRU eviction.
      java.util.Set keySet()  
      boolean pin​(java.lang.Object key)
      Locks the given key and its value into the map.
      java.lang.Object put​(java.lang.Object key, java.lang.Object value)  
      protected java.lang.Object put​(java.util.Map map, java.lang.Object key, java.lang.Object value)
      Put the given entry into the given map.
      void putAll​(java.util.Map map)  
      void putAll​(java.util.Map map, boolean replaceExisting)  
      void readLock()
      Acquire read lock.
      void readUnlock()
      Release read lock.
      java.lang.Object remove​(java.lang.Object key)
      If key is pinned into the cache, the pin is cleared and the object is removed.
      protected java.lang.Object remove​(java.util.Map map, java.lang.Object key)
      Remove the given key from the given map.
      void setCacheSize​(int size)
      The maximum number of hard references to maintain, or -1 for no limit.
      void setSoftReferenceSize​(int size)
      The maximum number of soft references to maintain, or -1 for no limit.
      int size()  
      protected void softMapOverflowRemoved​(java.lang.Object key, java.lang.Object value)
      protected void softMapValueExpired​(java.lang.Object key)
      Called when a value expires from the soft map.
      java.lang.String toString()  
      boolean unpin​(java.lang.Object key)
      Undo a pinning.
      java.util.Collection values()  
      void writeLock()
      Acquire write lock.
      void writeUnlock()
      Release write lock.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Field Detail

      • cacheMap

        protected final SizedMap cacheMap
        The map for non-expired and non-pinned references.
      • softMap

        protected final SizedMap softMap
        The map for expired references.
      • pinnedMap

        protected final java.util.Map pinnedMap
        The set of objects pinned into the cache.
    • Constructor Detail

      • CacheMap

        public CacheMap()
        Create a non-LRU (and therefore highly concurrent) cache map with a size of 1000.
      • CacheMap

        public CacheMap​(boolean lru)
        Create a cache map with a size of 1000.
        Parameters:
        lru - if true, create a LRU cache map otherwise a non-LRU map will be created.
      • CacheMap

        public CacheMap​(boolean lru,
                        int max)
        Create a cache map with the given properties.
      • CacheMap

        public CacheMap​(boolean lru,
                        int max,
                        int size,
                        float load,
                        int concurrencyLevel)
        Create a cache map with the given properties.
        Since:
        1.1.0
    • Method Detail

      • softMapValueExpired

        protected void softMapValueExpired​(java.lang.Object key)
        Called when a value expires from the soft map.
      • put

        protected java.lang.Object put​(java.util.Map map,
                                       java.lang.Object key,
                                       java.lang.Object value)
        Put the given entry into the given map. Allows subclasses to take additional actions.
      • remove

        protected java.lang.Object remove​(java.util.Map map,
                                          java.lang.Object key)
        Remove the given key from the given map. Allows subclasses to take additional actions.
      • readLock

        public void readLock()
        Acquire read lock.
      • readUnlock

        public void readUnlock()
        Release read lock.
      • writeLock

        public void writeLock()
        Acquire write lock.
      • writeUnlock

        public void writeUnlock()
        Release write lock.
      • isLRU

        public boolean isLRU()
        Whether this cache map uses LRU eviction.
      • setCacheSize

        public void setCacheSize​(int size)
        The maximum number of hard references to maintain, or -1 for no limit.
      • getCacheSize

        public int getCacheSize()
        The maximum number of hard references to maintain, or -1 for no limit.
      • setSoftReferenceSize

        public void setSoftReferenceSize​(int size)
        The maximum number of soft references to maintain, or -1 for no limit.
      • getSoftReferenceSize

        public int getSoftReferenceSize()
        The maximum number of soft references to maintain, or -1 for no limit.
      • getPinnedKeys

        public java.util.Set getPinnedKeys()
        The keys pinned into the map.
      • pin

        public boolean pin​(java.lang.Object key)
        Locks the given key and its value into the map. Objects pinned into the map are not counted towards the maximum cache size, and are never evicted implicitly. You may pin keys for which no value is in the map.
        Returns:
        true if the givne key's value was pinned; false if no value for the given key is cached
      • unpin

        public boolean unpin​(java.lang.Object key)
        Undo a pinning.
      • entryRemoved

        protected void entryRemoved​(java.lang.Object key,
                                    java.lang.Object value,
                                    boolean expired)
        Invoked when a key-value pair is evicted from this data structure. This is invoked with expired set to true when an object is dropped because of space requirements or through garbage collection of soft references. It is invoked with expired set to false when an object is explicitly removed via the remove(java.util.Map, java.lang.Object) or clear() methods. This may be invoked more than once for a given entry.
        Parameters:
        value - may be null if the value was a soft reference that has been GCd
        Since:
        0.2.5.0
      • entryAdded

        protected void entryAdded​(java.lang.Object key,
                                  java.lang.Object value)
        Invoked when an entry is added to the cache. This may be invoked more than once for an entry.
      • get

        public java.lang.Object get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map
      • put

        public java.lang.Object put​(java.lang.Object key,
                                    java.lang.Object value)
        Specified by:
        put in interface java.util.Map
      • putAll

        public void putAll​(java.util.Map map)
        Specified by:
        putAll in interface java.util.Map
      • putAll

        public void putAll​(java.util.Map map,
                           boolean replaceExisting)
      • remove

        public java.lang.Object remove​(java.lang.Object key)
        If key is pinned into the cache, the pin is cleared and the object is removed.
        Specified by:
        remove in interface java.util.Map
      • clear

        public void clear()
        Removes pinned objects as well as unpinned ones.
        Specified by:
        clear in interface java.util.Map
      • size

        public int size()
        Specified by:
        size in interface java.util.Map
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map
      • containsValue

        public boolean containsValue​(java.lang.Object val)
        Specified by:
        containsValue in interface java.util.Map
      • keySet

        public java.util.Set keySet()
        Specified by:
        keySet in interface java.util.Map
      • values

        public java.util.Collection values()
        Specified by:
        values in interface java.util.Map
      • entrySet

        public java.util.Set entrySet()
        Specified by:
        entrySet in interface java.util.Map
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object