|
| 1 | +*********** |
| 2 | +Query Cache |
| 3 | +*********** |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. _query-cache: |
| 14 | + |
| 15 | +The MongoDB Ruby driver provides a built-in query cache. When enabled, the |
| 16 | +query cache saves the results of previously-executed find and aggregation |
| 17 | +queries. When those same queries are performed again, the driver returns |
| 18 | +the cached reuslts to prevent unnecessary roundtrips to the database. |
| 19 | + |
| 20 | +Usage |
| 21 | +===== |
| 22 | + |
| 23 | +The query cache is disabled by default. It cache can be enabled on the global |
| 24 | +scope as well as within the context of a specific block. |
| 25 | + |
| 26 | +To enable the query cache globally: |
| 27 | + |
| 28 | +.. code-block:: ruby |
| 29 | + |
| 30 | + Mongo::QueryCache.enabled = true |
| 31 | + |
| 32 | +Similarly, to disable it globally: |
| 33 | + |
| 34 | +.. code-block:: ruby |
| 35 | + |
| 36 | + Mongo::QueryCache.enabled = false |
| 37 | + |
| 38 | +To enable the query cache within the context of a block: |
| 39 | + |
| 40 | +.. code-block:: ruby |
| 41 | + |
| 42 | + Mongo::QueryCache.cache do |
| 43 | + Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music') do |client| |
| 44 | + client['artists'].find(name: 'Flying Lotus').first |
| 45 | + #=> Queries the database and caches the result |
| 46 | + |
| 47 | + client['artists'].find(name: 'Flying Lotus').first |
| 48 | + #=> Returns the previously cached result |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | +And to disable the query cache in the context of a block: |
| 53 | + |
| 54 | +.. code-block:: ruby |
| 55 | + |
| 56 | + Mongo::QueryCache.uncached do |
| 57 | + Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music') do |client| |
| 58 | + client['artists'].find(name: 'Flying Lotus').first |
| 59 | + #=> Sends the query to the database; does NOT cache the result |
| 60 | + |
| 61 | + client['artists'].find(name: 'Flying Lotus').first |
| 62 | + #=> Queries the database again |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | +You may check whether the query cache is enabled at any time by calling |
| 67 | +``Mongo::QueryCache.enabled?``, which will return ``true`` or ``false``. |
| 68 | + |
| 69 | +.. _query-cache-matching: |
| 70 | + |
| 71 | +Query Matching |
| 72 | +============== |
| 73 | + |
| 74 | +A query is eligible to use cached results if it matches the original query |
| 75 | +that produced the cached results. Two queries are considered matching if they |
| 76 | +are identical in the following values: |
| 77 | + |
| 78 | +* Namespace (the database and collection on which the query was performed) |
| 79 | +* Selector (for aggregations, the aggregation pipeline stages) |
| 80 | +* Skip |
| 81 | +* Sort |
| 82 | +* Projection |
| 83 | +* Collation |
| 84 | +* Read Concern |
| 85 | +* Read Preference |
| 86 | + |
| 87 | +For example, if you perform one query, and then perform a mostly identical query |
| 88 | +with a different sort order, those queries will not be considered matching, |
| 89 | +and the second query will not use the cached results of the first. |
| 90 | + |
| 91 | +Limits |
| 92 | +====== |
| 93 | + |
| 94 | +When performing a query with a limit, the query cache will reuse an existing |
| 95 | +cached query with a larger limit if one exists. For example: |
| 96 | + |
| 97 | +.. code-block:: ruby |
| 98 | + |
| 99 | + Mongo::QueryCache.cache do |
| 100 | + Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music') do |client| |
| 101 | + client['artists'].find(genre: 'Rock', limit: 10) |
| 102 | + #=> Queries the database and caches the result |
| 103 | + |
| 104 | + client['artists'].find(genre: 'Rock', limit: 5) |
| 105 | + #=> Returns the first 5 results from the cached query |
| 106 | + |
| 107 | + client['artists'].find(genre: 'Rock', limit: 20) |
| 108 | + #=> Queries the database again and replaces the previously cached query results |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | +Cache Invalidation |
| 113 | +================== |
| 114 | + |
| 115 | +The query cache is cleared in part or in full on every write operation. Most |
| 116 | +write operations will clear the results of any queries were performed on the same |
| 117 | +collection that is being written to. Some operations will clear the entire |
| 118 | +query cache. |
| 119 | + |
| 120 | +The following operations will clear cached query results on the same database and |
| 121 | +collection (including during bulk writes): |
| 122 | + |
| 123 | +* ``insert_one`` |
| 124 | +* ``update_one`` |
| 125 | +* ``replace_one`` |
| 126 | +* ``update_many`` |
| 127 | +* ``delete_one`` |
| 128 | +* ``delete_many`` |
| 129 | +* ``find_one_and_delete`` |
| 130 | +* ``find_one_and_update`` |
| 131 | +* ``find_one_and_replace`` |
| 132 | + |
| 133 | +The following operations will clear the entire query cache: |
| 134 | + |
| 135 | +* aggregation with ``$merge`` or ``$out`` pipeline stages |
| 136 | +* ``commit_transaction`` |
| 137 | +* ``abort_transaction`` |
| 138 | + |
| 139 | +Manual Cache Invalidation |
| 140 | +========================= |
| 141 | + |
| 142 | +You may clear the query cache at any time with the following method: |
| 143 | + |
| 144 | +.. code-block:: ruby |
| 145 | + |
| 146 | + Mongo::QueryCache.clear |
| 147 | + |
| 148 | +This will remove all cached query results. |
| 149 | + |
| 150 | +Transactions |
| 151 | +============ |
| 152 | + |
| 153 | +Queries are cached within the context of a transaction, but the entire |
| 154 | +cache will be cleared when the transaction is committed or aborted. |
| 155 | + |
| 156 | +.. code-block:: ruby |
| 157 | + |
| 158 | + Mongo::QueryCache.cache do |
| 159 | + Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music') do |client| |
| 160 | + session = client.start_session |
| 161 | + |
| 162 | + session.with_transaction do |
| 163 | + client['artists'].insert_one({ name: 'Fleet Foxes' }, session: session) |
| 164 | + |
| 165 | + client['artists'].find({}, session: session).first |
| 166 | + #=> { name: 'Fleet Foxes' } |
| 167 | + #=> Queries the database and caches the result |
| 168 | + |
| 169 | + client['artists'].find({}, session: session).first |
| 170 | + #=> { name: 'Fleet Foxes' } |
| 171 | + #=> Returns the previously cached result |
| 172 | + |
| 173 | + session.abort_transaction |
| 174 | + end |
| 175 | + |
| 176 | + client['artists'].find.first |
| 177 | + #=> nil |
| 178 | + # The query cache was cleared on abort_transaction |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | +.. note:: |
| 183 | + |
| 184 | + Transactions are often performed with a "snapshot" read concern level. Keep |
| 185 | + in mind that a query with a "snapshot" read concern cannot return cached |
| 186 | + results from a query without the "snapshot" read concern, so it is possible |
| 187 | + that a transaction may not use previously cached queries. |
| 188 | + |
| 189 | + To understand when a query will use a cached result, see the |
| 190 | + :ref:`Query Matching <query-cache-matching>` section. |
| 191 | + |
| 192 | +Aggregations |
| 193 | +============ |
| 194 | + |
| 195 | +The query cache also caches the results of aggregation pipelines. For example: |
| 196 | + |
| 197 | +.. code-block:: ruby |
| 198 | + |
| 199 | + Mongo::QueryCache.cache do |
| 200 | + Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music') do |client| |
| 201 | + client['artists'].aggregate([ { '$match' => { name: 'Fleet Foxes' } } ]).first |
| 202 | + #=> Queries the database and caches the result |
| 203 | + |
| 204 | + client['artists'].aggregate([ { '$match' => { name: 'Fleet Foxes' } } ]).first |
| 205 | + #=> Returns the previously cached result |
| 206 | + end |
| 207 | + end |
| 208 | + |
| 209 | +.. note:: |
| 210 | + |
| 211 | + Aggregation results are cleared from the cache during every write operation, |
| 212 | + with no exceptions. |
| 213 | + |
0 commit comments