Skip to content

Latest commit

 

History

History
98 lines (58 loc) · 4.3 KB

README.rdoc

File metadata and controls

98 lines (58 loc) · 4.3 KB

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 like so:

    ruby script/plugin install git://github.com/yeah/page_cache_fu.git
  • Add something like :expires_in => 30.minutes to your caches_page calls.

  • Set up a cronjob to periodically run the page_cache_sweeper script using something like this in your crontab:

    * * * * * www-data /usr/bin/ruby /path/to/my/rails_app/script/page_cache_sweeper
  • Tell your web server to use the new cache location using something like this in your config:

Apache

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>

nginx (thanks to cokron)

if (-f $document_root/cache/$host/$uri/index.html) {
  rewrite (.*) /cache/$host/$1/index.html break;
}

if (-f $document_root/cache/$host/$uri.html) {
  rewrite (.*) /cache/$host/$1.html break;
}

Where’s the metadata?

PageCacheFu sets the file modification timestamp of cached pages to the point in time when the cached page is set to expire. The sweeper is periodically checking the filesystem for files which have a modification timestamp that is past the current date and deletes those files.

If you have issues with future dates as modification timestamps or can’t use the modification timestamps like this, PageCacheFu’s page cache expiration mechanism might not be the right thing for you.

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

(cokron says that :expires_in has to be specified. I didn’t have time to verify and fix this yet. See issue #1)

: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_sweeper script. 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 sweeper script)

: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.

Version

0.1

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