2

I'm attempting to use a sweeper to clear the home page index action when a new article is published.

The home page cache is working fine in development environment and expires after 1 minute. However when an article is saved, the sweeper action is not triggered.

class HomeController < ApplicationController
  caches_action :index, :expires_in => 1.minute
  cache_sweeper :article_sweeper
  def index
    @articles = Article.published.limit(5)
  end
end

class ArticleSweeper < ActionController::Caching::Sweeper
  observe Article
  def after_update(article)
    expire_action(:controller => 'home', :action => 'index')
  end
end

Either I've gone wrong somewhere or a different approach is needed to expire the home page cache.

My app uses ActiveAdmin to update articles, and Dalli for Memcache (as I'll be using Heroku).

Space
  • 2,022
  • 1
  • 19
  • 29
  • how is the configuration for your production dalli store? – phoet Jul 22 '12 at 19:02
  • Did you register your observer? – apneadiving Jul 22 '12 at 22:25
  • @phoet - I'm only in development at this stage. – Space Jul 23 '12 at 18:30
  • @apneadiving - Can't see that in the Railsguides sweepers doc, I have now tried adding this to application.rb though but doesn't appear to have made a difference: config.active_record.observers = :articles_sweeper I could be doing it wrong though. Edit: tried both articles_sweeper and article_sweeper naming across all files. – Space Jul 23 '12 at 18:30
  • so you have the memcache setup for your local development? – phoet Jul 23 '12 at 19:17
  • @phoet - yes, I followed the Heroku doc for this. brew install memcached and in development.rb: config.cache_store = :dalli_store in development.rb and memcached -vv to run daemon locally. Not sure the after_update is getting triggered at all though, so could be a more fundamental issue. – Space Jul 23 '12 at 19:59
  • @Ben: sorry, I wrote too fast, only observers need to be registered. – apneadiving Jul 23 '12 at 20:26

1 Answers1

4

Two steps to the solution:

The controller performing the changes on the model needs to have the sweeper reference, not the destination controller as shown above. In this case it is active_admin, so I added this to my admin/articles.rb file (source) instead of the home controller.

controller do
  cache_sweeper :article_sweeper
end

And the controller name needs a slash

expire_action(:controller => '/home', :action => 'index')
Space
  • 2,022
  • 1
  • 19
  • 29
  • That note about the controller needing a slash is very important/useful. Been chasing my tail on this one for an hour! Thanks for sharing. – Pete May 19 '15 at 21:46