module Mongoid::QueryCache
A cache of database queries on a per-request basis.
@since 4.0.0
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 68 def cache enabled = QueryCache.enabled? QueryCache.enabled = true yield ensure QueryCache.enabled = enabled 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
# File lib/mongoid/query_cache.rb, line 20 def cache_table Thread.current["[mongoid]:query_cache"] ||= {} 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 32 def clear_cache Thread.current["[mongoid]:query_cache"] = nil 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 44 def enabled=(value) Thread.current["[mongoid]:query_cache:enabled"] = value 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 56 def enabled? !!Thread.current["[mongoid]:query_cache:enabled"] 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 82 def uncached enabled = QueryCache.enabled? QueryCache.enabled = false yield ensure QueryCache.enabled = enabled end