如何在我的操作具有查询字符串参数的情况下使用Rails缓存我的REST控制器?
Example: GET /products/all.xml?max_price=200
THX!
答案 0 :(得分:88)
如果要根据所有查询参数(或几乎所有查询参数)缓存操作,您可以执行以下操作:
caches_action :my_action, :cache_path => Proc.new { |c| c.params }
或者,也许您只想要一些仅用于分析的params(但与您正在获取的记录无关):
caches_action :my_action, :cache_path => Proc.new { |c| c.params.delete_if { |k,v| k.starts_with?('utm_') } }
答案 1 :(得分:10)
要将请求网址用作缓存密钥,我会执行以下操作:
caches_action :index, :cache_path => Proc.new {|c| c.request.url }
答案 2 :(得分:4)
在这种情况下,您应该使用片段缓存:
在您的控制器中:
cache(params[:max_price], :expires_in => 10.minute) do
# get the result
end