expiry

package
v0.2.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 1 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

func Retrack added in v0.2.6

func Retrack[K comparable, V any](index *Index[K], entries map[K]V, expiresOf func(V) time.Time)

Retrack rebuilds the index from entries, dropping the keys the cache deleted without the index ever seeing them.

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

func (i *Index[K]) Expired(now time.Time) (K, time.Time, bool)

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

func (i *Index[K]) Len() int

Len is how many keys are tracked, including ones the cache has deleted and the index has not surfaced yet.

func (*Index[K]) Reset

func (i *Index[K]) Reset()

Reset empties the index.

func (*Index[K]) Soonest

func (i *Index[K]) Soonest() (K, time.Time, bool)

Soonest pops the key closest to expiry, with the expiry the index holds for it. It reports false when the index is empty.

func (*Index[K]) Stale

func (i *Index[K]) Stale(live int) bool

Stale reports whether the index holds far more keys than the live count, which is what happens when a cache deletes keys it never pops. The caller answers by resetting and re-tracking what it still holds.

func (*Index[K]) Track

func (i *Index[K]) Track(key K, expires time.Time)

Track records that key expires at expires. Call it when a key enters the cache, and again when a popped key turns out to have been refreshed since.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL