Class SoftCache<T>

  • Type Parameters:
    T - The concrete data type this cache tracks.

    public abstract class SoftCache<T>
    extends java.lang.Object
    Common code for a simple cache based on Soft References. As items are retrieved from the garbage collector, implementations can validate or reset instances to reusable condition, or signal non-reusability.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected SoftCache​(int each, int traceIndex)  
      protected SoftCache​(int each, int traceIndex, java.lang.String identity)
      Implementations can provide a more concise and/or more informative string to use in place of the simple class name.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      T allocate()
      Obtain a new or recycled object of the given type.
      protected abstract T create()
      When the cache runs empty, further allocations must create new objects.
      void free​(T recycle)
      Return an object of the given type for caching.
      protected java.lang.String[] prune​(long nonReturnThreshold)  
      protected abstract boolean qualified​(T recycled)
      An item returned from the garbage collector might have been changed in a way that makes it unsuitable for reuse.
      static void shutdown()  
      protected void stop()  
      • Methods inherited from class java.lang.Object

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

      • sLogger

        public static final org.slf4j.Logger sLogger
      • FullName

        public static final java.lang.String FullName
      • caches

        protected static final java.util.WeakHashMap<SoftCache<?>,​java.lang.String> caches
      • pruner

        protected static java.lang.Thread pruner
      • nonReturns

        protected final java.util.concurrent.ConcurrentHashMap<java.lang.Integer,​SoftCache.NonReturned> nonReturns
      • returnedMap

        protected final java.util.concurrent.ConcurrentHashMap<java.lang.Integer,​SoftCache.Returned> returnedMap
      • returned

        protected final java.util.concurrent.ConcurrentLinkedDeque<SoftCache.Returned> returned
      • each

        protected final int each
      • traceIndex

        protected final int traceIndex
      • identity

        protected final java.lang.String identity
      • capacity

        protected int capacity
      • sizeEstimate

        protected volatile int sizeEstimate
      • recycles0

        protected long recycles0
      • recycles

        protected long recycles
      • recycleCost

        protected volatile long recycleCost
      • creations0

        protected long creations0
      • creations

        protected long creations
      • creationCost

        protected volatile long creationCost
      • minQueued

        protected int minQueued
      • maxQueued

        protected int maxQueued
      • allocationCost

        protected volatile long allocationCost
    • Constructor Detail

      • SoftCache

        protected SoftCache​(int each,
                            int traceIndex)
      • SoftCache

        protected SoftCache​(int each,
                            int traceIndex,
                            java.lang.String identity)
        Implementations can provide a more concise and/or more informative string to use in place of the simple class name.
        Parameters:
        identity -
    • Method Detail

      • shutdown

        public static void shutdown()
      • prune

        protected java.lang.String[] prune​(long nonReturnThreshold)
      • stop

        protected void stop()
      • qualified

        protected abstract boolean qualified​(T recycled)
        An item returned from the garbage collector might have been changed in a way that makes it unsuitable for reuse. This method gives the implementation a chance to verify that and/or make corrections.
        Parameters:
        recycled - The subject item that is to be placed back in this cache's fast access list.
        Returns:
        True if ready for re-use, false if to be discarded.
      • create

        protected abstract T create()
        When the cache runs empty, further allocations must create new objects. Since the queue is based on soft references, the garbage collector can throw some away at will.
        Returns:
      • allocate

        public T allocate()
        Obtain a new or recycled object of the given type. Collect timing stats on allocation pace to maintain approximately five seconds of buffered objects.
        Returns:
      • free

        public void free​(T recycle)
        Return an object of the given type for caching.
        Parameters:
        recycle -