Set caches_action, :expires_in => dynamically

2.7k Views Asked by At

Every example of caches_action I've seen looks like:

caches_action, :expires_in => 5.minutes

but I'd like to set expires_in based on the expiration time of an object used in the action. Is there any way to reference that object when setting expires_in since the object in question is based on params sent to the action?

I tried using

caches_action, :expires_in => Object.find(params[:id])

but alas it won't let me reference params there. Suggestions welcome!

2

There are 2 best solutions below

0
On BEST ANSWER

Looks like you need to use a Proc which can get passed the params of the controller. Here is an example

caches_action :show, cache_path: Proc.new {|controller_instance| "some_cache_path_that_is_uniq/#{controller_instance.params[:id]}"}
1
On

The main point of action caching is to prevent actions being run every time a new request is coming in. Unless defined explicitly params are not taken into consideration.

If you want to cache based on instances I would suggest doing Conditional Gets.