如何使用 Heroku 和 Cloudfront 激活 Rails 的页面缓存?

How to activate page cache for Rails with Heroku & Cloudfront?

网站分析器说我没有激活页面缓存,https://tools.pingdom.com/#!/bDj23F/www.anthonygalli.com。不过,我还是按照我能动手的每一步去做。我正在使用 rails "4.2.7.1"

在视图中

<%= render :partial => @posts, cached: true %>

在控制器中

fresh_when last_modified: @posts.created_at.utc, etag: @posts

在production.rb

config.action_controller.perform_caching = true
config.action_controller.asset_host = 'd37p52igaahgm9.cloudfront.net'

更新

你可以在你的云端分配中做的一件事是添加一个 custom-header parameter.Here,Google and Amazon 不完全同意 Google 建议使用 Expires header 亚马逊建议使用 Cache-Control max-age parameter/value,所以你必须选择一个,因为如果你指定两个

,亚马逊将只处理 Cache-Control

We recommend that you use the Cache-Control max-age directive instead of the Expires header field to control object caching. If you specify values both for Cache-Control max-age and for Expires, CloudFront uses only the value of Cache-Control max-age.

如果您的云端分发来自 S3 存储桶,您可以 Add Headers to Your Objects Using the Amazon S3 Console

  • 从 aws s3 控制台打开 s3 object 的属性
  • 在右窗格中,展开元数据。
  • 单击“添加更多元数据”。
  • 在“密钥”列表中,单击 Cache-Control 或“过期”(视情况而定)。
  • 在“值”字段中,输入适用的值:

    • 对于 Cache-Control 字段,输入:

      max-age=number of seconds that you want objects to stay in a CloudFront edge cache
      
    • 对于“过期”字段,请以 HTML 格式输入日期和时间。

  • 点击保存。

见下文

据我所知,在rails

中有三种兑现方式
  • 页面缓存 => 将所有页面放入缓存..主要用于 静态页面

  • Action Caching => Top put a controllers action

  • 片段缓存 => 对于视图的一部分/或逻辑等

还需要实施观察者和清扫器以在某些 changes/conditions 上过期缓存,否则将再次渲染 n 次