Rails action caching gotcha

November 10, 2005 Link to post  Permalink

Rails provides a simple mechanism to allow the output from an action to be cached. Unlike the page caching, this allows all the filters to be executed, then the cached results returned. The filters executing is important for authentication.

This is great until you have an action that returns something with send_data (send_file probably has the same issue). Caching just doesn’t work in this case, and sends the wrong data to the client when the cache is hit.

Don’t use Action caching on actions that return binary data.

Update: I’ve written some code that fixes this problem, and documented it here

The main issue is that the custom headers set by my action that returned the binary data were being dropped by the Action Cache. My Action Cache Upgrade fixes that issue.