Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Adding public options to client-side caching

#2095

If you want to use Proxy caches with Rails, you have to modify the "Cache-Control" header to have the public keyword. This is defaulted to private in rails, which is fine, but the syntax to set cache-control to public isn't too pretty:


expires_in 10.minutes, :private => false, :public => true
 
fresh_when :last_modified => @user.updated_at.utc
headers['Cache-Control'] = 'public'
 
fresh_when :etag => @user
headers['Cache-Control'] = 'public'

Here is code (with tests) that allows you to do:


expires_in 10.minutes, :public => true
 
fresh_when :last_modified => @user.updated_at.utc, :public => true
 
fresh_when :etag => @user, :public => true

I've also added some additional tests for expires_at that seemed to be missing from the code. For additional explanation, see the Advanced HTTP Caching Screencast.

Reported by Gregg Pollack · February 27th, 2009 @ 07:14 PM

State: resolved
Milestone: 2.x
Assigned to: DHH DHH
Importance: none

Activity

  1. Repository
    Repository
    • State changed from new to committed

    (from [f2a32bd0dedf11021027e36cc2c99f97434cae17]) Added ability to pass in :public => true to fresh_when, stale?, and expires_in to make the request proxy cachable [#2095 Adding public options to client-side caching state:committed]

    Signed-off-by: David Heinemeier Hansson david@loudthinking.com http://github.com/rails/rails/co...

    February 28th, 2009 @ 09:34 AM

  2. Johan Sørensen
    Johan Sørensen
    • Assigned user set to DHH

    Actually, the default Cache-Control seems to be no-cache now, when you use the conditional GET helpers at least. So this patch only helps if you only want public headers.

    I'm thinking this patch should be reworked so that you can say :public => false. Which would make it set a Cache-Control header of private, instead of the no-cache default. This is useful if you want the clients to cache it, but not any proxies (assuming they play nice of course).

    March 5th, 2009 @ 03:40 PM

  3. Johan Sørensen
    Johan Sørensen

    Due to the fact that this commit introduces two test methods named test_expires_in_header this is never exposed in the tests.

    Attached is a patch that renames one of the test methods and fixes the regression.

    March 6th, 2009 @ 09:26 AM

  4. Pratik
    Pratik
    • State changed from committed to open

    March 7th, 2009 @ 12:17 PM

  5. Repository
    Repository
    • State changed from open to resolved

    (from [c071123b3e2d6fd918dd7e419efe7429355445c2]) Ensure expires_in without a :public key sets the Cache-Control header to private. [#2095 Adding public options to client-side caching state:resolved]

    This fixes a regression introduced in f2a32bd0, which wasn't exposed due to two test methods having the same name.

    Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...

    March 7th, 2009 @ 12:19 PM

  6. bingbing