expiry

package
v0.1.95 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: MIT Imports: 1 Imported by: 0

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

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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