#This program is free software: you can redistribute it and/or modify #it under the terms of the GNU General Public License as published by #the Free Software Foundation, either version 3 of the License, or #(at your option) any later version. # #This program is distributed in the hope that it will be useful, #but WITHOUT ANY WARRANTY; without even the implied warranty of #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #GNU General Public License for more details. # #You should have received a copy of the GNU General Public License #along with this program. If not, see . module ActionController module Caching module Pages module ClassMethods # Expires the page that was cached with the +path+ as a key. Example: # expire_memcached_page "/lists/show" def expire_memcached_page(path) return unless perform_caching Cache.delete(path, content) end # Manually cache the +content+ in the key determined by +path+. Example: # memcache_page "I'm the cached content", "/lists/show" def memcache_page(content, path) return unless perform_caching Cache.set(MemcachedPageKeyPrefix + path, content, MemcachedPageTtl, true) end # Caches the +actions+ using the page-caching approach that'll store the # contents in the cache with the key matching the triggering url. def memcaches_page(*actions) return unless perform_caching actions = actions.map(&:to_s) after_filter { |c| c.memcache_page if actions.include?(c.action_name) } end end # Expires the page that was cached with the +options+ as a key. Example: # expire_memcached_page :controller => "lists", :action => "show" def expire_memcached_page(options = {}) return unless perform_caching if options.is_a?(Hash) if options[:action].is_a?(Array) options[:action].dup.each do |action| self.class.expire_memcached_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :action => action))) end else self.class.expire_memcached_page(url_for(options.merge(:only_path => true, :skip_relative_url_root => true))) end else self.class.expire_memcached_page(options) end end # Manually cache the +content+ in the key determined by +options+. If no content is provided, the contents of response.body is used # If no options are provided, the requested url is used. Example: # memcache_page "I'm the cached content", :controller => "lists", :action => "show" def memcache_page(content = nil, options = nil) return unless perform_caching && caching_allowed path = case options when Hash url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format])) when String options else request.path end self.class.memcache_page(content || response.body, path) end private def caching_allowed request.get? && response.headers['Status'].to_i == 200 end end end end