Rails - fragment cache not expiring

2019-04-15 17:27发布

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?

4条回答
放我归山
2楼-- · 2019-04-15 17:46

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-15 17:46

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.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-04-15 17:49

The proper way to do this is:

cache :news do

  ...
end

And then in your sweeper:

expire_fragment :news
查看更多
疯言疯语
5楼-- · 2019-04-15 17:57

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

No time to explain, but it worked for me.

查看更多
登录 后发表回答