Expiring cache from a rake task


I have a question on cache expiration only working from the web, and not
from the console or from a rake task.

I have 2 models, Post e Comments.
I have this page /archives where I list all old posts.
I created a controller ‘archives’, where I have a page cache
I need to expire this page cache, every time a Post or Comment is

Using sweepers, this works 100%. (

The problem is that, now, I implemented spam moderation by Akismet.
So when a new comment is created in the database, it gets a ‘pending’
status. And there’s a rake task that runs every x minutes, in order to
all ‘pending’ comments against the Akismet database, and only then
earn an ‘approved’ or ‘spam’ status. And it’s only then that I should
the /archives page.

So I need to expire a cached page, from something that happens outsite a
controller. And it seems the only situation a sweeper works is when the
event is triggered by the web.

If I open the console, add a new comment to a post, nothing happens
And now I run the Akismet check, and assuming it gives the comment an
‘approved’ status, the cache is not expired, when it should be.

I tried to use Observers, from the Rails docs (

In environment.rb I added the line: config.active_record.observers =
I created the file app/models/archives_observer.rb with:

class ArchivesObserver < ActiveRecord::Observer
observe :post, :comment

def after_save(record)
expire_page(:controller => ‘archives’, :action => ‘index’)

But I get an error: NoMethodError: undefined method `expire_page’ for

I tried this also:

class ArchivesObserver < ActionController::Caching::Sweeper

In this case, there’s no error, and it does nothing at all. The
public/archives.html just stays there.

I’m trying my best to avoid hacking it, using a simple File.delete :slight_smile:
Like the code below:

# File rails/actionpack/lib/action_controller/caching/pages.rb, line 

65: def expire_page(path)
66: return unless perform_caching
68: benchmark “Expired page: #{page_cache_file(path)}” do
69: File.delete(page_cache_path(path)) if
70: end
71: end

Is there a way to expire pages without having to hack it?

Thanks a lot,

On Feb 11, 11:14 am, “Levy Carneiro Jr.” [email protected]

Is there a way to expire pages without having to hack it?
I ran into a similar issue a few days ago. If I remember correctly, if
you look at what Rails is doing under the covers, you’ll see that
ActionController::Caching::Sweeper subclasses will only work when
called from inside a controller. Basically the “expire_page” message
ends up getting intercepted by “method_missing”, which tries to
forward it on to a controller stored in the @controller instance
variable. So if you invoke your Sweeper subclass from somewhere else
(like a Rake task), that @controller variable will be nil and nothing
will happen.

My workaround was just to fall back to File.delete and friends. Ugly,
but it works.


Thanks, Vincent.

I just wanted to know if there was something better. I guess I’ll resort
the File.delete and friends as you mentioned :slight_smile:


Coming in late, but this might be helpful… I’ve used this from ./
script/runner… not quite rake, but…

require ‘lib/console_app’

ActionController::Base.expire_page(app.user_profile_path(:id =>

(where ‘user_profile’ is a named route)