views:

716

answers:

4

How can I cache my REST controller with Rails where my actions have query string parameters?

Example: GET /products/all.xml?max_price=200

Thx!

+1  A: 

if you want to cache on client side, here are some useful links:

just send the proper HTTP headers and the client will cache the response

KARASZI István
I'm looking for the server side caching like page/action/fragment caching.
xpepermint
+1  A: 

In this case you should use fragments caching:

in your controller:

cache(params[:max_price], :expires_in => 10.minute) do
  # get the result
end
KARASZI István
+4  A: 

I just blogged about this (see that for longer explanation), but 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_') } }
chrisrbailey
+1  A: 

To use the request url as cache key I do something like this:

caches_action :index, :cache_path => Proc.new {|c| c.request.url }
Alberto F. Capel