module Mongoid::QueryCache
A cache of database queries on a per-request basis.
@since 4.0.0
Constants
- LEGACY_WARNING
@api private
- Middleware
Public Class Methods
Execute the block while using the query cache.
@example Execute with the cache.
QueryCache.cache { collection.find }
@return [ Object
] The result of the block.
@since 4.0.0
# File 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
Get the cached queries.
@example Get the cached queries from the current thread.
QueryCache.cache_table
@return [ Hash ] The hash of cached queries.
@since 4.0.0 @api private
# File 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 the query cache.
@example Clear the cache.
QueryCache.clear_cache
@return [ nil ] Always nil.
@since 4.0.0
# File 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
Set whether the cache is enabled.
@example Set if the cache is enabled.
QueryCache.enabled = true
@param [ true, false ] value The enabled value.
@since 4.0.0
# File 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
Is the query cache enabled on the current thread?
@example Is the query cache enabled?
QueryCache.enabled?
@return [ true, false ] If the cache is enabled.
@since 4.0.0
# File 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
Execute the block with the query cache disabled.
@example Execute without the cache.
QueryCache.uncached { collection.find }
@return [ Object
] The result of the block.
# File 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