mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Provide a class optin for page_cache_compression.
This commit is contained in:
parent
7b1ac55f50
commit
39081f1660
2 changed files with 24 additions and 20 deletions
|
@ -38,8 +38,6 @@ module ActionController #:nodoc:
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
included do
|
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>.
|
# 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
|
# 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
|
# this setting can be useful to avoid naming conflicts with files in <tt>public/</tt>, but doing so will likely require configuring your
|
||||||
|
@ -47,14 +45,18 @@ module ActionController #:nodoc:
|
||||||
config_accessor :page_cache_directory
|
config_accessor :page_cache_directory
|
||||||
self.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
|
# 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>.
|
# 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
|
# 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.
|
# 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
|
config_accessor :page_cache_extension
|
||||||
self.page_cache_extension ||= '.html'
|
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
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
@ -85,10 +87,11 @@ module ActionController #:nodoc:
|
||||||
end
|
end
|
||||||
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.
|
# 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:
|
# Usage:
|
||||||
#
|
#
|
||||||
|
@ -104,16 +107,16 @@ module ActionController #:nodoc:
|
||||||
return unless perform_caching
|
return unless perform_caching
|
||||||
options = actions.extract_options!
|
options = actions.extract_options!
|
||||||
|
|
||||||
gzip_level = options.fetch(:gzip, :best_compression)
|
gzip_level = options.fetch(:gzip, page_cache_compression)
|
||||||
if gzip_level
|
gzip_level = case gzip_level
|
||||||
gzip_level = case gzip_level
|
when Symbol
|
||||||
when Symbol
|
Zlib.const_get(gzip_level.to_s.upcase)
|
||||||
Zlib.const_get(gzip_level.to_s.upcase)
|
when Fixnum
|
||||||
when Fixnum
|
gzip_level
|
||||||
gzip_level
|
when false
|
||||||
else
|
nil
|
||||||
Zlib::BEST_COMPRESSION
|
else
|
||||||
end
|
Zlib::BEST_COMPRESSION
|
||||||
end
|
end
|
||||||
|
|
||||||
after_filter({:only => actions}.merge(options)) do |c|
|
after_filter({:only => actions}.merge(options)) do |c|
|
||||||
|
|
|
@ -14,6 +14,8 @@ class CachingController < ActionController::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class PageCachingTestController < CachingController
|
class PageCachingTestController < CachingController
|
||||||
|
self.page_cache_compression = :best_compression
|
||||||
|
|
||||||
caches_page :ok, :no_content, :if => Proc.new { |c| !c.request.format.json? }
|
caches_page :ok, :no_content, :if => Proc.new { |c| !c.request.format.json? }
|
||||||
caches_page :found, :not_found
|
caches_page :found, :not_found
|
||||||
caches_page :about_me
|
caches_page :about_me
|
||||||
|
@ -21,7 +23,6 @@ class PageCachingTestController < CachingController
|
||||||
caches_page :no_gzip, :gzip => false
|
caches_page :no_gzip, :gzip => false
|
||||||
caches_page :gzip_level, :gzip => :best_speed
|
caches_page :gzip_level, :gzip => :best_speed
|
||||||
|
|
||||||
|
|
||||||
def ok
|
def ok
|
||||||
head :ok
|
head :ok
|
||||||
end
|
end
|
||||||
|
@ -144,7 +145,7 @@ class PageCachingTest < ActionController::TestCase
|
||||||
assert !File.exist?("#{FILE_STORE_PATH}/page_caching_test/no_gzip.html.gz")
|
assert !File.exist?("#{FILE_STORE_PATH}/page_caching_test/no_gzip.html.gz")
|
||||||
end
|
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)
|
@controller.expects(:cache_page).with(nil, nil, Zlib::BEST_COMPRESSION)
|
||||||
get :default_gzip
|
get :default_gzip
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue