Repository navigation
Add DeleteFunc and on-demand PurgeExpired - #11
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
expire.Cache.Allskips 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
DeleteFunc(del func(K, V) bool) inttoevict.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 ofevict.Cacheoutside this module must add the method, as withSetIfAbsentin v0.3.0.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. Unlessremovedis 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.DeleteFuncthroughstatswithout counting evictions, and throughexpirefor live entries only.Delete; purging makesSetIfAbsent's documented race more likely;statshas noPurgeExpired. The README lists "No background expiry" under what the module does not do.Verification
make test(race detector, root and bench modules)make lint