Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Allow sweepers to run expire_fragment without declaring cache_sweeper

#1961

Hi,

I've tried to find a ticket related to this issue, and didn't find any, sorry if it has been discussed here before. Ok, here's my problem :

I have an activity feed on my site. I'd like to fragment_cache it. The problem occurs when I want to invalid the cache when a new entry is created. I've setup a sweeper, declared it as an observer.

in the sweeper's after_create, I have :


def after_create
  puts("Here I am")
  expire_fragment(:controller => :last_activities, :action => :index, :locale => I18n.locale)
end

When a new entry is created, I can see the "Here I am" output, and expire_fragment will do nothing, silently. I've found some topics in forums saying that only a cache_sweeper in a controller can fire this. That's 100% true. My problem is now to retreive which controllers may create an entry, looking for some observed models before to investigate. This is painful and error-prone, since I will certainly forget a controller.

Would it be possible to have sweepers fire this method (I really don't see the link with a controller here)

Thanks a lot Gravis

Reported by Gravis · February 13th, 2009 @ 02:59 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Marcel Jackwerth
    Marcel Jackwerth

    Just ran across the same problem - most websites are describing how to use the Sweeper as an observer (which makes sense, since @@@class Sweeper < ActiveRecord::Observer@@@) but additionally the Sweeper requires a request which went through a controller before hand. It's definately not intuitive. Workers and other deamons (which do not rely on a controller) won't be able to swipe the caches.

    I use the following workaround in my Sweepers to make them cache-aware. Works with 2.3.2 but might cause issues sooner or later.

    
      delegate :cache_configured?, :cache_store, :to => ActionController::Base
      def perform_action; true; end
      def render; true; end
    
      def self.logger
        ActionController::Base.logger
      end
    
      include ActionController::Benchmarking
      include ActionController::Caching::Fragments
    

    I wouldn't use these Sweepers in your controllers then anymore.

    April 12th, 2009 @ 12:57 AM

  2. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  3. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Importance changed from to

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:41 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:41 PM