Hello,
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
(caches_page
:index).
I need to expire this page cache, every time a Post or Comment is
created/updated/destroyed.
Using sweepers, this works 100%. (
http://www.railsenvy.com/2007/2/28/rails-caching-tutorial#sweepers)
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
check
all ‘pending’ comments against the Akismet database, and only then
comments
earn an ‘approved’ or ‘spam’ status. And it’s only then that I should
expire
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
(ok!).
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 (
http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M001871&name=observe
).
In environment.rb I added the line: config.active_record.observers =
:archives_observer
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’)
end
end
But I get an error: NoMethodError: undefined method `expire_page’ for
#ArchivesObserver:0x247eddc
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
Like the code below:
# File rails/actionpack/lib/action_controller/caching/pages.rb, line
65
65: def expire_page(path)
66: return unless perform_caching
67:
68: benchmark “Expired page: #{page_cache_file(path)}” do
69: File.delete(page_cache_path(path)) if
File.exist?(page_cache_path(path))
70: end
71: end
Is there a way to expire pages without having to hack it?
Thanks a lot,
Levy