简体   繁体   中英

Possible to disable rack-cache on a per-request basis in Rails 3.2?

I have been attempting to get streaming working in Rails 3.2 on Heroku (see my SO post here: Rails 3.2 streaming ).

I am coming to the conclusion that rack-cache is causing the problem. Disabling it using config.middleware.delete(Rack::Cache) in production.rb seems to fix it. This, obviously, disables it for my entire app.

I only want it disabled for the one streaming request (which is on the admin side and will be used infrequently). Is this possible? It'd be a major bummer to lose caching for the sake of one small (but required) admin feature.

Thanks very much!!!

Edit: I have attempted setting the headers to not cache the action in question, but Rack::Cache is still causing the streaming to fail. Totally disabling it is the only solution I have found so far.

I ended up not needing to disable Rack-cache. Just needed to add this self.response.headers['Last-Modified'] = Time.now.ctime.to_s to my response.

While you can't disable it, you might not need to; you may just need to bypass the caching mechanism.

Per the source here and here , if the Cache-Control: no-cache header or the Pragma: no-cache headers are set, Rack::Cache won't attempt to pull a request from the cache. That doesn't disable it, but it does let you ensure that you don't have a request that shouldn't be cached end up returning a caching response.

Additionally, you can ensure that Rack::Cache never caches a response for a given action with something like:

response.headers['Cache-Control'] = 'private,max-age=0,must-revalidate,no-store'

in your controller action. This will ensure that Rack::Cache (and any other upstream proxies) don't cache the response, resulting in an always-fresh hit to your backend.

If this fails, then you're likely having issues due to the forward method in context.rb. There doesn't seem to be a way to bypass it, so you'd probably want to patch Rack::Cache to just invoke #call if a certain header is set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM