Skip to content

Commit cd229bc

Browse files
gjchong25Grace Chong
andauthored
Ruby-2339 Write Driver Query Cache documentation (#2029)
* RUBY-2339 query cache documentation * RUBY-2339 fix wording * RUBY-2339 fix title * RUBY-2339 style * RUBY-2339 formatting * RUBY-2339 PR comment fixes * RUBY-2339 style * RUBY-2339 fix grammar and formatting Co-authored-by: Grace Chong <[email protected]>
1 parent d152ab6 commit cd229bc

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

source/tutorials/ruby-driver-crud-operations.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,44 @@ object, or with ``Decimal128.from_string()``.
130130
price = BSON::Decimal128.from_string("428.79")
131131
# => BSON::Decimal128('428.79')
132132

133+
Query Cache
134+
===========
135+
136+
If the Ruby driver's query cache is enabled, it will cache find queries on
137+
the currently executed thread and avoid sending requests to the database for
138+
identical queries. The QueryCache class stores CachingCursor objects which attempt
139+
to load documents from memory before retrieving them from the database. Performing
140+
any write operations such as insert, update, or delete clears the query cache. Note that
141+
if the number of results is too large to be returned in a single batch,
142+
the query cache will not be used, even if ``Mongo::QueryCache.enabled`` is true, and
143+
an error will be returned.
144+
145+
Similar to Mongoid's query cache, the driver's query cache implementation
146+
does not support using CachingCursors for queries with different limit sizes if
147+
the original query has a specified limit. For example, if a query with a limit of
148+
100 were executed, followed by a query with a limit of 10, the second query
149+
would still run against the database and a new CachingCursor object would be stored,
150+
rather than using the existing one. However, if a query does not specify a limit
151+
initially, then any query that is run after it with a specified limit will use
152+
the original CachingCursor rather than going back to the database, and will return
153+
the correct number of documents accordingly.
154+
155+
To enable the query cache on a global scope:
156+
157+
.. code-block:: ruby
158+
159+
Mongo::QueryCache.enabled = true
160+
161+
The following code shows how to enable the query cache within the context of a
162+
specific block:
163+
164+
.. code-block:: ruby
165+
166+
Mongo::QueryCache.cache do
167+
# all queries within this block are cached
168+
end
169+
170+
133171
Reading
134172
=======
135173

0 commit comments

Comments
 (0)