Skip to content

Add DeleteFunc and on-demand PurgeExpired - #11

Merged
floatdrop merged 4 commits into
mainfrom
feat/purge-expired
Nov 25, 2026
Merged

floatdrop merged 4 commits into
mainfrom
feat/purge-expired

Conversation

@floatdrop

@floatdrop floatdrop commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Why

expire.Cache.All skips expired entries. Scanning the backing cache and then deleting by key can remove a value another goroutine has just refreshed. Callers need a way to reclaim expired entries on their own schedule without that race.

Changes

  • Add DeleteFunc(del func(K, V) bool) int to evict.Cache. All five built-in policies test and remove each entry under the same shard lock, one shard at a time. The predicate must not call back into the cache. Breaking: implementations of evict.Cache outside this module must add the method, as with SetIfAbsent in v0.3.0.
  • Add expire.Cache.PurgeExpired(removed func(K, V)) int. It reads the monotonic clock once, removes entries due at that cutoff, and returns the number removed. Values refreshed past the cutoff survive; entries that come due later are left for the next pass. Unless removed is nil, it gets each removed entry under the shard lock, so that what the value holds on to can be released. There is no background sweep.
  • Forward DeleteFunc through stats without counting evictions, and through expire for live entries only.
  • Docs: a purged key comes back in as a new entry, as after Delete; purging makes SetIfAbsent's documented race more likely; stats has no PurgeExpired. The README lists "No background expiry" under what the module does not do.

Verification

  • make test (race detector, root and bench modules)
  • make lint
  • Regression coverage includes policy bookkeeping against each policy's model, concurrent access, refresh during a purge, exact deadlines, and the purge callback (each removed entry once, panics).

floatdrop and others added 4 commits November 24, 2026 20:51
An optional method is lost without a word by any wrapper that embeds
evict.Cache, and PurgeExpired then removes nothing. A method of the
interface is checked by the compiler, and it needs no error: every
cache in the module supports it. This breaks implementations outside
the module, as SetIfAbsent did in v0.3.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Entry's deadline is unexported, so a caller could not sweep the
wrapped cache and release the values of expired entries without
racing a refresh. PurgeExpired now calls a function with each entry
it removes; nil only removes them.

The docs also say that a purged key starts over with the policy, that
purging makes SetIfAbsent's documented race likelier, and that stats
has no PurgeExpired. The README lists the lack of background expiry
again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@floatdrop
floatdrop merged commit 298dda3 into main Nov 25, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant