Module: Mongoid::QueryCache
- Defined in:
- build/mongoid-7.3/lib/mongoid/query_cache.rb
Overview
A cache of database queries on a per-request basis.
Defined Under Namespace
Modules: Base, Collection, Document, View Classes: CachedCursor, Middleware
Constant Summary collapse
- LEGACY_WARNING =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
<<~DOC You are using the legacy Mongoid query cache which has known issues. Please upgrade the `mongo' gem to at least 2.14.0 to use the improved driver query cache. Refer to: https://docs.mongodb.com/mongoid/current/tutorials/mongoid-queries/#the-improved-driver-query-cache DOC
Class Method Summary collapse
-
.cache(&block) ⇒ Object
Execute the block while using the query cache.
-
.cache_table ⇒ Hash
private
Get the cached queries.
-
.clear_cache ⇒ nil
Clear the query cache.
-
.enabled=(value) ⇒ Object
Set whether the cache is enabled.
-
.enabled? ⇒ true, false
Is the query cache enabled on the current thread?.
-
.uncached(&block) ⇒ Object
Execute the block with the query cache disabled.
Class Method Details
.cache(&block) ⇒ Object
Execute the block while using the query cache.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'build/mongoid-7.3/lib/mongoid/query_cache.rb', line 91 def cache(&block) if defined?(Mongo::QueryCache) Mongo::QueryCache.cache(&block) else @legacy_query_cache_warned ||= begin Mongoid.logger.warn(LEGACY_WARNING) true end enabled = QueryCache.enabled? QueryCache.enabled = true begin yield ensure QueryCache.enabled = enabled end end end |
.cache_table ⇒ Hash
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.
Get the cached queries.
27 28 29 30 31 32 33 |
# File 'build/mongoid-7.3/lib/mongoid/query_cache.rb', line 27 def cache_table if defined?(Mongo::QueryCache) raise NotImplementedError, "Mongoid does not expose driver's query cache table" else Thread.current["[mongoid]:query_cache"] ||= {} end end |
.clear_cache ⇒ nil
Clear the query cache.
43 44 45 46 47 48 49 |
# File 'build/mongoid-7.3/lib/mongoid/query_cache.rb', line 43 def clear_cache if defined?(Mongo::QueryCache) Mongo::QueryCache.clear else Thread.current["[mongoid]:query_cache"] = nil end end |
.enabled=(value) ⇒ Object
Set whether the cache is enabled.
59 60 61 62 63 64 65 |
# File 'build/mongoid-7.3/lib/mongoid/query_cache.rb', line 59 def enabled=(value) if defined?(Mongo::QueryCache) Mongo::QueryCache.enabled = value else Thread.current["[mongoid]:query_cache:enabled"] = value end end |
.enabled? ⇒ true, false
Is the query cache enabled on the current thread?
75 76 77 78 79 80 81 |
# File 'build/mongoid-7.3/lib/mongoid/query_cache.rb', line 75 def enabled? if defined?(Mongo::QueryCache) Mongo::QueryCache.enabled? else !!Thread.current["[mongoid]:query_cache:enabled"] end end |
.uncached(&block) ⇒ Object
Execute the block with the query cache disabled.
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'build/mongoid-7.3/lib/mongoid/query_cache.rb', line 115 def uncached(&block) if defined?(Mongo::QueryCache) Mongo::QueryCache.uncached(&block) else enabled = QueryCache.enabled? QueryCache.enabled = false begin yield ensure QueryCache.enabled = enabled end end end |