Question

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).

Était-ce utile?

La solution

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')
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top