Module: Mongo::QueryCache
- Defined in:
- lib/mongo/query_cache.rb
Defined Under Namespace
Classes: Middleware
Class Method Summary collapse
-
.cache ⇒ Object
Execute the block while using the query cache.
-
.clear ⇒ nil
Clear the query cache.
-
.clear_namespace(namespace) ⇒ nil
private
Clear the section of the query cache storing cursors with results from this namespace.
-
.enabled=(value) ⇒ Object
Set whether the cache is enabled.
-
.enabled? ⇒ true, false
Is the query cache enabled on the current thread?.
-
.get(**opts) ⇒ Mongo::CachingCursor | nil
private
For the given query options, retrieve a cached cursor that can be used to obtain the correct query results, if one exists in the cache.
- .normalized_limit(limit) ⇒ Object
-
.set(cursor, **opts) ⇒ true
private
Store a CachingCursor instance in the query cache associated with the specified query options.
-
.uncached ⇒ Object
Execute the block with the query cache disabled.
Class Method Details
.cache ⇒ Object
Execute the block while using the query cache.
46 47 48 49 50 51 52 53 54 |
# File 'lib/mongo/query_cache.rb', line 46 def cache enabled = enabled? self.enabled = true begin yield ensure self.enabled = enabled end end |
.clear ⇒ nil
Clear the query cache.
88 89 90 |
# File 'lib/mongo/query_cache.rb', line 88 def clear Thread.current['[mongo]:query_cache'] = nil end |
.clear_namespace(namespace) ⇒ nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Clear the section of the query cache storing cursors with results from this namespace.
101 102 103 104 105 106 107 108 |
# File 'lib/mongo/query_cache.rb', line 101 def clear_namespace(namespace) cache_table.delete(namespace) # The nil key is where cursors are stored that could potentially read from # multiple collections. This key should be cleared on every write operation # to prevent returning stale data. cache_table.delete(nil) nil end |
.enabled=(value) ⇒ Object
Set whether the cache is enabled.
26 27 28 |
# File 'lib/mongo/query_cache.rb', line 26 def enabled=(value) Thread.current['[mongo]:query_cache:enabled'] = value end |
.enabled? ⇒ true, false
Is the query cache enabled on the current thread?
36 37 38 |
# File 'lib/mongo/query_cache.rb', line 36 def enabled? !!Thread.current['[mongo]:query_cache:enabled'] end |
.get(**opts) ⇒ Mongo::CachingCursor | nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
For the given query options, retrieve a cached cursor that can be used to obtain the correct query results, if one exists in the cache.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/mongo/query_cache.rb', line 179 def get(**opts) limit = normalized_limit(opts[:limit]) _namespace_key = namespace_key(**opts) _cache_key = cache_key(**opts) namespace_hash = cache_table[_namespace_key] return nil unless namespace_hash caching_cursor = namespace_hash[_cache_key] return nil unless caching_cursor caching_cursor_limit = normalized_limit(caching_cursor.view.limit) # There are two scenarios in which a caching cursor could fulfill the # query: # 1. The query has a limit, and the stored cursor has no limit or # a larger limit. # 2. The query has no limit and the stored cursor has no limit. # # Otherwise, return nil because the stored cursor will not satisfy # the query. if limit && (caching_cursor_limit.nil? || caching_cursor_limit >= limit) caching_cursor elsif limit.nil? && caching_cursor_limit.nil? caching_cursor end end |
.normalized_limit(limit) ⇒ Object
209 210 211 212 213 214 215 216 |
# File 'lib/mongo/query_cache.rb', line 209 def normalized_limit(limit) return nil unless limit # For the purposes of caching, a limit of 0 means no limit, as mongo treats it as such. return nil if limit == 0 # For the purposes of caching, a negative limit is the same as as a positive limit. limit.abs end |
.set(cursor, **opts) ⇒ true
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Store a CachingCursor instance in the query cache associated with the specified query options.
140 141 142 143 144 145 146 147 148 |
# File 'lib/mongo/query_cache.rb', line 140 def set(cursor, **opts) _cache_key = cache_key(**opts) _namespace_key = namespace_key(**opts) cache_table[_namespace_key] ||= {} cache_table[_namespace_key][_cache_key] = cursor true end |
.uncached ⇒ Object
Execute the block with the query cache disabled.
62 63 64 65 66 67 68 69 70 |
# File 'lib/mongo/query_cache.rb', line 62 def uncached enabled = enabled? self.enabled = false begin yield ensure self.enabled = enabled end end |