Skip to content

Commit bd14813

Browse files
MONGOID-5597 Improve documentation for Query Cache (#5598)
* Mongoid 9.0: Remove Mongoid Query Cache, replace with Mongo * Fixes * Update mongoid-9.0.txt * Update mongoid-9.0.txt
1 parent c740212 commit bd14813

File tree

3 files changed

+46
-66
lines changed

3 files changed

+46
-66
lines changed

source/reference/configuration.txt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -886,13 +886,10 @@ before the workers are forked.
886886
Query Cache Middleware
887887
======================
888888

889-
.. note::
890-
891-
When used with Ruby driver version 2.15 or newer, Mongoid's Query Cache
892-
Middleware delegates to `the driver's Query Cache Middleware
893-
<https://www.mongodb.com/docs/ruby-driver/current/reference/query-cache/index.html#query-cache-middleware>`_.
889+
Enabling Query Cache for Rack Web Requests
890+
------------------------------------------
894891

895-
Mongoid provides a Rack middleware which enables the :ref:`Query Cache
892+
The MongoDB Ruby Driver provides a Rack middleware which enables the :ref:`Query Cache
896893
<query-cache>` for the duration of each web request. Below is an example of
897894
how to enable the Query Cache Middleware in a Ruby on Rails application:
898895

@@ -908,6 +905,29 @@ Please refer to the `Rails on Rack guide
908905
<https://guides.rubyonrails.org/rails_on_rack.html#configuring-middleware-stack>`_
909906
for more information about using Rack middleware in Rails applications.
910907

908+
Enabling Query Cache for ActiveJob
909+
----------------------------------
910+
911+
The MongoDB Ruby Driver also provides Query Cache middleware for ActiveJob.
912+
You may enable it for all jobs in an initializer:
913+
914+
.. code-block:: ruby
915+
916+
# config/initializers/active_job.rb
917+
918+
# Enable Mongo driver query cache for ActiveJob
919+
ActiveSupport.on_load(:active_job) do
920+
include Mongo::QueryCache::Middleware::ActiveJob
921+
end
922+
923+
Or for a specific job class:
924+
925+
.. code-block:: ruby
926+
927+
class MyJob < ActiveJob::Base
928+
include Mongo::QueryCache::Middleware::ActiveJob
929+
end
930+
911931

912932
Development Configuration
913933
=========================

source/reference/queries.txt

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,19 +2344,17 @@ The query cache may be enabled by using the driver's namespace or Mongoid's
23442344
namespace.
23452345

23462346

2347-
.. _enabling-query-cache-via-middleware:
2347+
.. _enabling-query-cache-automatically:
23482348

2349-
Enabling Query Cache Via Middleware
2350-
-----------------------------------
2349+
Enabling Query Cache Automatically
2350+
----------------------------------
23512351

2352-
Applications that utilize a Rack-based web framework (Rails, etc.) can
2353-
take advantage of the Rack middleware provided by Mongoid to automatically
2354-
enable the query cache for each web request, and clear the query cache after
2355-
each request completes. Please see the :ref:`Query Cache Rack Middleware
2352+
The MongoDB Ruby Driver provides middleware to automatically enable the query cache for
2353+
Rack web requests and ActiveJob job runs. Please see the :ref:`Query Cache Rack Middleware
23562354
<query-cache-middleware>` section on the configuration page for instructions.
23572355

2358-
Note that the Query Cache Middleware does not apply to code executed
2359-
outside web requests.
2356+
Note that the Query Cache Middleware does not apply to code executed outside web requests
2357+
and/or jobs.
23602358

23612359

23622360
.. _enabling-query-cache-manually:
@@ -2377,50 +2375,11 @@ recommend to use the block form described above:
23772375

23782376
.. code-block:: ruby
23792377

2380-
Mongo::QueryCache.enabled = true
2381-
2382-
# ...
2383-
2384-
Mongo::QueryCache.enabled = false
2385-
2386-
Alternatively, you can reference the driver's query cache module directly:
2387-
2388-
.. code-block:: ruby
2389-
2390-
Mongo::QueryCache.cache do
2378+
begin
2379+
Mongo::QueryCache.enabled = true
23912380
# ...
2392-
end
2393-
2394-
Mongo::QueryCache.enabled = true
2395-
2396-
Mongo::QueryCache.enabled = true
2397-
2398-
2399-
Query Cache Shared With Driver
2400-
------------------------------
2401-
2402-
When Mongoid delegates to the driver's query cache, the same cache is used
2403-
by both Mongoid and the driver. For example, the following two queries
2404-
are implemented via the same server query and only the first of them
2405-
would be sent to the server:
2406-
2407-
.. code-block:: ruby
2408-
2409-
Mongo::QueryCache.cache do
2410-
Band.first
2411-
Band.collection.find.sort(_id: 1).first
2412-
end
2413-
2414-
Similarly, write operations that clear the cache would do so when the read
2415-
is performed by the driver and write by Mongoid, or vice versa:
2416-
2417-
.. code-block:: ruby
2418-
2419-
Mongo::QueryCache.cache do
2420-
Band.first
2421-
Band.collection.insert_one({})
2422-
# Queries again
2423-
Band.first
2381+
ensure
2382+
Mongo::QueryCache.enabled = false
24242383
end
24252384

24262385

source/release-notes/mongoid-9.0.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ please consult GitHub releases for detailed release notes and JIRA for
1818
the complete list of issues fixed in each release, including bug fixes.
1919

2020

21-
``Mongoid::QueryCache`` removed
22-
-------------------------------
23-
24-
The ``Mongoid::QueryCache`` module has been deprecated in Mongoid 8.1.
25-
It is removed in Mongoid 9.0. Please use the query cache provided by the MongoDB Ruby driver
26-
instead. Please review the `driver query cache documentation
27-
<https://mongodb.com/docs/ruby-driver/current/reference/query-cache/>`_ for more details.
28-
2921
Deprecated options removed
3022
--------------------------
3123

@@ -52,6 +44,15 @@ In addition, support for ``config.load_defaults`` versions 7.5 and
5244
prior has been dropped (you must use a minimum of version 8.0.)
5345

5446

47+
Deprecated functionality removed
48+
--------------------------------
49+
50+
- The ``Mongoid::QueryCache`` module has been removed. Please replace any usages 1-for-1 with ``Mongo::QueryCache``.
51+
The method ``Mongoid::QueryCache#clear_cache`` should be replaced with ``Mongo::QueryCache#clear``.
52+
All other methods and submodules are identically named. Refer to the `driver query cache documentation
53+
<https://mongodb.com/docs/ruby-driver/current/reference/query-cache/>`_ for more details.
54+
55+
5556
``touch`` method now clears changed state
5657
-----------------------------------------
5758

0 commit comments

Comments
 (0)