diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-17 09:20:20 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-07-17 09:20:20 +0000 |
commit | d2d38f67004c529eda0009f8abc0d2a573dd3196 (patch) | |
tree | 60228b99d6490e7c4e32ac6c153566e9b298411f /actionpack | |
parent | 6baedb09362c12dca48863b6a26aa42c6e253019 (diff) | |
download | rails-d2d38f67004c529eda0009f8abc0d2a573dd3196.tar.gz rails-d2d38f67004c529eda0009f8abc0d2a573dd3196.tar.bz2 rails-d2d38f67004c529eda0009f8abc0d2a573dd3196.zip |
Added Base#expires_in(seconds)/Base#expires_now to control HTTP content cache headers #1755 [Thomas Fuchs]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1845 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rwxr-xr-x | actionpack/lib/action_controller/base.rb | 23 |
2 files changed, 25 insertions, 0 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 29aa7a1bbc..842ff278cb 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added Base#expires_in(seconds)/Base#expires_now to control HTTP content cache headers #1755 [Thomas Fuchs] + * Fixed line number reporting for Builder template errors #1753 [piotr] * Fixed assert_routing so that testing controllers in modules works as expected [Nicholas Seckar, Rick Olson] diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 5d6e8d9777..ac733c1d36 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -712,6 +712,29 @@ module ActionController #:nodoc: end end end + + # Sets a HTTP 1.1 Cache-Control header. Defaults to issuing a "private" instruction, so that + # intermediate caches shouldn't cache the response. + # + # Examples: + # expires_in 20.minutes + # expires_in 3.hours, :private => false + # expires in 3.hours, 'max-stale' => 5.hours, :private => nil, :public => true + # + # This method will overwrite an existing Cache-Control header. + # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html for more possibilities. + def expires_in(seconds, options = {}) + cache_options = { 'max-age' => seconds, 'private' => true }.symbolize_keys.merge!(options.symbolize_keys) + cache_options.delete_if { |k,v| v.nil? or v == false } + cache_control = cache_options.map{ |k,v| v == true ? k.to_s : "#{k.to_s}=#{v.to_s}"} + @response.headers["Cache-Control"] = cache_control.join(', ') + end + + # Sets a HTTP 1.1 Cache-Control header of "no-cache" so no caching should occur by the browser or + # intermediate caches (like caching proxy servers). + def expires_now + @response.headers["Cache-Control"] = "no-cache" + end # Resets the session by clearing out all the objects stored within and initializing a new session object. def reset_session #:doc: |