java.lang.Object
net.i2p.util.TryCache<T>
- Type Parameters:
T- type of objects cached
- Direct Known Subclasses:
ByteCache
A thread-safe, lock-free-ish object cache that reuses objects to reduce allocation overhead. Designed to avoid object loss due to lock contention.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFactory interface for creating new objects in TryCache.Field Summary
FieldsModifier and TypeFieldDescriptionprivate longObjects taken from the cache.private longObjects dropped instead of cached.private longWhen the cache last ran empty.private longTimes the cache was empty.private longObjects returned to the cache.private intMost objects to keep cached.private static final booleanTrue to log duplicate releases.private final TryCache.ObjectFactory<T> The factory making missing objects.private final ConcurrentLinkedDeque<T> The objects held in the cache.Constructor Summary
ConstructorsConstructorDescriptionTryCache(TryCache.ObjectFactory<T> factory, int capacity) Stores the factory and clamps a negative capacity to zero.Method Summary
All MethodsModifier and TypeMethodDescriptionacquire()Acquire an object from the cache or create a new one.voidclear()Clear all cached items.longReturn total acquire() count (hits + misses).intReturn the current cache capacity.longReturn the number ofrelease(T)calls that were discarded because the cache was full.longWhen the cache last fell below its target size.longReturn the number ofacquire()calls that found an empty cache (misses required allocating a new object).longReturn total release() count (returned to cache).voidTry to return the item to the cache.voidresize(int newCapacity) Resize the cache to a new capacity.voidshrink(int targetSize) Shrink the cache to the target size, evicting excess items.intsize()Number of items currently held.booleanwasUnderfilled(long thresholdMillis) Check if the cache has been underfilled longer than the threshold.
Field Details
DEBUG_DUP
factory
The factory making missing objects.capacity
private volatile int capacityMost objects to keep cached.items
The objects held in the cache._lastUnderflow
private volatile long _lastUnderflowWhen the cache last ran empty._acquireCount
private volatile long _acquireCountObjects taken from the cache._missCount
private volatile long _missCountTimes the cache was empty._releaseCount
private volatile long _releaseCountObjects returned to the cache._discardCount
private volatile long _discardCountObjects dropped instead of cached.
Constructor Details
TryCache
Stores the factory and clamps a negative capacity to zero.- Parameters:
factory- the source of instances to create when the cache is emptycapacity- the greatest number of instances to retain; negatives clamp to 0
Method Details
acquire
Acquire an object from the cache or create a new one.- Returns:
- a cached or newly created object
release
Try to return the item to the cache. If the cache is full, the item is discarded.- Parameters:
item- the value to hand back to the cache for reuse
clear
public void clear()Clear all cached items.size
public int size()Number of items currently held.- Returns:
- the count of cached items, 0 to capacity
wasUnderfilled
public boolean wasUnderfilled(long thresholdMillis) Check if the cache has been underfilled longer than the threshold.- Parameters:
thresholdMillis- the threshold in milliseconds- Returns:
- true if the cache has been underfilled longer than the threshold
shrink
public void shrink(int targetSize) Shrink the cache to the target size, evicting excess items.- Parameters:
targetSize- the desired maximum number of items
resize
public void resize(int newCapacity) Resize the cache to a new capacity. If shrinking, excess items are evicted. If growing, the cache simply allows more items to accumulate on future releases.- Parameters:
newCapacity- the new maximum number of entries
getLastUnderflowTime
public long getLastUnderflowTime()When the cache last fell below its target size.- Returns:
- the time in milliseconds since the epoch
getCapacity
public int getCapacity()Return the current cache capacity.- Returns:
- the maximum number of cached items
getMissCount
public long getMissCount()Return the number ofacquire()calls that found an empty cache (misses required allocating a new object).- Returns:
- the miss count
getDiscardCount
public long getDiscardCount()Return the number ofrelease(T)calls that were discarded because the cache was full.- Returns:
- the discard count
getAcquireCount
public long getAcquireCount()Return total acquire() count (hits + misses).- Returns:
- the acquire count
getReleaseCount
public long getReleaseCount()Return total release() count (returned to cache).- Returns:
- the release count