1
0
Fork 0
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:
José Valim 2011-12-24 09:54:29 +01:00
parent 7b1ac55f50
commit 39081f1660
2 changed files with 24 additions and 20 deletions

View file

@ -38,8 +38,6 @@ module ActionController #:nodoc:
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
@ -47,14 +45,18 @@ module ActionController #:nodoc:
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 @@ module ActionController #:nodoc:
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,17 +107,17 @@ module ActionController #:nodoc:
return unless perform_caching
options = actions.extract_options!
gzip_level = options.fetch(:gzip, :best_compression)
if gzip_level
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
end
after_filter({:only => actions}.merge(options)) do |c|
c.cache_page(nil, nil, gzip_level)

View file

@ -14,6 +14,8 @@ 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
@ -21,7 +23,6 @@ class PageCachingTestController < CachingController
caches_page :no_gzip, :gzip => false
caches_page :gzip_level, :gzip => :best_speed
def ok
head :ok
end
@ -144,7 +145,7 @@ class PageCachingTest < ActionController::TestCase
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