Manually cache the content in the key determined by path. Example:
cache_page "I'm the cached content", "/lists/show"
# File lib/action_controller/caching/pages.rb, line 77 77: def cache_page(content, path, extension = nil, gzip = Zlib::BEST_COMPRESSION) 78: return unless perform_caching 79: path = page_cache_path(path, extension) 80: 81: instrument_page_cache :write_page, path do 82: FileUtils.makedirs(File.dirname(path)) 83: File.open(path, "wb+") { |f| f.write(content) } 84: if gzip 85: Zlib::GzipWriter.open(path + '.gz', gzip) { |f| f.write(content) } 86: end 87: end 88: end
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.
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? } # don't gzip images caches_page :image, :gzip => false
# File lib/action_controller/caching/pages.rb, line 106 106: def caches_page(*actions) 107: return unless perform_caching 108: options = actions.extract_options! 109: 110: gzip_level = options.fetch(:gzip, page_cache_compression) 111: gzip_level = case gzip_level 112: when Symbol 113: Zlib.const_get(gzip_level.to_s.upcase) 114: when Fixnum 115: gzip_level 116: when false 117: nil 118: else 119: Zlib::BEST_COMPRESSION 120: end 121: 122: after_filter({:only => actions}.merge(options)) do |c| 123: c.cache_page(nil, nil, gzip_level) 124: end 125: end
Expires the page that was cached with the path as a key. Example:
expire_page "/lists/show"
# File lib/action_controller/caching/pages.rb, line 65 65: def expire_page(path) 66: return unless perform_caching 67: path = page_cache_path(path) 68: 69: instrument_page_cache :expire_page, path do 70: File.delete(path) if File.exist?(path) 71: File.delete(path + '.gz') if File.exist?(path + '.gz') 72: end 73: end
# File lib/action_controller/caching/pages.rb, line 140 140: def instrument_page_cache(name, path) 141: ActiveSupport::Notifications.instrument("#{name}.action_controller", :path => path){ yield } 142: end
# File lib/action_controller/caching/pages.rb, line 128 128: def page_cache_file(path, extension) 129: name = (path.empty? || path == "/") ? "/index" : URI.parser.unescape(path.chomp('/')) 130: unless (name.split('/').last || name).include? '.' 131: name << (extension || self.page_cache_extension) 132: end 133: return name 134: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.