Class TimerWheel<K,V>
java.lang.Object
com.github.benmanes.caffeine.cache.TimerWheel<K,V>
A hierarchical timer wheel to add, remove, and fire expiration events in amortized O(1) time. The
expiration events are deferred until the timer is advanced, which is performed as part of the
cache's maintenance cycle.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
A sentinel for the doubly-linked list in the bucket. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
advance
(long currentTimeNanos) Advances the timer and evicts entries that have expired.void
deschedule
(@NonNull Node<K, V> node) Removes a timer event for this entry if present.(package private) void
expire
(int index, long previousTicks, long currentTicks) Expires entries or reschedules into the proper bucket if still active.findBucket
(long time) Determines the bucket that the timer event should be added to.long
Returns the duration until the next bucket expires, orinvalid reference
Long.MAX_VALUE
(package private) void
Adds the entry at the tail of the bucket's list.(package private) long
peekAhead
(int i) Returns the duration when the wheel's next bucket expires, orinvalid reference
Long.MAX_VALUE
void
reschedule
(@NonNull Node<K, V> node) Reschedules an active timer event for the node.void
Schedules a timer event for the node.Returns an unmodifiable snapshot map roughly ordered by the expiration time.toString()
(package private) static <K,
V> Node <K, V> (package private) void
Removes the entry from its bucket, if scheduled.
-
Field Details
-
BUCKETS
static final int[] BUCKETS -
SPANS
static final long[] SPANS -
SHIFT
static final long[] SHIFT -
cache
-
wheel
-
nanos
long nanos
-
-
Constructor Details
-
TimerWheel
TimerWheel(BoundedLocalCache<K, V> cache)
-
-
Method Details
-
advance
public void advance(long currentTimeNanos) Advances the timer and evicts entries that have expired.- Parameters:
currentTimeNanos
- the current time, in nanoseconds
-
expire
void expire(int index, long previousTicks, long currentTicks) Expires entries or reschedules into the proper bucket if still active.- Parameters:
index
- the wheel being operated onpreviousTicks
- the previous number of tickscurrentTicks
- the current number of ticks
-
schedule
Schedules a timer event for the node.- Parameters:
node
- the entry in the cache
-
reschedule
Reschedules an active timer event for the node.- Parameters:
node
- the entry in the cache
-
deschedule
Removes a timer event for this entry if present.- Parameters:
node
- the entry in the cache
-
findBucket
Determines the bucket that the timer event should be added to.- Parameters:
time
- the time when the event fires- Returns:
- the sentinel at the head of the bucket
-
link
Adds the entry at the tail of the bucket's list. -
unlink
Removes the entry from its bucket, if scheduled. -
getExpirationDelay
public long getExpirationDelay()Returns the duration until the next bucket expires, orinvalid reference
Long.MAX_VALUE
-
peekAhead
long peekAhead(int i) Returns the duration when the wheel's next bucket expires, orinvalid reference
Long.MAX_VALUE
-
snapshot
Returns an unmodifiable snapshot map roughly ordered by the expiration time. The wheels are evaluated in order, but the timers that fall within the bucket's range are not sorted. Beware that obtaining the mappings is NOT a constant-time operation.- Parameters:
ascending
- the directionlimit
- the maximum number of entriestransformer
- a function that unwraps the value- Returns:
- an unmodifiable snapshot in the desired order
-
traverse
-
toString
-