mcpField
C
caching-strategies

caching-strategies

github.com/thibautbaissac开发67156 次安装22 周前更新
代码仓库

关于 caching-strategies

面向 Rails 8 开发者的缓存策略技能,介绍视图片段、低级别和 HTTP 等缓存方式,以及缓存存储和失效策略。适用于优化视图、查询和响应的缓存设计。

主要功能

  1. 01视图片段与集合缓存
  2. 02嵌套的 Russian doll 缓存
  3. 03低级别数据缓存
  4. 04HTTP 条件请求与缓存头
  5. 05缓存存储选择与失效策略

适用场景

  1. 01缓存高开销的视图片段
  2. 02复用集合中各项的缓存
  3. 03减少昂贵查询的重复计算
  4. 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.md
http-caching-and-testing.md
low-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 缓存标头
  • 为冷启动预热缓存(如有需要)
  • 监控命中率和未命中率

参考资料

评论

支持 Markdown