Rails 3.1 and Http Cache

Given that Heroku Kedra does not have http caching provided by Varnish, I would like to use Rack::Cache . I was told that in rails 3.1.1 Rack::Cache active by default, I just need to make sure the configuration:

 config.action_controller.perform_caching = true 

and I need to select cache storage, for this experiment I use:

 config.cache_store = :memory_store 

In the action of the page I want to cache, I added the following lines:

 response.header['Cache-Control'] = 'public, max-age=300' response.header['Expires'] = CGI.rfc1123_date(Time.now + 300) 

This code is used to work with varnish, the first request returns 200, and the next (within 5 minutes) returns 304 .

This does not happen with Rails 3.1 and Heroku Cedar Stack. I get these headers in the response, but subsequent requests return 200 instead of 304.

What am I doing wrong? Thanks.

+7
source share
1 answer

As you noted, cedar stack does not use varnish. This means that the web request always hits the ruby ​​server.

With that in mind, Rack :: Cache will respect your headers and serve cached content.

However, since the request does go through the http level in the rails application, the response will always be 200, because the cache will no longer be at the http level.

To confirm this, return this to one of your cached actions:

<%= Time.now.to_i %>

Then reload the page several times and you will notice that the timestamp does not change.

+11
source

All Articles