Documentation
¶
Overview ¶
Package expiry orders keys by expiry time, so a bounded cache can find what to drop without scanning its whole map.
Index ¶
- Constants
- func EvictSoonest[K comparable, V any](index *Index[K], entries map[K]V, expiresOf func(V) time.Time) bool
- func Prune[K comparable, V any](index *Index[K], entries map[K]V, expiresOf func(V) time.Time, now time.Time)
- func Retrack[K comparable, V any](index *Index[K], entries map[K]V, expiresOf func(V) time.Time)
- type Index
Constants ¶
const MaxReconciliations = 64
MaxReconciliations bounds how much index repair a single Prune or EvictSoonest does. A key refreshed since it was tracked surfaces with a stale expiry and has to be re-tracked; a cache whose keys have all been refreshed would otherwise repair the entire heap while its lock is held. Past the budget the index is rebuilt once instead, which costs less than the pops it replaces and leaves it exact, so the sweep cannot need a second rebuild.
Variables ¶
This section is empty.
Functions ¶
func EvictSoonest ¶ added in v0.2.6
func EvictSoonest[K comparable, V any](index *Index[K], entries map[K]V, expiresOf func(V) time.Time) bool
EvictSoonest deletes the entry closest to expiry, to make room for a new one. It reports false when entries held nothing the index knew about.
func Prune ¶ added in v0.2.6
func Prune[K comparable, V any](index *Index[K], entries map[K]V, expiresOf func(V) time.Time, now time.Time)
Prune deletes the entries due at now, touching only the keys that are actually due rather than scanning entries. A key whose entry has moved past the recorded expiry is re-tracked; a key already deleted is discarded. Prune rebuilds the index when it has grown Stale.
Types ¶
type Index ¶
type Index[K comparable] struct { // contains filtered or unexported fields }
Index is a min-heap of keys ordered by expiry. It holds no values and takes no lock: the cache owns both, and tracks a key when it first inserts it.
Extending a key's TTL is deliberately not reported to the index. A cache that refreshes on every read would otherwise pay a heap fix on its hottest path, so the index is allowed to hold an expiry earlier than the truth. That is why Expired and Soonest hand back the expiry they recorded: the caller compares it with its own map, and re-Tracks a key that has moved. A key the cache has already deleted is simply discarded when it surfaces.
func (*Index[K]) Expired ¶
Expired pops the next key due at now, with the expiry the index holds for it. It reports false when the soonest key is not due yet.
func (*Index[K]) Len ¶
Len is how many keys are tracked, including ones the cache has deleted and the index has not surfaced yet.
func (*Index[K]) Soonest ¶
Soonest pops the key closest to expiry, with the expiry the index holds for it. It reports false when the index is empty.