Add cache_bust: option for automatic cache invalidation#50
Merged
Conversation
cache_bust: accepts a callable (Proc/lambda) or Symbol naming an
instance method. On every cache lookup the callable is invoked via
instance_exec (or send for Symbol) to obtain a version token. The
token is folded into the cache key alongside the normal arguments as
a 2-element custom key [method, [frozen_args, frozen_kwargs, token]],
so when the token changes the old key no longer matches any entry —
the method is recomputed and stored under the new key.
Typical usage with ActiveRecord:
memoize :summary, cache_bust: -> { updated_at }
Other forms:
memoize :data, cache_bust: :cache_version # symbol → send
memoize :report, cache_bust: -> { [v1, v2] } # compound token
Composes with namespace:, ttl:, if:, shared_cache:.
Incompatible with key: (raises ArgumentError).
Old token entries accumulate as stale; pair with ttl: or max_size:
(on the store adapter) to bound memory usage.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <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.
Summary
cache_bust: callableoption tomemoize— a version token derived from instance state is folded into the cache key; when the token changes the old key produces no match and the method recomputes automaticallyinstance_exec, with full access toselfand instance variables) or a Symbol naming an instance methodTime,Integer,String,Array, etc.key:; composes withnamespace:,ttl:,if:,unless:, andshared_cache:Usage
How it works
The token is incorporated as a 2-element custom key
[method, [frozen_args, frozen_kwargs, token]]. Old token entries accumulate as stale until they expire viattl:or are cleared byreset_memo. No new cache path code — reuses the existing custom-key infrastructure entirely.Test plan
bundle exec rake— 763 examples, 0 failures, lint cleanspec/cache_bust_spec.rb— 18 new examples covering basic invalidation, method arguments, Symbol form, compound tokens, introspection transparency, TTL/namespace/shared_cache composition, and validation🤖 Generated with Claude Code