Docs Menu

Query Cache

In this guide, you can learn about query caching. The query cache saves the results of previous find and aggregation queries and reuses them in the future. This prevents Mongoid from performing the queries again, increasing application performance and reducing the database load.

To learn more about this feature, see Query Cache in the Ruby driver documentation.

In this section, you can learn how to enable the query caching feature in your application. You can enable the query cache by using the driver's namespace or Mongoid's namespace.

The Ruby driver provides middleware to automatically enable the query cache for Rack web requests and Active Job job runs. To view instructions on automatically enabling the query cache, see the Enable Query Cache for Rack Web Requests section of the Query Cache Middleware Configuration guide.

Note

Query cache middleware does not apply to code run outside web requests or jobs.

To enable the query cache manually for a specific code segment, you can run your code within the following block:

Mongo::QueryCache.cache do
# Include code here ...
end

You can explicitly enable and disable the query cache, but we recommend using the block form in the preceding code example. The following code demonstrates how to enable and disable the query cache:

begin
Mongo::QueryCache.enabled = true
# Include code here
ensure
Mongo::QueryCache.enabled = false
end

Calling the first method on a model class uses an ascending sort on the _id field when returning the result. This might produce unexpected behavior if you enable query caching.

For example, if you call the all method on a model class before calling first, you might expect the first method to use the cached results from all. However, because Mongoid applies a sort to the second call, both methods query the database and separately cache results.

To use the cached results when calling the first method, call all.to_a.first on the model class, as shown in the following example code:

Band.all.to_a.first

In the preceding example, chaining the to_a method runs the query and converts the results into an array in memory. Then, the first method simply returns the first array entry instead of triggering another query and caching the results.

To learn more about creating filter criteria, see the Specify a Document Query guide.

To learn how to customize your persistence target, see the Persistence Configuration guide.