-
Notifications
You must be signed in to change notification settings - Fork 2
Cacheable feature improvements and documentation updates #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
agrberg
wants to merge
10
commits into
main
Choose a base branch
from
ar/hangin_with_claude
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+381
−141
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2f88816
Forward keyword arguments and blocks through cached methods
agrberg 9ec5616
Warn when default cache key format is used with arguments
agrberg 8c488bf
Make MemoryAdapter thread-safe with Monitor
agrberg f7f6cba
Support per-class cache adapter configuration
agrberg 41e8118
Stop polluting Cacheable namespace with interceptor constants
agrberg 8dc4fb0
Ignore dev specific platforms
agrberg 44949f5
Add CLAUDE.md for Claude Code guidance
agrberg 0464167
Update star counts in examples and README
agrberg 79e3971
Bump version to 2.1.0
agrberg 4eb4a46
Disable RuboCop extension suggestions
agrberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # CLAUDE.md | ||
|
|
||
| This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. | ||
|
|
||
| ## Project | ||
|
|
||
| Cacheable is a Ruby gem by Splitwise that adds method caching via an AOP (Aspect-Oriented Programming) pattern. Include `Cacheable` in a class, annotate methods with `cacheable :method_name`, and results are automatically cached. | ||
|
|
||
| ## Commands | ||
|
|
||
| ```bash | ||
| # Install dependencies | ||
| bundle install | ||
|
|
||
| # Run full default task (rubocop + rspec) | ||
| bundle exec rake | ||
|
|
||
| # Run tests only | ||
| bundle exec rspec | ||
|
|
||
| # Run a single test file | ||
| bundle exec rspec spec/cacheable/cacheable_spec.rb | ||
|
|
||
| # Run a single test by line number | ||
| bundle exec rspec spec/cacheable/cacheable_spec.rb:45 | ||
|
|
||
| # Run linter only | ||
| bundle exec rubocop | ||
|
|
||
| # Auto-fix lint issues | ||
| bundle exec rubocop -a | ||
|
|
||
| # Watch and auto-run tests/lint on file changes | ||
| bundle exec guard | ||
| ``` | ||
|
|
||
| ## Architecture | ||
|
|
||
| The gem uses **module prepending with dynamic method generation** to intercept and cache method calls. | ||
|
|
||
| ### Core flow | ||
|
|
||
| 1. **`Cacheable`** (`lib/cacheable.rb`) — The module users include. On `included`, it extends the host class with `MethodGenerator` and creates a unique anonymous interceptor module that gets prepended to the class. | ||
|
|
||
| 2. **`MethodGenerator`** (`lib/cacheable/method_generator.rb`) — When `cacheable :method_name` is called, this generates five methods on the interceptor module: | ||
| - `method_name` (override) — dispatcher that routes to `with_cache` or `without_cache` based on the `unless:` condition | ||
| - `method_with_cache` — fetch from cache or compute and store | ||
| - `method_without_cache` — bypass cache, call original | ||
| - `method_key_format` — generate the cache key | ||
| - `clear_method_cache` — invalidate the cache entry | ||
|
|
||
| 3. **`CacheAdapter`** (`lib/cacheable/cache_adapter.rb`) — Protocol for cache backends. Default is `:memory`. Required interface: `fetch(key, options, &block)` and `delete(key)`. | ||
|
|
||
| 4. **`MemoryAdapter`** (`lib/cacheable/cache_adapters/memory_adapter.rb`) — Built-in hash-backed in-memory cache. Production use typically wires in `Rails.cache` or a custom adapter. | ||
|
|
||
| ### Key design details | ||
|
|
||
| - Each class that includes `Cacheable` gets its own unique interceptor module (created via `Module.new`), which is prepended to the class. This is how `super` chains through to the original method. | ||
| - The `unless:` option accepts a proc/symbol that, when truthy, skips caching and calls the original method directly. | ||
| - `key_format:` accepts a proc receiving `(target, method_name, args, **kwargs)` for custom cache key generation. | ||
|
|
||
| ## Style | ||
|
|
||
| - Max line length: 120 characters | ||
| - Max method length: 25 lines | ||
| - Rubocop enforced with `NewCops: enable` | ||
| - No frozen string literal comments required |
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.