Rails3 - how to process a filter after request sent to browser
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 midd开发者_如何学Pythonleware 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!
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.
精彩评论