Rails - fragment cache does not expire

I have 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 because the console output displays "expire_home_cache".

But the fragment is not updating ...

Any ideas?

+1


a source to share


4 answers


You can 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 think the problem might be that Ruby or rails have a hard time figuring out what a key is and so the cache and expire_fragment method generate two different cache keys.

+2


a source


Try replacing expire_fragment(:key => "news")

withActionController::Base.new.expire_fragment(:key => "news")



Nothing to explain, but it worked for me.

+4


a source


The correct way to do it is:

cache :news do

  ...
end

      

And then in your sweeper:

expire_fragment :news

      

+1


a source


This doesn't answer your question directly, 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 to expire fragments in my projects.

0


a source







All Articles