Learn the "why" behind slow queries and how to fix them in our 2-Part Webinar.
Register now >
Docs Menu
Docs Home
/ /

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.

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.

Note

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

The Ruby driver provides a Rack middleware which enables the query cache during each web request. The following code demonstrates how to enable the Query Cache Middleware in a Ruby on Rails application:

config/application.rb
# Add Mongo::QueryCache::Middleware at the bottom of the middleware
# stack or before other middleware that queries MongoDB.
config.middleware.use Mongo::QueryCache::Middleware

To learn more about using Rack middleware in Rails applications, see Configuring Middleware Stack in the Rails documentation.

The Ruby driver provides Query Cache Middleware for Active Job. You can enable it for all jobs in an initializer, as shown in the following code:

config/initializers/active_job.rb
# Enable Mongo driver query cache for Active Job
ActiveSupport.on_load(:active_job) do
include Mongo::QueryCache::Middleware::ActiveJob
end

You can also enable it for a specific job class, as shown in the following code:

class MyJob < ActiveJob::Base
include Mongo::QueryCache::Middleware::ActiveJob
end

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.

Back

Persist Data from Queries

On this page