Permalink
Browse files

adding more options

  • Loading branch information...
1 parent 048406f commit 9450cda86d8176aa0caca489c835ed51ab62be44 @yeah yeah committed Sep 12, 2009
Showing with 92 additions and 35 deletions.
  1. +40 −0 README.rdoc
  2. +9 −5 init.rb
  3. +43 −30 lib/page_cache_fu.rb
View
40 README.rdoc
@@ -26,4 +26,44 @@ PageCacheFu adds the following missing features:
If someone could supply a nginx sample config, it'd be greatly appreciated...
+== Options for +caches_page+
+
+You can use the following options with <code>caches_page</code> in your controllers:
+
+=== +:expires_in+
+
+Use this to specify a time interval after which the cache should expire. Example:
+
+ caches_page :show, :expires_in => 30.minutes
+
+=== +:include_query_string+
+
+By default, page_cache_fu stores the query string as part of the cached filename to enable caching for stuff like pagination and search results. You can disable this by setting this to <code>false</code>, like so:
+
+ caches_page :show, :include_query_string => false
+
+=== +:page_cache_directory+
+
+By default, page_cache_fu stores cached files in <code>#{RAILS_ROOT}/public/cache/#{hostname}</code>. If you want to override this, you can use this option. Examples:
+
+ caches_page :show, :page_cache_directory => '/' # yields #{RAILS_ROOT}/public/cache (without hostname)
+ caches_page :show, :page_cache_directory => '/../' # yields #{RAILS_ROOT}/public
+ caches_page :show, :page_cache_directory => '/../my_other_folder' # yields #{RAILS_ROOT}/public/my_other_folder
+
+== Sweeper options
+
+The sweeper is usually called via the <code>page_cache_fu:sweep_expired_page_caches</code> Rake task. However, you can call <code>PageCacheFu::CacheSweeper#sweep_if_expired</code> on your own, if you like. It takes the cache directory as a first parameter and a hash of options as the second. These options are:
+
+=== +:recursive+
+
+Set this to <code>true</code> to descend into subdirectories. (Default in the Rake task)
+
+=== +:match_mode+
+
+Required Unix file permissions. Specify this to tell the sweeper to skip files and directories which don't satisfy your required permissions. For example, <code>:match_mode => 220</code> will skip files which aren't writable by both the file owner and group.
+
+== Credits
+
+The initial code for hostname based caching has been taken from Nate Bibler (initial post) and Andy Triggs (in the comments), http://launchpad.rocketjumpindustries.com/posts/5-defining-a-dynamic-page-cache-loction-by-subdomain-in-rails
+
Copyright (c) 2009 Jan Schulz-Hofen, ROCKET RENTALS GmbH, released under the MIT license
View
14 init.rb
@@ -2,18 +2,22 @@
# require 'lib/page_cache_fu.rb'
Dispatcher.to_prepare do
- ApplicationController.send(:class_inheritable_accessor, :expiry_page_cache_options)
+ ApplicationController.send(:class_inheritable_accessor, :page_cache_fu_options)
if File.expand_path(ActionController::Base.page_cache_directory) == File.expand_path('public',RAILS_ROOT)
ActionController::Base.page_cache_directory = File.expand_path('public/cache',RAILS_ROOT)
end
- unless ActionController::Caching::Pages::ClassMethods.include?(PageCacheFu::Patches::ClassMethods)
- ActionController::Caching::Pages::ClassMethods.send(:include, PageCacheFu::Patches::ClassMethods)
+ unless ActionController::Caching::Pages::ClassMethods.include?(PageCacheFu::Patches::CachingPagesClassMethods)
+ ActionController::Caching::Pages::ClassMethods.send(:include, PageCacheFu::Patches::CachingPagesClassMethods)
end
- unless ActionController::Caching::Pages.include?(PageCacheFu::Patches::InstanceMethods)
- ActionController::Caching::Pages.send(:include, PageCacheFu::Patches::InstanceMethods)
+ unless ActionController::Caching::Pages.include?(PageCacheFu::Patches::CachingPages)
+ ActionController::Caching::Pages.send(:include, PageCacheFu::Patches::CachingPages)
+ end
+
+ unless ActionController::Base.include?(PageCacheFu::Patches::Base)
+ ActionController::Base.send(:include, PageCacheFu::Patches::Base)
end
end
View
73 lib/page_cache_fu.rb
@@ -1,12 +1,12 @@
module PageCacheFu
module Patches
- module ClassMethods
+ module CachingPagesClassMethods
def caches_page_with_expiry(*actions)
- if actions.last.is_a?(::Hash) and (expires_in = actions.last.delete(:expires_in))
- self.expiry_page_cache_options ||= {}
+ if actions.last.is_a?(::Hash)
+ self.page_cache_fu_options ||= {}
actions[0..-2].each do |action|
- self.expiry_page_cache_options[action.to_sym] = (Time.now + expires_in)
+ self.page_cache_fu_options[action.to_sym] = actions.last
end
end
caches_page_without_expiry(*actions)
@@ -18,32 +18,64 @@ def self.included(base)
end
- module InstanceMethods
+ module CachingPages
def expire_page_with_domain_and_query(options)
- expire_page_without_domain_and_query(PageCacheFu::page_cache_path_with_domain_and_query(options, request))
+ expire_page_without_domain_and_query(page_cache_fu_path(options))
end
def cache_page_with_domain_and_query(content = nil, options = nil)
- path = PageCacheFu::page_cache_path_with_domain_and_query(options, request)
+ path = page_cache_fu_path(options)
cache_page_without_domain_and_query(content, path)
end
def cache_page_with_expiry(content = nil, options = nil)
cache_page_without_expiry(content, options)
- if self.class.expiry_page_cache_options and (expires_in = self.class.expiry_page_cache_options[params[:action].to_sym])
- file = self.class.send(:page_cache_path, PageCacheFu::page_cache_path_with_domain_and_query((options||request.path), request))
- File.utime(expires_in.to_time, expires_in.to_time, file) if File.exists?(file)
+ if self.class.page_cache_fu_options[params[:action].to_sym] and (expires_in = self.class.page_cache_fu_options[params[:action].to_sym][:expires_in])
+ expires_at = Time.now + expires_in
+ file = self.class.send(:page_cache_path, options)
+ File.utime(expires_at, expires_at, file) if File.exists?(file)
end
end
def self.included(base)
base.send(:alias_method_chain, :expire_page, :domain_and_query)
- base.send(:alias_method_chain, :cache_page, :domain_and_query)
base.send(:alias_method_chain, :cache_page, :expiry)
+ base.send(:alias_method_chain, :cache_page, :domain_and_query)
end
end
+
+ module Base
+ def page_cache_fu_path(orig_path)
+
+ page_cache_fu_options = case orig_path
+ when Hash
+ self.class.page_cache_fu_options[orig_path[:action].to_sym]
+ when String
+ nil
+ else
+ self.class.page_cache_fu_options[self.request[:action].to_sym]
+ end
+
+ path = (page_cache_fu_options[:page_cache_directory]||"/#{self.request.host}/")
+ path << case orig_path
+ when Hash
+ url_for(orig_path.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format]))
+ when String
+ orig_path
+ else
+ if self.request.path.empty? || self.request.path == '/'
+ '/index'
+ else
+ self.request.path
+ end
+ end
+ path << CGI::escape(self.request.query_string) if !self.request.query_string.blank? and page_cache_fu_options[:include_query_string] != false
+ return path
+ end
+
+ end
end
module CacheSweeper
def self.sweep_if_expired(file, options={})
@@ -86,23 +118,4 @@ def self.mode_match(mode1, mode2)
end
end
- def self.page_cache_path_with_domain_and_query(orig_path, request)
- path = "/#{request.host}/"
- path << case orig_path
- when Hash
- url_for(orig_path.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format]))
- when String
- orig_path
- else
- if request.path.empty? || request.path == '/'
- '/index'
- else
- request.path
- end
- end
- path << CGI::escape(request.query_string) unless request.query_string.blank?
- return path
- end
-
-
end

0 comments on commit 9450cda

Please sign in to comment.