caching-strategies
Skill665 starsChanged 6 months agoMITView on GitHub
Implements Rails caching patterns for performance optimization. Use when adding fragment caching, Russian doll caching, low-level caching, cache invalidation, or when user mentions caching, performance, cache keys, or memoization. WHEN NOT: General query optimization (use performance-optimization), background job processing, or problems caused by N+1 queries rather than missing caches.
11 other projects use a version of this.
Adopted by
Most recently taken up by
- sf-scalar/skiLL.Mdtheir own version, first seen Nov 8, 2026
- reddygopalkrishna/developer-agent-skillstheir own version, first seen Nov 8, 2026
- cookieMonsterDev/agents-skillstheir own version, first seen Nov 1, 2026
- novatic-cu/nova-skillstheir own version, first seen Sep 20, 2026
- 2867203296/hermes-agenttheir own version, first seen Aug 19, 2026
- Bappoz/My-Claude-Skillstheir own version, first seen Jul 15, 2026
Used with
Projects with it also use observability and api-design.
Other files people use alongside this one.
Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
OpenAISkillobservability
Vercel Observability expert guidance — Drains (logs, traces, speed insights, web analytics), Web Analytics, Speed Insights, runtime logs, custom events, OpenTelemetry integration, and monitoring dashboards. Use when instrumenting, debugging, or optimizing application performance and user experience on Vercel.
Also in 3 projects that use this
MicrosoftSkillapi-design
Guide for designing and documenting RESTful APIs. Use when asked to design an API, create endpoints, or document an API.
Also in 3 projects that use this
Other versions11 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 11
- reddygopalkrishna/developer-agent-skills★ 8same name
- cookieMonsterDev/agents-skills★ 7same name
- sf-scalar/skiLL.Md★ 6same name
- ffsshhttiikk/opencode-agents-skills★ 2same name
- kimtth/agent-skill-100-lines-or-less★ 2same name
- Bappoz/My-Claude-Skills★ 1same name
- harrissohail6/claude-code-config★ 1same name
- michaelsvanbeek/personal-agent-skills★ 1same name
- novatic-cu/nova-skills★ 1same name
- 2867203296/hermes-agent★ 0same name
1 more not listed
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository43 other files it gives its agents.
- AGENTS.mdAGENTS.md
- .claude_37signals/CLAUDE.mdCLAUDE.md
- CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- accessibility-reviewSkill
- action-cable-patternsSkill
- active-storage-setupSkill
- api-versioningSkill
- authentication-flowSkill
- behavioral-guidelinesSkill
- code-reviewSkill
- codex-reviewSkill
- dependabot-reviewSkill
- extraction-timingSkill
- friction-reviewSkill
- i18n-patternsSkill
- mutation-testingSkill
- performance-optimizationSkill
- postgres-patternsSkill
- rails-architectureSkill
- rails-concernSkill
- security-auditSkill
- solid-queue-setupSkill
- accessibility-reviewSkill
- action-cable-patternsSkill
- active-storage-setupSkill
- api-versioningSkill
- authentication-flowSkill
- behavioral-guidelinesSkill
- caching-strategiesSkill
- code-reviewSkill
- codex-reviewSkill
- dependabot-reviewSkill
- extraction-timingSkill
- friction-reviewSkill
- i18n-patternsSkill
- mutation-testingSkill
- performance-optimizationSkill
- postgres-patternsSkill
- rails-architectureSkill
- rails-concernSkill
- security-auditSkill
- solid-queue-setupSkill
The file
We split this skill into the rules it gives an agent
4 rules. 0 are common, 0 are distinctive, and 4 are very specific to this project.
0
Common
Most of the 9 edited versions we read keep these.
0
Distinctive
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 127
Use touch: true on belongs_to associations to cascade invalidation up the chain.
- Very specific: line 145
Use Rails.cache.fetch with a block for the most common pattern.
- Very specific: line 163
Use stale? for conditional GET (ETags/Last-Modified) and expires_in for Cache-Control headers.
- Very specific: line 167
Use a :caching metadata tag to enable caching in specs.
“Common” is measured: kept by at least 6 in 10 of the 9 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Caching Strategies for Rails 8
Overview
Rails provides multiple caching layers:
- Fragment caching: Cache view partials
- Russian doll caching: Nested cache fragments
- Low-level caching: Cache arbitrary data
- HTTP caching: Browser and CDN caching
- Query caching: Automatic within requests
Quick Start
# config/environments/development.rb
config.action_controller.perform_caching = true
config.cache_store = :memory_store
# config/environments/production.rb
config.cache_store = :solid_cache_store # Rails 8 default
# OR
config.cache_store = :redis_cache_store, { url: ENV["REDIS_URL"] }Enable caching in development:
bin/rails dev:cacheCache Store Options
| Store | Use Case | Pros | Cons |
:memory_store | Development | Fast, no setup | Not shared, limited size |
:solid_cache_store | Production (Rails 8) | Database-backed, no Redis | Slightly slower |
:redis_cache_store | Production | Fast, shared | Requires Redis |
:file_store | Simple production | Persistent, no Redis | Slow, not shared |
:null_store | Testing | No caching | N/A |
Fragment Caching
Basic Fragment Cache
<%# app/views/events/_event.html.erb %>
<% cache event do %>
<article class="event-card">
<h3><%= event.name %></h3>
<p><%= event.description %></p>
<time><%= l(event.event_date, format: :long) %></time>
<%= render event.venue %>
</article>
<% end %>Cache Key Components
Rails generates cache keys from:
- Model name
- Model ID
updated_attimestamp- Template digest (automatic)
# Generated key example:
# views/events/123-20240115120000000000/abc123digestCustom Cache Keys
<%# With version %>
<% cache [event, "v2"] do %>
...
<% end %>
<%# With user-specific content %>
<% cache [event, current_user] do %>
...
<% end %>
<%# With explicit key %>
<% cache "featured-events-#{Date.current}" do %>
<%= render @featured_events %>
<% end %>Russian Doll Caching
Nested caches where inner caches are reused when outer cache is invalidated:
<%# app/views/events/show.html.erb %>
<% cache @event do %>
<h1><%= @event.name %></h1>
<section class="vendors">
<% @event.vendors.each do |vendor| %>
<% cache vendor do %>
<%= render partial: "vendors/card", locals: { vendor: vendor } %>
<% end %>
<% end %>
</section>
<section class="comments">
<% @event.comments.each do |comment| %>
<% cache comment do %>
<%= render comment %>
<% end %>
<% end %>
</section>
<% end %>Use touch: true on belongs_to associations to cascade invalidation up the chain. See cache-invalidation.md for examples.
Collection Caching
Efficient Collection Rendering
<%# Caches each item individually %>
<%= render partial: "events/event", collection: @events, cached: true %>
<%# With custom cache key %>
<%= render partial: "events/event",
collection: @events,
cached: ->(event) { [event, current_user.admin?] } %>Low-Level Caching
Use Rails.cache.fetch with a block for the most common pattern. See low-level-caching.md for:
- Basic read/write/fetch examples
- Caching in service objects
- Caching in query objects
- Instance variable memoization
- Request-scoped memoization with
CurrentAttributes
Cache Invalidation
Three strategies: time-based expiration, key-based expiration (using updated_at), and manual deletion. See cache-invalidation.md for:
- Time-based and key-based expiration
- Manual invalidation in model callbacks and services
- Pattern-based deletion (
delete_matched) touch: truefor Russian doll cascade- Built-in and custom counter caches
HTTP Caching
Use stale? for conditional GET (ETags/Last-Modified) and expires_in for Cache-Control headers. See http-caching-and-testing.md for full examples.
Testing Caching
Use a :caching metadata tag to enable caching in specs. See http-caching-and-testing.md for:
rails_helper.rbconfiguration- Testing cached view invalidation
- Testing cache invalidation in services
- Performance monitoring and instrumentation
Checklist
- Cache store configured for environment
- Fragment caching on expensive partials
touch: trueon belongs_to for Russian doll- Collection caching with
cached: true - Low-level caching for expensive queries
- Cache invalidation strategy defined
- Counter caches for counts
- HTTP caching headers for API
- Cache warming for cold starts (if needed)
- Monitoring for hit/miss rates
References
- low-level-caching.md - fetch/read/write, service objects, query objects, memoization
- cache-invalidation.md - expiration strategies, manual invalidation, touch, counter caches
- http-caching-and-testing.md - ETags, Cache-Control, spec configuration, monitoring
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

