Module: Mongoid::QueryCache

Defined in:
lib/mongoid/query_cache.rb

Overview

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

Since:

  • 4.0.0

Defined Under Namespace

Modules: Base, Collection, View Classes: CachedCursor, Middleware

Class Method Summary collapse

Class Method Details

.cacheObject

Execute the block while using the query cache.

Examples:

Execute with the cache.

QueryCache.cache { collection.find }

Returns:

  • (Object)

    The result of the block.

Since:

  • 4.0.0


66
67
68
69
70
71
72
# File 'lib/mongoid/query_cache.rb', line 66

def cache
  enabled = QueryCache.enabled?
  QueryCache.enabled = true
  yield
ensure
  QueryCache.enabled = enabled
end

.cache_tableHash

Get the cached queries.

Examples:

Get the cached queries from the current thread.

QueryCache.cache_table

Returns:

  • (Hash)

    The hash of cached queries.

Since:

  • 4.0.0


18
19
20
# File 'lib/mongoid/query_cache.rb', line 18

def cache_table
  Thread.current["[mongoid]:query_cache"] ||= {}
end

.clear_cachenil

Clear the query cache.

Examples:

Clear the cache.

QueryCache.clear_cache

Returns:

  • (nil)

    Always nil.

Since:

  • 4.0.0


30
31
32
# File 'lib/mongoid/query_cache.rb', line 30

def clear_cache
  Thread.current["[mongoid]:query_cache"] = nil
end

.enabled=(value) ⇒ Object

Set whether the cache is enabled.

Examples:

Set if the cache is enabled.

QueryCache.enabled = true

Parameters:

  • value (true, false)

    The enabled value.

Since:

  • 4.0.0


42
43
44
# File 'lib/mongoid/query_cache.rb', line 42

def enabled=(value)
  Thread.current["[mongoid]:query_cache:enabled"] = value
end

.enabled?true, false

Is the query cache enabled on the current thread?

Examples:

Is the query cache enabled?

QueryCache.enabled?

Returns:

  • (true, false)

    If the cache is enabled.

Since:

  • 4.0.0


54
55
56
# File 'lib/mongoid/query_cache.rb', line 54

def enabled?
  !!Thread.current["[mongoid]:query_cache:enabled"]
end