module ActionController::Caching::Pages::ClassMethods

Public Instance Methods

cache_page(content, path, extension = nil, gzip = Zlib::BEST_COMPRESSION) click to toggle source

Manually cache the content in the key determined by path.

cache_page "I'm the cached content", '/lists/show'
# File lib/action_controller/caching/pages.rb, line 92
def cache_page(content, path, extension = nil, gzip = Zlib::BEST_COMPRESSION)
  return unless perform_caching
  path = page_cache_path(path, extension)

  instrument_page_cache :write_page, path do
    tmpfile = Tempfile.new([File.basename(path), File.extname(path)])
    tmpfile.write(content)
    tmpfile.close

    dirname = File.dirname(path)
    FileUtils.makedirs(dirname) unless File.directory?(dirname)
    FileUtils.mv(tmpfile.path, path)

    if gzip
      Zlib::GzipWriter.open(tmpfile.path + '.gz', gzip) { |gz| gz.write(content) }
      FileUtils.mv(tmpfile.path + '.gz', path + '.gz')
    end
  end
end
caches_page(*actions) click to toggle source

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 also pass a :gzip option to override the class configuration one.

# cache the index action
caches_page :index

# cache the index action except for JSON requests
caches_page :index, if: Proc.new { !request.format.json? }

# don't gzip images
caches_page :image, gzip: false
# File lib/action_controller/caching/pages.rb, line 126
def caches_page(*actions)
  return unless perform_caching
  options = actions.extract_options!

  gzip_level = options.fetch(:gzip, page_cache_compression)
  gzip_level = case gzip_level
  when Symbol
    Zlib.const_get(gzip_level.upcase)
  when Fixnum
    gzip_level
  when false
    nil
  else
    Zlib::BEST_COMPRESSION
  end

  after_filter({only: actions}.merge(options)) do |c|
    c.cache_page(nil, nil, gzip_level)
  end
end
expire_page(path) click to toggle source

Expires the page that was cached with the path as a key.

expire_page '/lists/show'
# File lib/action_controller/caching/pages.rb, line 79
def expire_page(path)
  return unless perform_caching
  path = page_cache_path(path)

  instrument_page_cache :expire_page, path do
    File.delete(path) if File.exist?(path)
    File.delete(path + '.gz') if File.exist?(path + '.gz')
  end
end
page_cache_compression() click to toggle source

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.

# File lib/action_controller/caching/pages.rb, line 60
def page_cache_compression
  thread_storage[:page_cache_compression]
end
page_cache_compression=(value) click to toggle source
# File lib/action_controller/caching/pages.rb, line 68
def page_cache_compression=(value)
  thread_storage[:page_cache_compression] = value
end
page_cache_directory() click to toggle source

The cache directory should be the document root for the web server and is set using Base.page_cache_directory = "/document/root". For Rails, this directory has already been set to Rails.public_path (which is usually set to Rails.root + "/public"). Changing this setting can be useful to avoid naming conflicts with files in public/, but doing so will likely require configuring your web server to look in the new location for cached files.

# File lib/action_controller/caching/pages.rb, line 53
def page_cache_directory
  thread_storage[:page_cache_directory]
end
page_cache_directory=(value) click to toggle source
# File lib/action_controller/caching/pages.rb, line 64
def page_cache_directory=(value)
  thread_storage[:page_cache_directory] = value
end
thread_storage() click to toggle source
# File lib/action_controller/caching/pages.rb, line 72
def thread_storage
  Thread.current[:multiple_page_caching] ||= {}
end

Private Instance Methods

instrument_page_cache(name, path) { || ... } click to toggle source
# File lib/action_controller/caching/pages.rb, line 160
def instrument_page_cache(name, path)
  ActiveSupport::Notifications.instrument("#{name}.action_controller", path: path){ yield }
end
page_cache_file(path, extension) click to toggle source
# File lib/action_controller/caching/pages.rb, line 148
def page_cache_file(path, extension)
  name = (path.empty? || path == '/') ? '/index' : URI.parser.unescape(path.chomp('/'))
  unless (name.split('/').last || name).include? '.'
    name << (extension || self.default_static_extension)
  end
  return name
end
page_cache_path(path, extension = nil) click to toggle source
# File lib/action_controller/caching/pages.rb, line 156
def page_cache_path(path, extension = nil)
  page_cache_directory.to_s + page_cache_file(path, extension)
end