Add memoize: true option for per-instance caching#22
Add memoize: true option for per-instance caching#22agrberg wants to merge 1 commit intoar/hangin_with_claudefrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds an opt-in memoize: true mode to cacheable to avoid repeated cache-adapter fetch/deserialization by storing the deserialized result on the receiver (instance or class object) for the lifetime of that receiver.
Changes:
- Add per-receiver memoization to generated cacheable methods, keyed by computed cache key.
- Add a
Cacheable::MEMOIZE_NOT_SETsentinel to correctly memoizenil/false. - Add RSpec coverage, README documentation, and an example script demonstrating reduced adapter hits.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/cacheable/method_generator.rb | Implements memoization fast-path and memo store writes; hooks memo clearing into clear_*_cache. |
| lib/cacheable.rb | Introduces sentinel constant used by memoization to distinguish “unset” from nil/false. |
| spec/cacheable/cacheable_spec.rb | Adds tests covering memoization behavior (adapter hit reduction, nil/false, unless, clearing, class methods). |
| README.md | Documents memoize: true, usage, and caveats around object lifetime / staleness. |
| examples/memoize_example.rb | Demonstrates adapter-hit difference with and without memoization. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
643e670 to
92b72df
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
When `memoize: true` is passed to `cacheable`, repeated calls on the same instance skip the cache adapter entirely and return the previously deserialized result. This avoids expensive repeated deserialization for objects like ActiveRecord models. Memoized values are cleared by `clear_*_cache` and garbage collected with the instance. Closes #15 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
92b72df to
c82a767
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
This branch should address and close our last open issue. To be merged after #21 |
Summary
memoize: trueoption tocacheablethat stores deserialized values directly on the instance, skipping the cache adapter on repeated callsclear_*_cachememoizeis not used — the feature is fully opt-inCloses #15
Context
When using a serializing cache backend (Redis, Memcached), every call to a cached method pays the deserialization cost via
Marshal.load. For expensive objects like ActiveRecord models, this becomes a bottleneck when the same method is called repeatedly within a single request. Withmemoize: true, the first call fetches from the adapter as usual, but subsequent calls on the same instance return the already-deserialized object directly.Test plan
bundle exec rspec)bundle exec rubocop)bundle exec ruby examples/memoize_example.rb)🤖 Generated with Claude Code