This is how I handle page cache sweeping in Ruby on Rails 3.1 with pagination. This should work with earlier versions of Ruby on Rails.

First of all, I’m using the will_paginate gem gem ‘will_paginate’, ‘~> 3.0’ and have my routes setup to match the url and replace the ?page= with /page/2.

In routes.rb:

match '/articles/page/:page' => 'articles#index'

So now you want to do page caching on your site, but the problem is you can’t sweep the folder and the page numbers inside of them normally within the sweeper.

For example, you have your index action which uses pagination:

def index
  @articles = Article.paginate(:page => params[:page], :per_page => 5, :conditions => {:published => true}, :order => ('created_at DESC'))
end

and have it properly cached and watched by the sweeper:

caches_page :index
cache_sweeper :article_sweeper

When the pages get cached, they’ll be placed in /public/articles/page/1.html and so forth. The problem is getting the sweeper to delete the pages when there’s a change to the Article model.

Solution: Manually remove each page on the sweep.