Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails action caching with querystring parameters

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!

like image 289
xpepermint Avatar asked Jan 01 '10 13:01

xpepermint


Video Answer


3 Answers

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_') } } 
like image 127
chrisrbailey Avatar answered Oct 13 '22 11:10

chrisrbailey


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

caches_action :index, :cache_path => Proc.new {|c| c.request.url } 
like image 24
Alberto F. Capel Avatar answered Oct 13 '22 13:10

Alberto F. Capel


In this case you should use fragments caching:

in your controller:

cache(params[:max_price], :expires_in => 10.minute) do
  # get the result
end
like image 32
KARASZI István Avatar answered Oct 13 '22 11:10

KARASZI István