Rails3 - 如何在请求发送到浏览器后处理过滤器
我正在对请求进行一些自定义缓存,并且我想在响应已发送到服务器后执行缓慢的保存到磁盘步骤。
标准的 after_filter 在响应发送到客户端之前运行。
根据 Rails 系统的工作方式,这也许是不可能的,因为它仅在完全处理完请求后才发送回数据。
从我发现的一些较旧的问题来看,似乎 Rack 中间件可能是答案,但没有一个示例看起来远程相关,例如: https://github.com/rack/rack/wiki/List-of-Middleware
我还需要访问 @response 和 params 对象,但我认为那是可能的。
谢谢!
I'm doing some custom caching of requests, and I'd like to do the slow saving-to-disk step after the response has already been sent to the server.
A standard after_filter runs before the response is sent to the client still.
Depending upon how the rails system works, perhaps this is imposible because it only sends back data when it's completely done processing the request.
From some older questions I've found, it seems a Rack middleware might be the answer, but none of the examples seem remotely related, like: https://github.com/rack/rack/wiki/List-of-Middleware
I'd also need access to the @response and params objects, but I think thats possible.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法理解你的目标。
如果缓存是一个透明地存储数据的组件,以便可以更快地提供未来对该数据的请求,那么您应该担心如何尽快使缓存的内容可供未来的请求使用,即使它意味着要求第一个请求等待一些毫秒才能发送其响应。
如果在您的上下文中认为写入磁盘太慢,您可以尝试使用 Rails 支持的其他缓存后端之一。
您还可以使用 Resque 或 delayed_job;该作业将负责创建缓存并将与响应异步。但同样,缓存的目的将与缓存的内容一起被错过。
There is something I can't understand about your goal.
If a cache is a component that transparently stores data so that future requests for that data can be served faster, your should worry to make the cached content available to the future requests as soon as possible, even if it means asking for the first request to wait some ms for its response to be sent.
If writing to disk is considered too slow in your context, you can try to engage one of the other caching backends Rails supports.
You could also add a background job with Resque or delayed_job; that job will take care of creating you cache and will be asynchronous to the response. But again the purpose of caching will be missed along with the cached content.