I''ve been playing with fragment caching
http://rails.rubyonrails.com/classes/ActionController/Caching/Fragments.
html and it works great.
But I hit upon the idea of automatically expiring/replacing the fragment
based on some simple checks - examples follow:
1) replace the fragment based on cache age
<% cache :action => "list", :action_suffix =>
"all_topics", :expires_in
=> 1.hour do %>
2) replace the fragment based on hash of expiry tags and a callback
function
<% cache :action => "list", :action_suffix =>
"all_topics", :tags => {},
:expiry_callback => :my_func do %>
The idea above is for the cache to call my_func(old_tags, new_tags)
which returns true/false [to expiry&replace] based on the tag hashes.
For example, a "stock view" fragment might only need to be regenerated
if something like the "price" has changed. In this case, the cache
call
might look like:
<% cache :action => "view_stock", :action_suffix =>
"IPOD", :tags =>
{:price => @stock.price}, :expiry_callback => :price_changed %>
With the callback:
Def price_changed(old_tags, new_tags)
old_tags[:price] <> new_tags[:price]
End
Comments?