Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
PageCacheFu adds the following missing features to Rails page caching: expiry time for cached pages, different caches for different hostnames (e.g. subdomains), and different caches for query strings.
tree: 87a6e6d2fe

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
tasks
test
MIT-LICENSE
README.rdoc
Rakefile
init.rb
install.rb
uninstall.rb

README.rdoc

PageCacheFu

PageCacheFu adds the following missing features:

  • Expiry time for cached pages (using :expires_in option)

  • Different caches for different hostnames (e.g. subdomains)

  • Different caches for query strings

Installation

  • Just install the plugin, add something like :expires_in => 30.minutes to your caches_page calls.

  • Set up a cronjob to periodically run the page_cache_fu:sweep_expired_page_caches Rake task.

  • Tell your Apache to use the new cache location using something like this in your Virtualhost config:

    RewriteMap uri_escape int:escape
    <Directory /path/to/my/rails_app/public/>
      RewriteEngine On
      RewriteCond %{REQUEST_METHOD} GET [NC]
      RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}%{REQUEST_URI}%{QUERY_STRING}.html -f
      RewriteRule ^([^.]+)$ cache/%{HTTP_HOST}/$1${uri_escape:%{QUERY_STRING}}.html [L]		
    
      RewriteCond %{REQUEST_METHOD} GET [NC]
      RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}/index.html -f
      RewriteRule ^$ cache/%{HTTP_HOST}/index.html
    </Directory>

If someone could supply a nginx sample config, it'd be greatly appreciated…

Options for caches_page

You can use the following options with caches_page 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 false, like so:

caches_page :show, :include_query_string => false

:page_cache_directory

By default, page_cache_fu stores cached files in #{RAILS_ROOT}/public/cache/#{hostname}. 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 page_cache_fu:sweep_expired_page_caches Rake task. However, you can call PageCacheFu::CacheSweeper#sweep_if_expired 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 true 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, :match_mode => 220 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), launchpad.rocketjumpindustries.com/posts/5-defining-a-dynamic-page-cache-loction-by-subdomain-in-rails

Copyright © 2009 Jan Schulz-Hofen, ROCKET RENTALS GmbH, released under the MIT license

Something went wrong with that request. Please try again.