Skip to main content

rails-caching

Rails caching strategies including fragment caching, low-level caching with Rails.cache, cache keys, and conditional GET. Use when the user asks about caching performance or invalidation in Rails.

Informações da origem

Repositório
bastos/ruby-plugin-marketplace
Última atividade na origem
9 de janeiro de 2026 às 03:05
Idioma detectado do SKILL.md
inglês
Estrelas
3
Forks
0

Opções de instalação

Por padrão, está selecionado o prompt que primeiro revisa a origem. Você pode mudar para um comando direto ou baixar uma cópia local.

Revise os arquivos de origem

Leia o SKILL.md e os arquivos complementares exibidos pelo SkillsMP antes de decidir se vai instalar.

Exibindo SKILL.md

SKILL.md
Instruções da origem · Visualização somente leitura
name
rails-caching
description
Rails caching strategies including fragment caching, low-level caching with Rails.cache, cache keys, and conditional GET. Use when the user asks about caching performance or invalidation in Rails.
version
1.0.0
# Rails Caching Use Rails caching to reduce response time and database load while keeping content fresh. This skill focuses on fragment caching, low-level caching, cache keys, and conditional GETs. ## Fragment caching Wrap view fragments that can be reused: ```erb <% @products.each do |product| %> <% cache product do %> <%= render product %> <% end %> <% end %> ``` Notes: - Cache keys include a template digest; edits to the fragment invalidate it. - Model versions (`cache_key_with_version`) help expire cached content when records change. ## Low-level caching with Rails.cache Use `Rails.cache.fetch` for expensive computations: ```ruby class Product < ApplicationRecord def competing_price Rails.cache.fetch("#{cache_key_with_version}/competing_price", expires_in: 12.hours) do Competitor::API.find_price(id) end end end ``` Guidance: - Prefer `fetch` to avoid separate read/write logic. - Choose stable keys; use arrays/hashes when appropriate. - Set `expires_in` for data that should age out. ## Cache keys Objects should implement `cache_key` (Active Record provides it). Keys can be arrays or hashes: ```ruby Rails.cache.read(site: "mysite", owners: [owner_1, owner_2]) ``` Notes: - Storage backends may modify keys; don’t depend on backend-specific formats. ## Conditional GETs Leverage ETags and `Last-Modified` to return `304 Not Modified`: ```ruby class ProductsController < ApplicationController def show @product = Product.find(params[:id]) if stale?(last_modified: @product.updated_at.utc, etag: @product.cache_key_with_version) # normal response rendering end end end ``` ## When to use this skill - Cache expensive views or queries - Decide between fragment and low-level caching - Configure cache stores as needed - Implement cache keys and conditional GETs
Ver no GitHub