$npx -y skills add ThibautBaissac/rails_ai_agents --skill caching-strategiesImplements 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 perform
| 1 | # Caching Strategies for Rails 8 |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Rails provides multiple caching layers: |
| 6 | - **Fragment caching**: Cache view partials |
| 7 | - **Russian doll caching**: Nested cache fragments |
| 8 | - **Low-level caching**: Cache arbitrary data |
| 9 | - **HTTP caching**: Browser and CDN caching |
| 10 | - **Query caching**: Automatic within requests |
| 11 | |
| 12 | ## Quick Start |
| 13 | |
| 14 | ```ruby |
| 15 | # config/environments/development.rb |
| 16 | config.action_controller.perform_caching = true |
| 17 | config.cache_store = :memory_store |
| 18 | |
| 19 | # config/environments/production.rb |
| 20 | config.cache_store = :solid_cache_store # Rails 8 default |
| 21 | # OR |
| 22 | config.cache_store = :redis_cache_store, { url: ENV["REDIS_URL"] } |
| 23 | ``` |
| 24 | |
| 25 | Enable caching in development: |
| 26 | ```bash |
| 27 | bin/rails dev:cache |
| 28 | ``` |
| 29 | |
| 30 | ## Cache Store Options |
| 31 | |
| 32 | | Store | Use Case | Pros | Cons | |
| 33 | |-------|----------|------|------| |
| 34 | | `:memory_store` | Development | Fast, no setup | Not shared, limited size | |
| 35 | | `:solid_cache_store` | Production (Rails 8) | Database-backed, no Redis | Slightly slower | |
| 36 | | `:redis_cache_store` | Production | Fast, shared | Requires Redis | |
| 37 | | `:file_store` | Simple production | Persistent, no Redis | Slow, not shared | |
| 38 | | `:null_store` | Testing | No caching | N/A | |
| 39 | |
| 40 | ## Fragment Caching |
| 41 | |
| 42 | ### Basic Fragment Cache |
| 43 | |
| 44 | ```erb |
| 45 | <%# app/views/events/_event.html.erb %> |
| 46 | <% cache event do %> |
| 47 | <article class="event-card"> |
| 48 | <h3><%= event.name %></h3> |
| 49 | <p><%= event.description %></p> |
| 50 | <time><%= l(event.event_date, format: :long) %></time> |
| 51 | <%= render event.venue %> |
| 52 | </article> |
| 53 | <% end %> |
| 54 | ``` |
| 55 | |
| 56 | ### Cache Key Components |
| 57 | |
| 58 | Rails generates cache keys from: |
| 59 | - Model name |
| 60 | - Model ID |
| 61 | - `updated_at` timestamp |
| 62 | - Template digest (automatic) |
| 63 | |
| 64 | ```ruby |
| 65 | # Generated key example: |
| 66 | # views/events/123-20240115120000000000/abc123digest |
| 67 | ``` |
| 68 | |
| 69 | ### Custom Cache Keys |
| 70 | |
| 71 | ```erb |
| 72 | <%# With version %> |
| 73 | <% cache [event, "v2"] do %> |
| 74 | ... |
| 75 | <% end %> |
| 76 | |
| 77 | <%# With user-specific content %> |
| 78 | <% cache [event, current_user] do %> |
| 79 | ... |
| 80 | <% end %> |
| 81 | |
| 82 | <%# With explicit key %> |
| 83 | <% cache "featured-events-#{Date.current}" do %> |
| 84 | <%= render @featured_events %> |
| 85 | <% end %> |
| 86 | ``` |
| 87 | |
| 88 | ## Russian Doll Caching |
| 89 | |
| 90 | Nested caches where inner caches are reused when outer cache is invalidated: |
| 91 | |
| 92 | ```erb |
| 93 | <%# app/views/events/show.html.erb %> |
| 94 | <% cache @event do %> |
| 95 | <h1><%= @event.name %></h1> |
| 96 | |
| 97 | <section class="vendors"> |
| 98 | <% @event.vendors.each do |vendor| %> |
| 99 | <% cache vendor do %> |
| 100 | <%= render partial: "vendors/card", locals: { vendor: vendor } %> |
| 101 | <% end %> |
| 102 | <% end %> |
| 103 | </section> |
| 104 | |
| 105 | <section class="comments"> |
| 106 | <% @event.comments.each do |comment| %> |
| 107 | <% cache comment do %> |
| 108 | <%= render comment %> |
| 109 | <% end %> |
| 110 | <% end %> |
| 111 | </section> |
| 112 | <% end %> |
| 113 | ``` |
| 114 | |
| 115 | Use `touch: true` on `belongs_to` associations to cascade invalidation up the chain. See [cache-invalidation.md](references/cache-invalidation.md) for examples. |
| 116 | |
| 117 | ## Collection Caching |
| 118 | |
| 119 | ### Efficient Collection Rendering |
| 120 | |
| 121 | ```erb |
| 122 | <%# Caches each item individually %> |
| 123 | <%= render partial: "events/event", collection: @events, cached: true %> |
| 124 | |
| 125 | <%# With custom cache key %> |
| 126 | <%= render partial: "events/event", |
| 127 | collection: @events, |
| 128 | cached: ->(event) { [event, current_user.admin?] } %> |
| 129 | ``` |
| 130 | |
| 131 | ## Low-Level Caching |
| 132 | |
| 133 | Use `Rails.cache.fetch` with a block for the most common pattern. See [low-level-caching.md](references/low-level-caching.md) for: |
| 134 | - Basic read/write/fetch examples |
| 135 | - Caching in service objects |
| 136 | - Caching in query objects |
| 137 | - Instance variable memoization |
| 138 | - Request-scoped memoization with `CurrentAttributes` |
| 139 | |
| 140 | ## Cache Invalidation |
| 141 | |
| 142 | Three strategies: time-based expiration, key-based expiration (using `updated_at`), and manual deletion. See [cache-invalidation.md](references/cache-invalidation.md) for: |
| 143 | - Time-based and key-based expiration |
| 144 | - Manual invalidation in model callbacks and services |
| 145 | - Pattern-based deletion (`delete_matched`) |
| 146 | - `touch: true` for Russian doll cascade |
| 147 | - Built-in and custom counter caches |
| 148 | |
| 149 | ## HTTP Caching |
| 150 | |
| 151 | Use `stale?` for conditional GET (ETags/Last-Modified) and `expires_in` for Cache-Control headers. See [http-caching-and-testing.md](references/http-caching-and-testing.md) for full examples. |
| 152 | |
| 153 | ## Testing Caching |
| 154 | |
| 155 | Use a `:caching` metadata tag to enable caching in specs. See [http-caching-and-testing.md](references/http-caching-and-testing.md) for: |
| 156 | - `rails_helper.rb` configuration |
| 157 | - Testing cached view invalidation |
| 158 | - Testing cache invalidation in services |
| 159 | - Performance monitoring and instrumentation |
| 160 | |
| 161 | ## Checklist |
| 162 | |
| 163 | - [ ] Cache store configured for environment |
| 164 | - [ ] Fragment caching on expe |