关于 caching-strategies
面向 Rails 8 开发者的缓存策略技能,介绍视图片段、低级别和 HTTP 等缓存方式,以及缓存存储和失效策略。适用于优化视图、查询和响应的缓存设计。
主要功能
- 01视图片段与集合缓存
- 02嵌套的 Russian doll 缓存
- 03低级别数据缓存
- 04HTTP 条件请求与缓存头
- 05缓存存储选择与失效策略
适用场景
- 01缓存高开销的视图片段
- 02复用集合中各项的缓存
- 03减少昂贵查询的重复计算
- 04为 API 响应配置 HTTP 缓存
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ thibautbaissac/ rails_ai_agents --skill caching-strategies安装后在 Claude Code 中输入/caching-strategies确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/cache-invalidation.mdhttp-caching-and-testing.mdlow-level-caching.md详细介绍
Rails 8 的缓存策略
概览
Rails 提供多个缓存层:
- 片段缓存:缓存视图局部模板
- 俄罗斯套娃缓存:嵌套缓存片段
- 低级缓存:缓存任意数据
- HTTP 缓存:浏览器和 CDN 缓存
- 查询缓存:在请求期间自动启用
快速入门
# 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"] }
在开发环境中启用缓存:
bin/rails dev:cache
缓存存储选项
| 存储 | 用例 | 优点 | 缺点 |
|---|---|---|---|
:memory_store | 开发环境 | 快速,无需设置 | 不共享,容量有限 |
:solid_cache_store | 生产环境(Rails 8) | 基于数据库,无需 Redis | 速度稍慢 |
:redis_cache_store | 生产环境 | 快速,可共享 | 需要 Redis |
:file_store | 简单的生产环境 | 持久化,无需 Redis | 速度慢,不共享 |
:null_store | 测试 | 不缓存 | 不适用 |
片段缓存
基本片段缓存
<%# 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 %>
缓存键组成部分
Rails 根据以下内容生成缓存键:
- 模型名称
- 模型 ID
updated_at时间戳- 模板摘要(自动生成)
# Generated key example:
# views/events/123-20240115120000000000/abc123digest
自定义缓存键
<%# 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 %>
俄罗斯套娃缓存
嵌套缓存,当外层缓存失效时,内层缓存仍可复用:
<%# 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 %>
在 belongs_to 关联上使用 touch: true,使失效沿关联链向上传递。示例请参阅 cache-invalidation.md。
集合缓存
高效渲染集合
<%# 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?] } %>
低级缓存
最常见的模式是使用带代码块的 Rails.cache.fetch。请参阅 low-level-caching.md,了解以下内容:
- 基本的读取、写入和获取示例
- 在服务对象中缓存
- 在查询对象中缓存
- 实例变量记忆化
- 使用
CurrentAttributes实现请求范围内的记忆化
缓存失效
三种策略:基于时间的过期、基于键的过期(使用 updated_at)和手动删除。请参阅 cache-invalidation.md,了解以下内容:
- 基于时间和基于键的过期
- 在模型回调和服务中手动使缓存失效
- 基于模式的删除(
delete_matched) - 使用
touch: true实现俄罗斯套娃缓存级联失效 - 内置和自定义计数器缓存
HTTP 缓存
使用 stale? 实现条件 GET(ETag/Last-Modified),使用 expires_in 设置 Cache-Control 标头。完整示例请参阅 http-caching-and-testing.md。
测试缓存
使用 :caching 元数据标签在规范中启用缓存。请参阅 http-caching-and-testing.md,了解以下内容:
rails_helper.rb配置- 测试缓存视图的失效
- 测试服务中的缓存失效
- 性能监控和检测
检查清单
- 为环境配置缓存存储
- 为开销较大的局部模板启用片段缓存
- 为俄罗斯套娃缓存,在 belongs_to 上使用
touch: true - 使用
cached: true启用集合缓存 - 为开销较大的查询启用低级缓存
- 制定缓存失效策略
- 为计数启用计数器缓存
- 为 API 设置 HTTP 缓存标头
- 为冷启动预热缓存(如有需要)
- 监控命中率和未命中率
参考资料
- low-level-caching.md - fetch/read/write、服务对象、查询对象、记忆化
- cache-invalidation.md - 过期策略、手动失效、touch、计数器缓存
- http-caching-and-testing.md - ETag、Cache-Control、规范配置、监控
评论
支持 Markdown