rails 3 caching: expire action for named route

前端 未结 3 820
北荒
北荒 2021-01-05 14:38

My controller has this:

caches_action :render_ticker_for_channel, :expires_in => 30.seconds

In my routes file I have this:



        
相关标签:
3条回答
  • 2021-01-05 14:59

    Use the regex version of expire_fragment:

    expire_fragment %r{render_c_t/#{c.id}/}
    
    0 讨论(0)
  • 2021-01-05 15:03
    caches_action :render_ticker_for_channel, :if => proc do 
      !!params['doCache']
    end
    

    But for this solution to work we need to pass an extra param either through query string or post body.

    0 讨论(0)
  • 2021-01-05 15:20

    There should definitely be a more "Rails Way" to do this, but this might work as a back door: Rails.application.routes.url_helpers will give you access to your helpers and each will return a string that's the path and/or url that would be sent to the browser in a Location header.

    Try Rails.application.routes.url_helpers.render_ticker_for_channel_path(63) which should return /render_c_t/63 and Rails.application.routes.url_helpers.render_ticker_for_channel(63, :host => 'mcr3.dev') which should return http://mcr3.dev/render_c_t/63

    With some manipulation you could parse apart that second string to get back to the name that Rails is using for the cached action:

    def funky_action_cache_name(route, params)
      Rails.application.routes.url_helpers.send(route.to_s+'_url', params).gsub(/https?:\/\//,'')
    end
    
    # expire_action(funky_action_cache_name(:render_ticker_for_channel, :id => 63))
    

    Not the most beautiful solution, but should work!

    0 讨论(0)
提交回复
热议问题