Query caching in active record


It seems that Active record query caching is all done in memory. This is fine untill you need to run with unicorn that will have multiple workers each with their own connection. It would be nice to be able to configure a query caching backend in the same way active support configures caching in other parts of rails thus allowing for a shared cache between workers. Is this functionally worth investigating? Have I missed something in the source code that already does this?


I don’t think that would work well for the purpose that query caching serves.

Query caching takes advantage of the fact that Rails is for web apps, which operate in request-response cycles. Each request starts with a clean cache. The query cache is there so that within the scope of handling a single request, if the code makes the same call multiple times (without an intervening write), it will get the same answer without having to go to the database again.

If the query cache were to go across handlers, it would be impossible to know when the cache should be cleared. Clearing it when one handler is done would cause it to be cleared right in the middle of another handler. Then it wouldn’t serve the purpose for which it was designed.

Brian Morearty

I thought query caching is per-request? (thread?) Is there a scenario where sharing the query cache is appropriate?