带有查询字符串参数的 Rails 动作缓存 [英] Rails action caching with querystring parameters

查看:21
本文介绍了带有查询字符串参数的 Rails 动作缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Rails 缓存我的 REST 控制器,其中我的操作具有查询字符串参数?

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

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

谢谢!

推荐答案

如果你想缓存一个动作,基于所有的查询参数(或者说几乎所有的参数),你可以这样做:

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 }

或者,您可能只需要一些仅用于 Analytics 的参数(但与您获取的记录无关):

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_') } }

这篇关于带有查询字符串参数的 Rails 动作缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆