Rails action caching with querystring parameters
How can I cache my REST controller wi开发者_开发技巧th Rails where my actions have query string parameters?
Example: GET /products/all.xml?max_price=200
Thx!
If you want to cache an action, based on all the query parameters (or say on nearly all of them), you can do:
caches_action :my_action, :cache_path => Proc.new { |c| c.params }
Or, maybe you want all but some params that you just use for Analytics (but that have no bearing on the records you're fetching):
caches_action :my_action, :cache_path => Proc.new { |c| c.params.delete_if { |k,v| k.starts_with?('utm_') } }
To use the request url as cache key I do something like this:
caches_action :index, :cache_path => Proc.new {|c| c.request.url }
In this case you should use fragments caching:
in your controller:
cache(params[:max_price], :expires_in => 10.minute) do
# get the result
end
精彩评论