Skip to content

Add cache_bust: option for automatic cache invalidation#50

Merged
eclectic-coding merged 2 commits into
mainfrom
feature/cache-bust
May 28, 2026
Merged

Add cache_bust: option for automatic cache invalidation#50
eclectic-coding merged 2 commits into
mainfrom
feature/cache-bust

Conversation

@eclectic-coding
Copy link
Copy Markdown
Owner

Summary

  • Adds cache_bust: callable option to memoize — 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 automatically
  • Accepts a zero-argument Proc/lambda (invoked via instance_exec, with full access to self and instance variables) or a Symbol naming an instance method
  • Returns any comparable value as the token: Time, Integer, String, Array, etc.
  • Incompatible with key:; composes with namespace:, ttl:, if:, unless:, and shared_cache:

Usage

# ActiveRecord — cache busts automatically on every save
memoize :summary, cache_bust: -> { @order.updated_at }

# Symbol form
memoize :data, cache_bust: :cache_version

# Compound token
memoize :report, cache_bust: -> { [version, tenant_id] }

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 via ttl: or are cleared by reset_memo. No new cache path code — reuses the existing custom-key infrastructure entirely.

Test plan

  • bundle exec rake — 763 examples, 0 failures, lint clean
  • spec/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

eclectic-coding and others added 2 commits May 28, 2026 16:22
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>
@eclectic-coding eclectic-coding merged commit 254cf83 into main May 28, 2026
4 checks passed
@eclectic-coding eclectic-coding deleted the feature/cache-bust branch May 28, 2026 20:26
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