Module: Mongoid::QueryCache
- Defined in:
- 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
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.
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mongoid/query_cache.rb', line 85 def cache(&block) if defined?(Mongo::QueryCache) Mongo::QueryCache.cache(&block) else 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.
21 22 23 24 25 26 27 |
# File 'lib/mongoid/query_cache.rb', line 21 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.
37 38 39 40 41 42 43 |
# File 'lib/mongoid/query_cache.rb', line 37 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.
53 54 55 56 57 58 59 |
# File 'lib/mongoid/query_cache.rb', line 53 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?
69 70 71 72 73 74 75 |
# File 'lib/mongoid/query_cache.rb', line 69 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.
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/mongoid/query_cache.rb', line 105 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 |