@@ -130,6 +130,44 @@ object, or with ``Decimal128.from_string()``.
130
130
price = BSON::Decimal128.from_string("428.79")
131
131
# => BSON::Decimal128('428.79')
132
132
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
+
133
171
Reading
134
172
=======
135
173
0 commit comments