$npx -y skills add ThibautBaissac/rails_ai_agents --skill caching-patternsImplements HTTP caching with ETags, fragment caching, Russian doll caching, and Solid Cache configuration. Use when optimizing performance, adding caching layers, or when user mentions ETags, fresh_when, stale?, cache keys, or Russian doll caching. WHEN NOT: For Turbo Stream real
| 1 | # Caching Patterns |
| 2 | |
| 3 | ## Philosophy: Cache Aggressively, Invalidate Precisely |
| 4 | |
| 5 | - HTTP caching with ETags and `fresh_when` for free 304 Not Modified responses |
| 6 | - Russian doll caching with `touch: true` for automatic cache invalidation |
| 7 | - Fragment caching in views with cache keys based on `updated_at` timestamps |
| 8 | - Solid Cache (database-backed, no Redis) for production caching |
| 9 | - Collection caching with `cache_collection` for lists |
| 10 | - Low-level caching with `Rails.cache.fetch` for expensive computations |
| 11 | |
| 12 | ## Project Knowledge |
| 13 | |
| 14 | **Stack:** Solid Cache (database-backed), Turbo for page refreshes, ETags with |
| 15 | conditional GET, fragment caching in ERB views, collection caching for lists. |
| 16 | |
| 17 | **Multi-tenancy:** Cache keys scoped to account. URL-based: |
| 18 | `app.myapp.com/123/projects/456`. |
| 19 | |
| 20 | **Commands:** |
| 21 | ```bash |
| 22 | rails solid_cache:install # Install Solid Cache |
| 23 | rails db:migrate # Run cache migrations |
| 24 | rails cache:clear # Clear cache |
| 25 | ``` |
| 26 | |
| 27 | ## Caching Strategy Hierarchy |
| 28 | |
| 29 | Apply caching in this order (highest impact first): |
| 30 | |
| 31 | 1. **HTTP caching** -- `fresh_when` / `stale?` in controllers (free 304s) |
| 32 | 2. **Fragment caching** -- `cache` blocks in views (Russian doll) |
| 33 | 3. **Collection caching** -- `cache_collection` for lists of partials |
| 34 | 4. **Low-level caching** -- `Rails.cache.fetch` for expensive computations |
| 35 | |
| 36 | ## Pattern 1: HTTP Caching with ETags |
| 37 | |
| 38 | See @references/http-caching.md for full details. |
| 39 | |
| 40 | ```ruby |
| 41 | # Single resource -- returns 304 if ETag matches |
| 42 | class BoardsController < ApplicationController |
| 43 | def show |
| 44 | @board = Current.account.boards.find(params[:id]) |
| 45 | fresh_when @board |
| 46 | end |
| 47 | |
| 48 | def index |
| 49 | @boards = Current.account.boards.includes(:creator) |
| 50 | fresh_when @boards |
| 51 | end |
| 52 | end |
| 53 | |
| 54 | # Composite ETag from multiple objects |
| 55 | def show |
| 56 | fresh_when [@board, @card, Current.user] |
| 57 | end |
| 58 | |
| 59 | # API with stale? for conditional rendering |
| 60 | def show |
| 61 | @board = Current.account.boards.find(params[:id]) |
| 62 | if stale?(@board) |
| 63 | render json: @board |
| 64 | end |
| 65 | end |
| 66 | |
| 67 | # Custom ETag with parameters |
| 68 | fresh_when etag: [@activities, @report_date, Current.user.timezone] |
| 69 | ``` |
| 70 | |
| 71 | ## Pattern 2: Russian Doll Caching |
| 72 | |
| 73 | See @references/fragment-caching.md for full details. |
| 74 | |
| 75 | Set up touch cascades in models: |
| 76 | |
| 77 | ```ruby |
| 78 | class Card < ApplicationRecord |
| 79 | belongs_to :board, touch: true |
| 80 | end |
| 81 | |
| 82 | class Comment < ApplicationRecord |
| 83 | belongs_to :card, touch: true |
| 84 | # Updating comment touches card -> touches board -> invalidates all caches |
| 85 | end |
| 86 | ``` |
| 87 | |
| 88 | Nest cache blocks in views: |
| 89 | |
| 90 | ```erb |
| 91 | <% cache @board do %> |
| 92 | <h1><%= @board.name %></h1> |
| 93 | <% @board.columns.each do |column| %> |
| 94 | <% cache column do %> |
| 95 | <% column.cards.each do |card| %> |
| 96 | <% cache card do %> |
| 97 | <%= render card %> |
| 98 | <% end %> |
| 99 | <% end %> |
| 100 | <% end %> |
| 101 | <% end %> |
| 102 | <% end %> |
| 103 | ``` |
| 104 | |
| 105 | ## Pattern 3: Collection Caching |
| 106 | |
| 107 | ```erb |
| 108 | <%# Cache each item individually with multi-fetch optimization %> |
| 109 | <% cache_collection @boards, partial: "boards/board" %> |
| 110 | |
| 111 | <%# Manual alternative %> |
| 112 | <% @boards.each do |board| %> |
| 113 | <% cache board do %> |
| 114 | <%= render "boards/board", board: board %> |
| 115 | <% end %> |
| 116 | <% end %> |
| 117 | ``` |
| 118 | |
| 119 | Use counter caches to avoid N+1 in cache keys: |
| 120 | |
| 121 | ```ruby |
| 122 | class Card < ApplicationRecord |
| 123 | belongs_to :board, counter_cache: true, touch: true |
| 124 | end |
| 125 | |
| 126 | class Board < ApplicationRecord |
| 127 | def cache_key_with_version |
| 128 | "#{cache_key}/cards-#{cards_count}-#{updated_at.to_i}" |
| 129 | end |
| 130 | end |
| 131 | ``` |
| 132 | |
| 133 | ## Pattern 4: Fragment Caching with Custom Keys |
| 134 | |
| 135 | ```erb |
| 136 | <%# Multiple dependencies %> |
| 137 | <% cache ["board_header", @board, Current.user] do %> |
| 138 | <h1><%= @board.name %></h1> |
| 139 | <% if Current.user.can_edit?(@board) %> |
| 140 | <%= link_to "Edit", edit_board_path(@board) %> |
| 141 | <% end %> |
| 142 | <% end %> |
| 143 | |
| 144 | <%# With expiration %> |
| 145 | <% cache ["board_stats", @board], expires_in: 15.minutes do %> |
| 146 | <div class="stats"><%= @board.cards.count %> cards</div> |
| 147 | <% end %> |
| 148 | |
| 149 | <%# Conditional caching %> |
| 150 | <% cache_if @enable_caching, board do %> |
| 151 | <%= board.name %> |
| 152 | <% end %> |
| 153 | |
| 154 | <%# Multi-key with locale %> |
| 155 | <% cache ["dashboard", Current.account, Current.user, |
| 156 | @boards.maximum(:updated_at), I18n.locale] do %> |
| 157 | <%= render "boards_summary", boards: @boards %> |
| 158 | <% end %> |
| 159 | ``` |
| 160 | |
| 161 | ## Pattern 5: Low-Level Caching |
| 162 | |
| 163 | ```ruby |
| 164 | class Board < ApplicationRecord |
| 165 | def statistics |
| 166 | Rails.cache.fetch([self, "statistics"], expires_in: 1.hour) do |
| 167 | { |
| 168 | total_cards: cards.count, |
| 169 | completed_cards: cards.joins(:closure).count, |
| 170 | total_comments: cards.joins(:comments).count |
| 171 | } |
| 172 | end |
| 173 | end |
| 174 | |
| 175 | # Race condition protection for expensive operations |
| 176 | def expensi |