module Mongoid::QueryCache

A cache of database queries on a per-request basis.

@since 4.0.0

Public Class Methods

cache() { || ... } click to toggle source

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 66
def cache
  enabled = QueryCache.enabled?
  QueryCache.enabled = true
  yield
ensure
  QueryCache.enabled = enabled
end
cache_table() click to toggle source

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 18
def cache_table
  Thread.current["[mongoid]:query_cache"] ||= {}
end
clear_cache() click to toggle source

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 30
def clear_cache
  Thread.current["[mongoid]:query_cache"] = nil
end
enabled=(value) click to toggle source

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 42
def enabled=(value)
  Thread.current["[mongoid]:query_cache:enabled"] = value
end
enabled?() click to toggle source

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 54
def enabled?
  !!Thread.current["[mongoid]:query_cache:enabled"]
end
uncached() { || ... } click to toggle source

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 80
def uncached
  enabled = QueryCache.enabled?
  QueryCache.enabled = false
  yield
ensure
  QueryCache.enabled = enabled
end