Class BoundedLocalCache<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- com.github.benmanes.caffeine.cache.BLCHeader.PadDrainStatus<K,V>
-
- com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef<K,V>
-
- com.github.benmanes.caffeine.cache.BoundedLocalCache<K,V>
-
- Type Parameters:
K- the type of keys maintained by this cacheV- the type of mapped values
- All Implemented Interfaces:
LocalCache<K,V>,java.util.concurrent.ConcurrentMap<K,V>,java.util.Map<K,V>
- Direct Known Subclasses:
LocalCacheFactory.SI,LocalCacheFactory.SS,LocalCacheFactory.WI,LocalCacheFactory.WS
@ThreadSafe abstract class BoundedLocalCache<K,V> extends BLCHeader.DrainStatusRef<K,V> implements LocalCache<K,V>
An in-memory cache implementation that supports full concurrency of retrievals, a high expected concurrency for updates, and multiple ways to bound the cache.This class is abstract and code generated subclasses provide the complete implementation for a particular configuration. This is to ensure that only the fields and execution paths necessary for a given configuration are used.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classBoundedLocalCache.AddTaskAdds the node to the page replacement policy.(package private) static classBoundedLocalCache.BoundedLocalAsyncLoadingCache<K,V>(package private) static classBoundedLocalCache.BoundedLocalLoadingCache<K,V>(package private) static classBoundedLocalCache.BoundedLocalManualCache<K,V>(package private) static classBoundedLocalCache.BoundedPolicy<K,V>(package private) static classBoundedLocalCache.EntryIterator<K,V>An adapter to safely externalize the entry iterator.(package private) static classBoundedLocalCache.EntrySetView<K,V>An adapter to safely externalize the entries.(package private) static classBoundedLocalCache.EntrySpliterator<K,V>An adapter to safely externalize the entry spliterator.(package private) static classBoundedLocalCache.KeyIterator<K,V>An adapter to safely externalize the key iterator.(package private) static classBoundedLocalCache.KeySetView<K,V>An adapter to safely externalize the keys.(package private) static classBoundedLocalCache.KeySpliterator<K,V>An adapter to safely externalize the key spliterator.(package private) classBoundedLocalCache.PerformCleanupTaskA reusable task that performs the maintenance work; used to avoid wrapping by ForkJoinPool.(package private) classBoundedLocalCache.RemovalTaskRemoves a node from the page replacement policy.(package private) classBoundedLocalCache.UpdateTaskUpdates the weighted size and evicts an entry on overflow.(package private) static classBoundedLocalCache.ValueIterator<K,V>An adapter to safely externalize the value iterator.(package private) static classBoundedLocalCache.ValueSpliterator<K,V>An adapter to safely externalize the value spliterator.(package private) static classBoundedLocalCache.ValuesView<K,V>An adapter to safely externalize the values.
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.function.Consumer<Node<K,V>>accessPolicy(package private) CacheLoader<K,V>cacheLoader(package private) java.util.concurrent.ConcurrentHashMap<java.lang.Object,Node<K,V>>data(package private) BoundedLocalCache.PerformCleanupTaskdrainBuffersTask(package private) java.util.Set<java.util.Map.Entry<K,V>>entrySet(package private) java.util.concurrent.locks.LockevictionLock(package private) java.util.concurrent.Executorexecutor(package private) static longEXPIRE_WRITE_TOLERANCEThe maximum time window between entry updates before the expiration must be reordered.(package private) booleanisAsync(package private) java.util.Set<K>keySet(package private) static java.util.logging.Loggerlogger(package private) static longMAXIMUM_CAPACITYThe maximum weighted capacity of the map.(package private) static intNCPUThe number of CPUs(package private) NodeFactorynodeFactory(package private) static doublePERCENT_MAINThe percent of the maximum weighted capacity dedicated to the main space.(package private) static doublePERCENT_MAIN_PROTECTEDThe percent of the maximum weighted capacity dedicated to the main's protected space.(package private) Buffer<Node<K,V>>readBuffer(package private) java.util.Collection<V>values(package private) Weigher<K,V>weigher(package private) static intWRITE_BUFFER_MAXThe maximum capacity of the write buffer.(package private) static intWRITE_BUFFER_MINThe initial capacity of the write buffer.(package private) static intWRITE_BUFFER_RETRIESThe number of attempts to insert into the write buffer before yielding.(package private) CacheWriter<K,V>writer-
Fields inherited from class com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef
DRAIN_STATUS_OFFSET, drainStatus, IDLE, PROCESSING_TO_IDLE, PROCESSING_TO_REQUIRED, REQUIRED
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedBoundedLocalCache(Caffeine<K,V> builder, CacheLoader<K,V> cacheLoader, boolean isAsync)Creates an instance based on the builder's configuration.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected AccessOrderDeque<Node<K,V>>accessOrderEdenDeque()protected AccessOrderDeque<Node<K,V>>accessOrderProbationDeque()protected AccessOrderDeque<Node<K,V>>accessOrderProtectedDeque()(package private) longadjustedWeightedSize()Returns the combined weight of the values in the cache.(package private) booleanadmit(K candidateKey, K victimKey)Determines if the candidate should be accepted into the main space, as determined by its frequency relative to the victim.(package private) voidafterRead(Node<K,V> node, long now, boolean recordHit)Performs the post-processing work required after a read.(package private) voidafterWrite(Node<K,V> node, java.lang.Runnable task, long now)Performs the post-processing work required after a write.protected booleanbuffersWrites()If the page replacement policy buffers writes.(package private) static intceilingPowerOfTwo(int x)voidcleanUp()SeeCache.cleanUp().voidclear()protected booleancollectKeys()Returns if the keys are weak reference garbage collected.protected booleancollectValues()Returns if the values are weak or soft reference garbage collected.Vcompute(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction, boolean recordMiss, boolean recordLoad)SeeConcurrentMap.compute(K, java.util.function.BiFunction<? super K, ? super V, ? extends V>).VcomputeIfAbsent(K key, java.util.function.Function<? super K,? extends V> mappingFunction, boolean recordStats, boolean recordLoad)SeeConcurrentMap.computeIfAbsent(K, java.util.function.Function<? super K, ? extends V>).VcomputeIfPresent(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)booleancontainsKey(java.lang.Object key)booleancontainsValue(java.lang.Object value)(package private) VdoComputeIfAbsent(K key, java.lang.Object keyRef, java.util.function.Function<? super K,? extends V> mappingFunction, long now)Returns the current value from a computeIfAbsent invocation.(package private) voiddrainKeyReferences()Drains the weak key references queue.(package private) voiddrainReadBuffer()Drains the read buffer.(package private) voiddrainValueReferences()Drains the weak / soft value references queue.(package private) voiddrainWriteBuffer()Drains the write buffer.protected longedenMaximum()Returns the maximum weighted size of the eden space.protected longedenWeightedSize()Returns the uncorrected combined weight of the values in the eden space.java.util.Set<java.util.Map.Entry<K,V>>entrySet()longestimatedSize()(package private) voidevictEntries()Evicts entries if the cache exceeds the maximum.(package private) voidevictEntry(Node<K,V> node, RemovalCause cause, long now)Attempts to evict the entry based on the given removal cause.(package private) intevictFromEden()Evicts entries from the eden space into the main space while the eden size exceeds a maximum.(package private) voidevictFromMain(int candidates)Evicts entries from the main space if the cache exceeds the maximum capacity.(package private) java.util.Map<K,V>evictionOrder(int limit, java.util.function.Function<V,V> transformer, boolean hottest)Returns an unmodifiable snapshot map ordered in eviction order, either ascending or descending.protected booleanevicts()Returns if the cache evicts entries due to a maximum size or weight threshold.java.util.concurrent.Executorexecutor()Returns theExecutorused by this cache.TickerexpirationTicker()Returns theTickerused by this cache for expiration.(package private) voidexpireAfterAccessEntries(long now)Expires entries in the access-order queue.(package private) voidexpireAfterAccessEntries(AccessOrderDeque<Node<K,V>> accessOrderDeque, long expirationTime, long now)Expires entries in an access-order queue.(package private) java.util.Map<K,V>expireAfterAcessOrder(int limit, java.util.function.Function<V,V> transformer, boolean oldest)Returns an unmodifiable snapshot map ordered in access expiration order, either ascending or descending.(package private) voidexpireAfterWriteEntries(long now)Expires entries on the write-order queue.(package private) java.util.Map<K,V>expireAfterWriteOrder(int limit, java.util.function.Function<V,V> transformer, boolean oldest)Returns an unmodifiable snapshot map ordered in write expiration order, either ascending or descending.(package private) voidexpireEntries()Expires entries that have expired in the access and write queues.protected booleanexpiresAfterAccess()Returns if the cache expires entries after an access time threshold.protected longexpiresAfterAccessNanos()How long after the last access to an entry the map will retain that entry.protected booleanexpiresAfterWrite()Returns if the cache expires entries after an write time threshold.protected longexpiresAfterWriteNanos()How long after the last write to an entry the map will retain that entry.protected booleanfastpath()Returns if an access to an entry can skip notifying the eviction policy.protected FrequencySketch<K>frequencySketch()Vget(java.lang.Object key)java.util.Map<K,V>getAllPresent(java.lang.Iterable<?> keys)VgetIfPresent(java.lang.Object key, boolean recordStats)VgetIfPresentQuietly(java.lang.Object key, long[] writeTime)(package private) booleanhasExpired(Node<K,V> node, long now)Returns if the entry has expired.booleanhasRemovalListener()Returns whether this cache notifies when an entry is removed.protected booleanhasWriter()Returns whether this cache notifies a writer when an entry is modified.booleanhasWriteTime()Returns whether the cache captures the write time of the entry.(package private) booleanisComputingAsync(Node<?,?> node)Returns if the node's value is currently being computed, asynchronously.booleanisEmpty()booleanisRecordingStats()Returns whether this cache has statistics enabled.protected booleanisWeighted()Returns if entries may be assigned different weights.protected java.lang.ref.ReferenceQueue<K>keyReferenceQueue()java.util.Set<K>keySet()protected voidlazySetEdenMaximum(long maximum)protected voidlazySetEdenWeightedSize(long weightedSize)protected voidlazySetMainProtectedMaximum(long maximum)protected voidlazySetMainProtectedWeightedSize(long weightedSize)protected voidlazySetMaximum(long maximum)protected voidlazySetWeightedSize(long weightedSize)protected longmainProtectedMaximum()Returns the maximum weighted size of the main's protected space.protected longmainProtectedWeightedSize()Returns the uncorrected combined weight of the values in the main's protected space.(package private) voidmaintenance(java.lang.Runnable task)Performs the pending maintenance work and sets the state flags during processing to avoid excess scheduling attempts.(package private) voidmakeDead(Node<K,V> node)Atomically transitions the node to the dead state and decrements the weightedSize.(package private) static <K,V>
SerializationProxy<K,V>makeSerializationProxy(BoundedLocalCache<?,?> cache, boolean isWeighted)Creates a serialization proxy based on the common configuration shared by all cache types.protected longmaximum()Returns the maximum weighted size.Vmerge(K key, V value, java.util.function.BiFunction<? super V,? super V,? extends V> remappingFunction)voidnotifyRemoval(K key, V value, RemovalCause cause)Asynchronously sends a removal notification to the listener.(package private) voidonAccess(Node<K,V> node)Updates the node's location in the page replacement policy.(package private) voidperformCleanUp(java.lang.Runnable task)Performs the maintenance work, blocking until the lock is acquired.Vput(K key, V value)Vput(K key, V value, boolean notifyWriter)(package private) VputFast(K key, V value, int newWeight, boolean notifyWriter, boolean onlyIfAbsent)Adds a node to the policy and the data store.VputIfAbsent(K key, V value)(package private) VputSlow(K key, V value, int newWeight, boolean notifyWriter, boolean onlyIfAbsent)Adds a node to the policy and the data store.protected booleanrefreshAfterWrite()Returns if the cache refreshes entries after an write time threshold.protected longrefreshAfterWriteNanos()How long after the last write an entry becomes a candidate for refresh.(package private) voidrefreshIfNeeded(Node<K,V> node, long now)Asynchronously refreshes the entry if eligible.(package private) Vremap(K key, java.lang.Object keyRef, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction, long now, boolean computeIfAbsent)Attempts to compute a mapping for the specified key and its current mapped value (ornullif there is no current mapping).RemovalListener<K,V>removalListener()Returns theRemovalListenerused by this cache or null if not used.Vremove(java.lang.Object key)booleanremove(java.lang.Object key, java.lang.Object value)(package private) voidremoveNode(Node<K,V> node, long now)(package private) voidremoveNodes(LinkedDeque<Node<K,V>> deque, long now)(package private) VremoveNoWriter(java.lang.Object key)Removes the mapping for a key without notifying the writer.(package private) VremoveWithWriter(java.lang.Object key)Removes the mapping for a key after notifying the writer.(package private) static <K,V>
voidreorder(LinkedDeque<Node<K,V>> deque, Node<K,V> node)Updates the node's location in the policy's deque.(package private) voidreorderProbation(Node<K,V> node)Promote the node from probation to protected on an access.Vreplace(K key, V value)booleanreplace(K key, V oldValue, V newValue)voidreplaceAll(java.util.function.BiFunction<? super K,? super V,? extends V> function)(package private) voidscheduleAfterWrite()Conditionally schedules the asynchronous maintenance task after a write operation.(package private) voidscheduleDrainBuffers()Attempts to schedule an asynchronous task to apply the pending operations to the page replacement policy.protected voidsetExpiresAfterAccessNanos(long expireAfterAccessNanos)protected voidsetExpiresAfterWriteNanos(long expireAfterWriteNanos)(package private) voidsetMaximum(long maximum)Sets the maximum weighted size of the cache.protected voidsetRefreshAfterWriteNanos(long refreshAfterWriteNanos)intsize()(package private) booleanskipReadBuffer()Returns if the cache should bypass the read buffer.(package private) java.util.Map<K,V>snapshot(java.util.function.Supplier<java.util.Iterator<Node<K,V>>> iteratorSupplier, java.util.function.Function<V,V> transformer, int limit)Returns an unmodifiable snapshot map ordered by the provided iterator.StatsCounterstatsCounter()Returns theStatsCounterused by this cache.TickerstatsTicker()Returns theTickerused by this cache for statistics.protected java.lang.ref.ReferenceQueue<V>valueReferenceQueue()java.util.Collection<V>values()protected longweightedSize()Returns the uncorrected combined weight of the values in the cache.protected MpscGrowableArrayQueue<java.lang.Runnable>writeBuffer()protected WriteOrderDeque<Node<K,V>>writeOrderDeque()-
Methods inherited from class com.github.benmanes.caffeine.cache.BLCHeader.DrainStatusRef
casDrainStatus, drainStatus, lazySetDrainStatus, shouldDrainBuffers
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.github.benmanes.caffeine.cache.LocalCache
compute, computeIfAbsent, invalidateAll, statsAware, statsAware, statsAware
-
-
-
-
Field Detail
-
logger
static final java.util.logging.Logger logger
-
NCPU
static final int NCPU
The number of CPUs
-
WRITE_BUFFER_MIN
static final int WRITE_BUFFER_MIN
The initial capacity of the write buffer.- See Also:
- Constant Field Values
-
WRITE_BUFFER_MAX
static final int WRITE_BUFFER_MAX
The maximum capacity of the write buffer.
-
WRITE_BUFFER_RETRIES
static final int WRITE_BUFFER_RETRIES
The number of attempts to insert into the write buffer before yielding.- See Also:
- Constant Field Values
-
MAXIMUM_CAPACITY
static final long MAXIMUM_CAPACITY
The maximum weighted capacity of the map.- See Also:
- Constant Field Values
-
PERCENT_MAIN
static final double PERCENT_MAIN
The percent of the maximum weighted capacity dedicated to the main space.- See Also:
- Constant Field Values
-
PERCENT_MAIN_PROTECTED
static final double PERCENT_MAIN_PROTECTED
The percent of the maximum weighted capacity dedicated to the main's protected space.- See Also:
- Constant Field Values
-
EXPIRE_WRITE_TOLERANCE
static final long EXPIRE_WRITE_TOLERANCE
The maximum time window between entry updates before the expiration must be reordered.
-
drainBuffersTask
final BoundedLocalCache.PerformCleanupTask drainBuffersTask
-
cacheLoader
final CacheLoader<K,V> cacheLoader
-
writer
final CacheWriter<K,V> writer
-
nodeFactory
final NodeFactory nodeFactory
-
evictionLock
final java.util.concurrent.locks.Lock evictionLock
-
executor
final java.util.concurrent.Executor executor
-
isAsync
final boolean isAsync
-
keySet
transient java.util.Set<K> keySet
-
values
transient java.util.Collection<V> values
-
-
Method Detail
-
ceilingPowerOfTwo
static int ceilingPowerOfTwo(int x)
-
isComputingAsync
final boolean isComputingAsync(Node<?,?> node)
Returns if the node's value is currently being computed, asynchronously.
-
accessOrderEdenDeque
protected AccessOrderDeque<Node<K,V>> accessOrderEdenDeque()
-
accessOrderProbationDeque
protected AccessOrderDeque<Node<K,V>> accessOrderProbationDeque()
-
accessOrderProtectedDeque
protected AccessOrderDeque<Node<K,V>> accessOrderProtectedDeque()
-
writeOrderDeque
protected WriteOrderDeque<Node<K,V>> writeOrderDeque()
-
buffersWrites
protected boolean buffersWrites()
If the page replacement policy buffers writes.
-
writeBuffer
protected MpscGrowableArrayQueue<java.lang.Runnable> writeBuffer()
-
executor
public final java.util.concurrent.Executor executor()
Description copied from interface:LocalCacheReturns theExecutorused by this cache.- Specified by:
executorin interfaceLocalCache<K,V>
-
hasWriter
protected boolean hasWriter()
Returns whether this cache notifies a writer when an entry is modified.
-
isRecordingStats
public boolean isRecordingStats()
Description copied from interface:LocalCacheReturns whether this cache has statistics enabled.- Specified by:
isRecordingStatsin interfaceLocalCache<K,V>
-
statsCounter
public StatsCounter statsCounter()
Description copied from interface:LocalCacheReturns theStatsCounterused by this cache.- Specified by:
statsCounterin interfaceLocalCache<K,V>
-
statsTicker
public Ticker statsTicker()
Description copied from interface:LocalCacheReturns theTickerused by this cache for statistics.- Specified by:
statsTickerin interfaceLocalCache<K,V>
-
removalListener
public RemovalListener<K,V> removalListener()
Description copied from interface:LocalCacheReturns theRemovalListenerused by this cache or null if not used.- Specified by:
removalListenerin interfaceLocalCache<K,V>
-
hasRemovalListener
public boolean hasRemovalListener()
Description copied from interface:LocalCacheReturns whether this cache notifies when an entry is removed.- Specified by:
hasRemovalListenerin interfaceLocalCache<K,V>
-
notifyRemoval
public void notifyRemoval(@Nullable K key, @Nullable V value, RemovalCause cause)Description copied from interface:LocalCacheAsynchronously sends a removal notification to the listener.- Specified by:
notifyRemovalin interfaceLocalCache<K,V>
-
collectKeys
protected boolean collectKeys()
Returns if the keys are weak reference garbage collected.
-
collectValues
protected boolean collectValues()
Returns if the values are weak or soft reference garbage collected.
-
keyReferenceQueue
@Nullable protected java.lang.ref.ReferenceQueue<K> keyReferenceQueue()
-
valueReferenceQueue
@Nullable protected java.lang.ref.ReferenceQueue<V> valueReferenceQueue()
-
expiresAfterAccess
protected boolean expiresAfterAccess()
Returns if the cache expires entries after an access time threshold.
-
expiresAfterAccessNanos
protected long expiresAfterAccessNanos()
How long after the last access to an entry the map will retain that entry.
-
setExpiresAfterAccessNanos
protected void setExpiresAfterAccessNanos(long expireAfterAccessNanos)
-
expiresAfterWrite
protected boolean expiresAfterWrite()
Returns if the cache expires entries after an write time threshold.
-
expiresAfterWriteNanos
protected long expiresAfterWriteNanos()
How long after the last write to an entry the map will retain that entry.
-
setExpiresAfterWriteNanos
protected void setExpiresAfterWriteNanos(long expireAfterWriteNanos)
-
refreshAfterWrite
protected boolean refreshAfterWrite()
Returns if the cache refreshes entries after an write time threshold.
-
refreshAfterWriteNanos
protected long refreshAfterWriteNanos()
How long after the last write an entry becomes a candidate for refresh.
-
setRefreshAfterWriteNanos
protected void setRefreshAfterWriteNanos(long refreshAfterWriteNanos)
-
hasWriteTime
public boolean hasWriteTime()
Description copied from interface:LocalCacheReturns whether the cache captures the write time of the entry.- Specified by:
hasWriteTimein interfaceLocalCache<K,V>
-
expirationTicker
public Ticker expirationTicker()
Description copied from interface:LocalCacheReturns theTickerused by this cache for expiration.- Specified by:
expirationTickerin interfaceLocalCache<K,V>
-
evicts
protected boolean evicts()
Returns if the cache evicts entries due to a maximum size or weight threshold.
-
isWeighted
protected boolean isWeighted()
Returns if entries may be assigned different weights.
-
frequencySketch
protected FrequencySketch<K> frequencySketch()
-
fastpath
protected boolean fastpath()
Returns if an access to an entry can skip notifying the eviction policy.
-
maximum
protected long maximum()
Returns the maximum weighted size.
-
edenMaximum
protected long edenMaximum()
Returns the maximum weighted size of the eden space.
-
mainProtectedMaximum
protected long mainProtectedMaximum()
Returns the maximum weighted size of the main's protected space.
-
lazySetMaximum
protected void lazySetMaximum(long maximum)
-
lazySetEdenMaximum
protected void lazySetEdenMaximum(long maximum)
-
lazySetMainProtectedMaximum
protected void lazySetMainProtectedMaximum(long maximum)
-
setMaximum
void setMaximum(long maximum)
Sets the maximum weighted size of the cache. The caller may need to perform a maintenance cycle to eagerly evicts entries until the cache shrinks to the appropriate size.
-
adjustedWeightedSize
long adjustedWeightedSize()
Returns the combined weight of the values in the cache.
-
weightedSize
protected long weightedSize()
Returns the uncorrected combined weight of the values in the cache.
-
edenWeightedSize
protected long edenWeightedSize()
Returns the uncorrected combined weight of the values in the eden space.
-
mainProtectedWeightedSize
protected long mainProtectedWeightedSize()
Returns the uncorrected combined weight of the values in the main's protected space.
-
lazySetWeightedSize
protected void lazySetWeightedSize(long weightedSize)
-
lazySetEdenWeightedSize
protected void lazySetEdenWeightedSize(long weightedSize)
-
lazySetMainProtectedWeightedSize
protected void lazySetMainProtectedWeightedSize(long weightedSize)
-
evictEntries
void evictEntries()
Evicts entries if the cache exceeds the maximum.
-
evictFromEden
int evictFromEden()
Evicts entries from the eden space into the main space while the eden size exceeds a maximum.- Returns:
- the number of candidate entries evicted from the eden space
-
evictFromMain
void evictFromMain(int candidates)
Evicts entries from the main space if the cache exceeds the maximum capacity. The main space determines whether admitting an entry (coming from the eden space) is preferable to retaining the eviction policy's victim. This is decision is made using a frequency filter so that the least frequently used entry is removed. The eden space candidates were previously placed in the MRU position and the eviction policy's victim is at the LRU position. The two ends of the queue are evaluated while an eviction is required. The number of remaining candidates is provided and decremented on eviction, so that when there are no more candidates the victim is evicted.- Parameters:
candidates- the number of candidate entries evicted from the eden space
-
admit
boolean admit(K candidateKey, K victimKey)
Determines if the candidate should be accepted into the main space, as determined by its frequency relative to the victim. A small amount of randomness is used to protect against hash collision attacks, where the victim's frequency is artificially raised so that no new entries are admitted.- Parameters:
candidateKey- the key for the entry being proposed for long term retentionvictimKey- the key for the entry chosen by the eviction policy for replacement- Returns:
- if the candidate should be admitted and the victim ejected
-
expireEntries
void expireEntries()
Expires entries that have expired in the access and write queues.
-
expireAfterAccessEntries
void expireAfterAccessEntries(long now)
Expires entries in the access-order queue.
-
expireAfterAccessEntries
void expireAfterAccessEntries(AccessOrderDeque<Node<K,V>> accessOrderDeque, long expirationTime, long now)
Expires entries in an access-order queue.
-
expireAfterWriteEntries
void expireAfterWriteEntries(long now)
Expires entries on the write-order queue.
-
evictEntry
void evictEntry(Node<K,V> node, RemovalCause cause, long now)
Attempts to evict the entry based on the given removal cause. A removal due to expiration or size may be ignored if the entry was updated and is no longer eligible for eviction.- Parameters:
node- the entry to evictcause- the reason to evictnow- the current time, used only if expiring
-
afterRead
void afterRead(Node<K,V> node, long now, boolean recordHit)
Performs the post-processing work required after a read.- Parameters:
node- the entry in the page replacement policynow- the current expiration time, in nanosecondsrecordHit- if the hit count should be incremented
-
skipReadBuffer
boolean skipReadBuffer()
Returns if the cache should bypass the read buffer.
-
refreshIfNeeded
void refreshIfNeeded(Node<K,V> node, long now)
Asynchronously refreshes the entry if eligible.- Parameters:
node- the entry in the cache to refreshnow- the current time, in nanoseconds
-
afterWrite
void afterWrite(@Nullable Node<K,V> node, java.lang.Runnable task, long now)Performs the post-processing work required after a write.- Parameters:
node- the node that was written totask- the pending operation to be appliednow- the current expiration time, in nanoseconds
-
scheduleAfterWrite
void scheduleAfterWrite()
Conditionally schedules the asynchronous maintenance task after a write operation. If the task status was IDLE or REQUIRED then the maintenance task is scheduled immediately. If it is already processing then it is set to transition to REQUIRED upon completion so that a new execution is triggered by the next operation.
-
scheduleDrainBuffers
void scheduleDrainBuffers()
Attempts to schedule an asynchronous task to apply the pending operations to the page replacement policy. If the executor rejects the task then it is run directly.
-
cleanUp
public void cleanUp()
Description copied from interface:LocalCacheSeeCache.cleanUp().- Specified by:
cleanUpin interfaceLocalCache<K,V>
-
performCleanUp
void performCleanUp(@Nullable java.lang.Runnable task)Performs the maintenance work, blocking until the lock is acquired. Any exception thrown, such as byCacheWriter#delete(), is propagated to the caller.- Parameters:
task- an additional pending task to run, ornullif not present
-
maintenance
void maintenance(@Nullable java.lang.Runnable task)Performs the pending maintenance work and sets the state flags during processing to avoid excess scheduling attempts. The read buffer, write buffer, and reference queues are drained, followed by expiration, and size-based eviction.- Parameters:
task- an additional pending task to run, ornullif not present
-
drainKeyReferences
void drainKeyReferences()
Drains the weak key references queue.
-
drainValueReferences
void drainValueReferences()
Drains the weak / soft value references queue.
-
drainReadBuffer
void drainReadBuffer()
Drains the read buffer.
-
onAccess
void onAccess(Node<K,V> node)
Updates the node's location in the page replacement policy.
-
reorderProbation
void reorderProbation(Node<K,V> node)
Promote the node from probation to protected on an access.
-
reorder
static <K,V> void reorder(LinkedDeque<Node<K,V>> deque, Node<K,V> node)
Updates the node's location in the policy's deque.
-
drainWriteBuffer
void drainWriteBuffer()
Drains the write buffer.
-
makeDead
void makeDead(Node<K,V> node)
Atomically transitions the node to the dead state and decrements the weightedSize.- Parameters:
node- the entry in the page replacement policy
-
isEmpty
public boolean isEmpty()
-
size
public int size()
-
estimatedSize
public long estimatedSize()
Description copied from interface:LocalCache- Specified by:
estimatedSizein interfaceLocalCache<K,V>
-
clear
public void clear()
-
removeNodes
void removeNodes(LinkedDeque<Node<K,V>> deque, long now)
-
containsKey
public boolean containsKey(java.lang.Object key)
-
containsValue
public boolean containsValue(java.lang.Object value)
-
get
public V get(java.lang.Object key)
-
getIfPresent
public V getIfPresent(java.lang.Object key, boolean recordStats)
Description copied from interface:LocalCacheSeeCache.getIfPresent(Object). This method differs by accepting a parameter of whether to record the hit and miss statistics based on the success of this operation.- Specified by:
getIfPresentin interfaceLocalCache<K,V>
-
getIfPresentQuietly
public V getIfPresentQuietly(java.lang.Object key, long[] writeTime)
Description copied from interface:LocalCacheSeeCache.getIfPresent(Object). This method differs by not recording the access with the statistics nor the eviction policy, and populates the write time if known.- Specified by:
getIfPresentQuietlyin interfaceLocalCache<K,V>
-
getAllPresent
public java.util.Map<K,V> getAllPresent(java.lang.Iterable<?> keys)
Description copied from interface:LocalCache- Specified by:
getAllPresentin interfaceLocalCache<K,V>
-
put
public V put(K key, V value, boolean notifyWriter)
Description copied from interface:LocalCacheSeeCache.put(Object, Object). This method differs by allowing the operation to not notify the writer when an entry was inserted or updated.- Specified by:
putin interfaceLocalCache<K,V>
-
putFast
V putFast(K key, V value, int newWeight, boolean notifyWriter, boolean onlyIfAbsent)
Adds a node to the policy and the data store. If an existing node is found, then its value is updated if allowed. This implementation is optimized for writing values with a non-zero weight. A zero weight is incompatible due to the potential for the update to race with eviction, where the entry should no longer be eligible if the update was successful. This implementation is ~50% faster thanputSlow(K, V, int, boolean, boolean)due to not incurring the penalty of a compute and lambda in the common case.- Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified keynotifyWriter- if the writer should be notified for an inserted or updated entryonlyIfAbsent- a write is performed only if the key is not already associated with a value- Returns:
- the prior value in or null if no mapping was found
-
putSlow
V putSlow(K key, V value, int newWeight, boolean notifyWriter, boolean onlyIfAbsent)
Adds a node to the policy and the data store. If an existing node is found, then its value is updated if allowed. This implementation is strict by using a compute to block other writers to that entry. This guards against an eviction trying to discard an entry concurrently (and successfully) updated to have a zero weight. The penalty is 50% of the throughput when compared toputFast(K, V, int, boolean, boolean).- Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified keynotifyWriter- if the writer should be notified for an inserted or updated entryonlyIfAbsent- a write is performed only if the key is not already associated with a value- Returns:
- the prior value or null if no mapping was found
-
remove
public V remove(java.lang.Object key)
-
removeNoWriter
V removeNoWriter(java.lang.Object key)
Removes the mapping for a key without notifying the writer.- Parameters:
key- key whose mapping is to be removed- Returns:
- the removed value or null if no mapping was found
-
removeWithWriter
V removeWithWriter(java.lang.Object key)
Removes the mapping for a key after notifying the writer.- Parameters:
key- key whose mapping is to be removed- Returns:
- the removed value or null if no mapping was found
-
remove
public boolean remove(java.lang.Object key, java.lang.Object value)
-
replaceAll
public void replaceAll(java.util.function.BiFunction<? super K,? super V,? extends V> function)
-
computeIfAbsent
public V computeIfAbsent(K key, java.util.function.Function<? super K,? extends V> mappingFunction, boolean recordStats, boolean recordLoad)
Description copied from interface:LocalCacheSeeConcurrentMap.computeIfAbsent(K, java.util.function.Function<? super K, ? extends V>). This method differs by accepting parameters indicating how to record statistics.- Specified by:
computeIfAbsentin interfaceLocalCache<K,V>
-
doComputeIfAbsent
V doComputeIfAbsent(K key, java.lang.Object keyRef, java.util.function.Function<? super K,? extends V> mappingFunction, long now)
Returns the current value from a computeIfAbsent invocation.
-
computeIfPresent
public V computeIfPresent(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
-
compute
public V compute(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction, boolean recordMiss, boolean recordLoad)
Description copied from interface:LocalCacheSeeConcurrentMap.compute(K, java.util.function.BiFunction<? super K, ? super V, ? extends V>). This method differs by accepting parameters indicating whether to record miss and load statistics based on the success of this operation.- Specified by:
computein interfaceLocalCache<K,V>
-
merge
public V merge(K key, V value, java.util.function.BiFunction<? super V,? super V,? extends V> remappingFunction)
-
remap
V remap(K key, java.lang.Object keyRef, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction, long now, boolean computeIfAbsent)
Attempts to compute a mapping for the specified key and its current mapped value (ornullif there is no current mapping).An entry that has expired or been reference collected is evicted and the computation continues as if the entry had not been present. This method does not pre-screen and does not wrap the remappingFuntion to be statistics aware.
- Parameters:
key- key with which the specified value is to be associatedkeyRef- the key to associate with or a lookup only key if not computeIfAbsentremappingFunction- the function to compute a valuenow- the current time, according to the tickercomputeIfAbsent- if an absent entry can be computed- Returns:
- the new value associated with the specified key, or null if none
-
keySet
public java.util.Set<K> keySet()
-
values
public java.util.Collection<V> values()
-
evictionOrder
java.util.Map<K,V> evictionOrder(int limit, java.util.function.Function<V,V> transformer, boolean hottest)
Returns an unmodifiable snapshot map ordered in eviction order, either ascending or descending. Beware that obtaining the mappings is NOT a constant-time operation.- Parameters:
limit- the maximum number of entriestransformer- a function that unwraps the valuehottest- the iteration order- Returns:
- an unmodifiable snapshot in a specified order
-
expireAfterAcessOrder
java.util.Map<K,V> expireAfterAcessOrder(int limit, java.util.function.Function<V,V> transformer, boolean oldest)
Returns an unmodifiable snapshot map ordered in access expiration order, either ascending or descending. Beware that obtaining the mappings is NOT a constant-time operation.- Parameters:
limit- the maximum number of entriestransformer- a function that unwraps the valueoldest- the iteration order- Returns:
- an unmodifiable snapshot in a specified order
-
expireAfterWriteOrder
java.util.Map<K,V> expireAfterWriteOrder(int limit, java.util.function.Function<V,V> transformer, boolean oldest)
Returns an unmodifiable snapshot map ordered in write expiration order, either ascending or descending. Beware that obtaining the mappings is NOT a constant-time operation.- Parameters:
limit- the maximum number of entriestransformer- a function that unwraps the valueoldest- the iteration order- Returns:
- an unmodifiable snapshot in a specified order
-
snapshot
java.util.Map<K,V> snapshot(java.util.function.Supplier<java.util.Iterator<Node<K,V>>> iteratorSupplier, java.util.function.Function<V,V> transformer, int limit)
Returns an unmodifiable snapshot map ordered by the provided iterator. Beware that obtaining the mappings is NOT a constant-time operation.- Parameters:
iteratorSupplier- the iteratorlimit- the maximum number of entriestransformer- a function that unwraps the value- Returns:
- an unmodifiable snapshot in the iterator's order
-
makeSerializationProxy
static <K,V> SerializationProxy<K,V> makeSerializationProxy(BoundedLocalCache<?,?> cache, boolean isWeighted)
Creates a serialization proxy based on the common configuration shared by all cache types.
-
-