Permalink
Browse files

Provide a class optin for page_cache_compression.

  • Loading branch information...
1 parent 754823d commit ba868929d3461799714bb6d73773b5824cb1384e @josevalim josevalim committed Dec 24, 2011
Showing with 24 additions and 20 deletions.
  1. +20 −17 actionpack/lib/action_controller/caching/pages.rb
  2. +4 −3 actionpack/test/controller/caching_test.rb
@@ -16,7 +16,7 @@ module Caching
# caches_page :show, :new
# end
#
- # This will generate cache files such as <tt>weblog/show/5.html</tt> and <tt>weblog/new.html</tt>, which match the URLs used
+ # This will generate cache files such as <tt>weblog/show/5.html</tt> and <tt>weblog/new.html</tt>, which match the URLs used
# that would normally trigger dynamic page generation. Page caching works by configuring a web server to first check for the
# existence of files on disk, and to serve them directly when found, without passing the request through to Action Pack.
# This is much faster than handling the full dynamic request in the usual way.
@@ -38,23 +38,25 @@ module Pages
extend ActiveSupport::Concern
included do
- ##
- # :singleton-method:
# The cache directory should be the document root for the web server and is set using <tt>Base.page_cache_directory = "/document/root"</tt>.
# For Rails, this directory has already been set to Rails.public_path (which is usually set to <tt>Rails.root + "/public"</tt>). Changing
# this setting can be useful to avoid naming conflicts with files in <tt>public/</tt>, but doing so will likely require configuring your
# web server to look in the new location for cached files.
config_accessor :page_cache_directory
self.page_cache_directory ||= ''
- ##
- # :singleton-method:
# Most Rails requests do not have an extension, such as <tt>/weblog/new</tt>. In these cases, the page caching mechanism will add one in
# order to make it easy for the cached files to be picked up properly by the web server. By default, this cache extension is <tt>.html</tt>.
# If you want something else, like <tt>.php</tt> or <tt>.shtml</tt>, just set Base.page_cache_extension. In cases where a request already has an
# extension, such as <tt>.xml</tt> or <tt>.rss</tt>, page caching will not add an extension. This allows it to work well with RESTful apps.
config_accessor :page_cache_extension
self.page_cache_extension ||= '.html'
+
+ # The compression used for gzip. If false (default), the page is not compressed.
+ # If can be a symbol showing the ZLib compression method, for example, :best_compression
+ # or :best_speed or an integer configuring the compression level.
+ config_accessor :page_cache_compression
+ self.page_cache_compression ||= false
end
module ClassMethods
@@ -85,10 +87,11 @@ def cache_page(content, path, extension = nil, gzip = Zlib::BEST_COMPRESSION)
end
end
- # Caches the +actions+ using the page-caching approach that'll store the cache in a path within the page_cache_directory that
+ # Caches the +actions+ using the page-caching approach that'll store
+ # the cache in a path within the page_cache_directory that
# matches the triggering url.
#
- # You can disable gzipping by setting +:gzip+ option to false.
+ # You can also pass a :gzip option to override the class configuration one.
#
# Usage:
#
@@ -104,16 +107,16 @@ def caches_page(*actions)
return unless perform_caching
options = actions.extract_options!
- gzip_level = options.fetch(:gzip, :best_compression)
- if gzip_level
- gzip_level = case gzip_level
- when Symbol
- Zlib.const_get(gzip_level.to_s.upcase)
- when Fixnum
- gzip_level
- else
- Zlib::BEST_COMPRESSION
- end
+ gzip_level = options.fetch(:gzip, page_cache_compression)
+ gzip_level = case gzip_level
+ when Symbol
+ Zlib.const_get(gzip_level.to_s.upcase)
+ when Fixnum
+ gzip_level
+ when false
+ nil
+ else
+ Zlib::BEST_COMPRESSION
end
after_filter({:only => actions}.merge(options)) do |c|
@@ -14,14 +14,15 @@ class CachingController < ActionController::Base
end
class PageCachingTestController < CachingController
+ self.page_cache_compression = :best_compression
+
caches_page :ok, :no_content, :if => Proc.new { |c| !c.request.format.json? }
caches_page :found, :not_found
caches_page :about_me
caches_page :default_gzip
caches_page :no_gzip, :gzip => false
caches_page :gzip_level, :gzip => :best_speed
-
def ok
head :ok
end
@@ -144,7 +145,7 @@ def test_should_allow_to_disable_gzip
assert !File.exist?("#{FILE_STORE_PATH}/page_caching_test/no_gzip.html.gz")
end
- def test_should_use_best_gzip_by_default
+ def test_should_use_config_gzip_by_default
@controller.expects(:cache_page).with(nil, nil, Zlib::BEST_COMPRESSION)
get :default_gzip
end
@@ -233,7 +234,7 @@ class ActionCachingTestController < CachingController
caches_action :show, :cache_path => 'http://test.host/custom/show'
caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" }
caches_action :with_layout
- caches_action :with_format_and_http_param, :cache_path => Proc.new { |c| { :key => 'value' } }
+ caches_action :with_format_and_http_param, :cache_path => Proc.new { |c| { :key => 'value' } }
caches_action :layout_false, :layout => false
caches_action :record_not_found, :four_oh_four, :simple_runtime_error

0 comments on commit ba86892

Please sign in to comment.