简体   繁体   English

可以在Rails 3.2中基于每个请求禁用机架缓存吗?

[英]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 ). 我一直试图在Heroku上使用Rails 3.2进行流式传输 (请参阅我的帖子: Rails 3.2流式传输 )。

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. 在production.rb中使用config.middleware.delete(Rack::Cache)禁用它似乎可以解决它。 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. 编辑:我试图将标题设置为不缓存有问题的操作,但Rack :: Cache仍然导致流失败。 Totally disabling it is the only solution I have found so far. 完全禁用它是迄今为止我找到的唯一解决方案。

I ended up not needing to disable Rack-cache. 我最终不需要禁用Rack-cache。 Just needed to add this self.response.headers['Last-Modified'] = Time.now.ctime.to_s to my response. 只需要将self.response.headers['Last-Modified'] = Time.now.ctime.to_s到我的回复中。

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. 根据此处此处的源,如果设置了Cache-Control: no-cache标头或Pragma: no-cache标头,则Rack :: 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: 此外,您可以确保Rack :: Cache永远不会缓存给定操作的响应,例如:

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. 这将确保Rack :: Cache(以及任何其他上游代理)不会缓存响应,从而导致后端始终保持新鲜。

If this fails, then you're likely having issues due to the forward method in context.rb. 如果失败,那么由于context.rb中的forward方法,您可能会遇到问题。 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. 似乎没有办法绕过它,所以如果设置了某个标头,你可能想要修改Rack :: Cache来调用#call

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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