1

This one has me stumped.

I have a view with a cached fragment:

 - cache :key=>"news" do    
   %h2 News
   - etc

I have a sweeper that uses:

def expire_home_cache
  puts "expire_home_cache"
  expire_fragment(:key => "news") 
end

The sweeper is called as I can see "expire_home_cache" in the console output.

But the fragment is not updated ...

Any ideas?

Toby Hede
  • 36,755
  • 28
  • 133
  • 162

4 Answers4

4

Try replacing expire_fragment(:key => "news") with ActionController::Base.new.expire_fragment(:key => "news")

No time to explain, but it worked for me.

fkoessler
  • 6,932
  • 11
  • 60
  • 92
  • Make sure you put --- cache_sweeper :whatever_sweeper --- in your application_controller.rb, for the above to work. – concept47 Sep 07 '10 at 11:18
  • I was having an unexplainable instance where the sweeper would refuse to expire the cache for a child association until I tried this answer, and it worked. I don't understand why. The sweeper callbacks were getting called as was the expire_fragment code. Odd. – twmills Mar 06 '12 at 03:55
  • I don't know why, but I had similar results: expire_fragment(key) was not working, but when I prepended that call with logger.warn (initially just to see the output of expire_fragment) the fragment expiration suddenly works like a charm. Confusing. – effkay Jan 08 '13 at 13:51
  • Awesome tip, it works. But if you get a time, I would like to see explaination... – Gabor Garami Jun 23 '13 at 17:59
2

You might try this:

   cache("news") do    
     %h2 News
     - etc
   end

and...

def expire_home_cache
  puts "expire_home_cache"
  expire_fragment("news") 
end

...or try this ...

 - cache({:key=>"news"}) do    
   %h2 News
   - etc

I am thinking the issue may be that ruby or rails is having a hard time determining what the key is exactly and so the cache method and expire_fragment are generating two different cache keys.

dave elkins
  • 331
  • 2
  • 4
1

The proper way to do this is:

cache :news do

  ...
end

And then in your sweeper:

expire_fragment :news
Mark S.
  • 1,082
  • 1
  • 12
  • 22
0

This doesn't directly answer your question, but have you tried the timed_fragment_cache plugin as an alternative?

http://github.com/tricycle/timed_fragment_cache/tree/master

I found this to be a much simpler way of expiring fragments in my projects.

tmarkiewicz
  • 515
  • 5
  • 12