Skip to content
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.
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



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


  • 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_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 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

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:


Use this to specify a time interval after which the cache should expire. Example:

caches_page :show, :expires_in => 30.minutes


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


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:


Set this to true to descend into subdirectories. (Default in the Rake task)


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.




The initial code for hostname based caching has been taken from Nate Bibler (initial post) and Andy Triggs (in the comments),

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

Something went wrong with that request. Please try again.