Module: ActionController::Caching::Pages::ClassMethods

Defined in:
actionpack/lib/action_controller/caching/pages.rb

Instance Method Summary collapse

Instance Method Details

#cache_page(content, path, extension = nil) ⇒ Object

Manually cache the content in the key determined by path. Example:

cache_page "I'm the cached content", "/lists/show"


73
74
75
76
77
78
79
80
81
# File 'actionpack/lib/action_controller/caching/pages.rb', line 73

def cache_page(content, path, extension = nil)
  return unless perform_caching
  path = page_cache_path(path, extension)

  instrument_page_cache :write_page, path do
    FileUtils.makedirs(File.dirname(path))
    File.open(path, "wb+") { |f| f.write(content) }
  end
end

#caches_page(*actions) ⇒ Object

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.

Usage:

# cache the index action
caches_page :index

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


93
94
95
96
97
# File 'actionpack/lib/action_controller/caching/pages.rb', line 93

def caches_page(*actions)
  return unless perform_caching
  options = actions.extract_options!
  after_filter({:only => actions}.merge(options)) { |c| c.cache_page }
end

#expire_page(path) ⇒ Object

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

expire_page "/lists/show"


62
63
64
65
66
67
68
69
# File 'actionpack/lib/action_controller/caching/pages.rb', line 62

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)
  end
end