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 classA sentinel for the doubly-linked list in the bucket. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadvance(long currentTimeNanos) Advances the timer and evicts entries that have expired.voiddeschedule(@NonNull Node<K, V> node) Removes a timer event for this entry if present.(package private) voidexpire(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.longReturns the duration until the next bucket expires, orif none.invalid reference
Long.MAX_VALUE(package private) voidAdds the entry at the tail of the bucket's list.(package private) longpeekAhead(int i) Returns the duration when the wheel's next bucket expires, orif empty.invalid reference
Long.MAX_VALUEvoidreschedule(@NonNull Node<K, V> node) Reschedules an active timer event for the node.voidSchedules 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) voidRemoves 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
-
reschedule
-
deschedule
-
findBucket
-
link
-
unlink
-
getExpirationDelay
public long getExpirationDelay()Returns the duration until the next bucket expires, orif none.invalid reference
Long.MAX_VALUE -
peekAhead
long peekAhead(int i) Returns the duration when the wheel's next bucket expires, orif empty.invalid 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
-