简体   繁体   中英

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

A website analyzer says I don't have page cache activated, https://tools.pingdom.com/#!/bDj23F/www.anthonygalli.com . I followed every step I could get my hands on though. I'm using rails "4.2.7.1"

In the view

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

In the controller

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

In production.rb

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

UPDATE

在此处输入图片说明

one thing you can do in your cloudfront distribution is to add a custom-header parameter.Here, Google and Amazon do not fully agree as Google recommends the use of Expires header and Amazon recommends the use of Cache-Control max-age parameter/value, so you must choose one as amazon will treat only Cache-Control if you specify the two

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.

If your cloudfront distribution comes from a S3 bucket you can Add Headers to Your Objects Using the Amazon S3 Console

  • Open the Properties of your s3 object from the aws s3 console
  • In the right pane, expand Metadata.
  • Click Add More Metadata.
  • In the Key list, click Cache-Control or Expires, as applicable.
  • In the Value field, enter the applicable value:

    • For a Cache-Control field, enter:

       max-age=number of seconds that you want objects to stay in a CloudFront edge cache 
    • For an Expires field, enter a date and time in HTML format.

  • Click Save.

See below in action

在此处输入图片说明

As per my knowledge, there are three type of cashing in rails

  • Page Caching => For all page to put in cache.. mostly used for static pages

  • Action Caching => Top put a controllers action

  • Fragment Caching => For A part of view/ or for a logic etc

Also necessary to implement observers and sweeper to expire cache on some changes/conditions, otherwise same will be render again n again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM