Rails action caching with querystring parameters

前端 未结 3 1272
滥情空心
滥情空心 2020-11-30 19:20

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

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

T

相关标签:
3条回答
  • 2020-11-30 19:52

    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_') } }
    
    0 讨论(0)
  • 2020-11-30 20:00

    In this case you should use fragments caching:

    in your controller:

    cache(params[:max_price], :expires_in => 10.minute) do
      # get the result
    end
    
    0 讨论(0)
  • 2020-11-30 20:17

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

    caches_action :index, :cache_path => Proc.new {|c| c.request.url }
    
    0 讨论(0)
提交回复
热议问题