Follow up on caches_action and content_for blocks
December 16th, 2009
Alright, I worked on a solution today to follow up on my earlier post… This is very raw, but showing some good signs of working. I’ve been testing locally against memcached. One concern, I have with this solution is the content_for block is cached in a separate key, if the action key expires after the content_for key, then some requests might render without the content_for content… Perhaps, before rendering the action from cache we need to check that all fragments are available? This could also make it possible to do a multi get from memcached as well? Let me know what you all think.

Tried it. Seems to work. But the cache key for content_for contents does not consider :cache_path Proc. Below is the example:
1. In Controller
caches_action_with_content_for :predl,
:expires_in => Conf.general['cache_expire'].minutes,
:layout => false,
:cache_path => Proc.new{|controller| controller.params.merge({:region => controller.current_region.name_lat})}
So, I merge external parameter “:region” (which is taken from the user session in my case) into the cache key.
These are the relevant lines from the server log (region is “ua”):
calling our version of the render method # -> predl
caching names: [:description]
use cache: cryo.feta.apress.ru/predl/computer/communications;description :predl -> {:expires_in=>1800 seconds}
Cached fragment hit: views/cryo.feta.apress.ru/ua/predl/computer/communications (34.9ms)
So, the content of the action itself is cached with correct key (views/cryo.feta.apress.ru/ua/predl/computer/communications), but all content_for blocks have no “ua” in their cache_key (cryo.feta.apress.ru/predl/computer/communications;description).
Can’t figure out how to fix it. Seems that rails somewhere inside throws :cache_path option away and the line
“options = view.controller.class.instance_variable_get(:@action_caches)”
get options without :cache_path.
What should I do get the desired behaviour?
Thank you.
You are right. When I circle back to this, I’ll work on a solution.
Todd´s last blog ..Why I like Curb
Cryo,
Okay, I figured out why the cache_path is lost. In the override method
caches_action_with_content_for
options = actions.last.dup
Needs to create a deep copy otherwise somewhere else those options are removed from the original Hash.