Package com.automation_pros.odva.util
Class SoftCache<T>
- java.lang.Object
-
- com.automation_pros.odva.util.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
SoftCache.NonReturned
protected class
SoftCache.NonReturnedSum
protected class
SoftCache.Returned
-
Field Summary
Fields Modifier and Type Field Description protected long
allocationCost
protected static java.util.WeakHashMap<SoftCache<?>,java.lang.String>
caches
protected int
capacity
protected long
creationCost
protected long
creations
protected long
creations0
protected int
each
static java.lang.String
FullName
protected java.lang.String
identity
protected int
maxQueued
protected int
minQueued
protected java.util.concurrent.ConcurrentHashMap<java.lang.Integer,SoftCache.NonReturned>
nonReturns
protected static java.lang.Thread
pruner
protected long
recycleCost
protected long
recycles
protected long
recycles0
protected java.util.concurrent.ConcurrentLinkedDeque<SoftCache.Returned>
returned
protected java.util.concurrent.ConcurrentHashMap<java.lang.Integer,SoftCache.Returned>
returnedMap
protected int
sizeEstimate
static org.slf4j.Logger
sLogger
protected int
traceIndex
-
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()
-
-
-
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
-
-
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
-
-
-