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

Constant Summary collapse

LEGACY_WARNING =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

<<~DOC
  You are using the legacy Mongoid query cache which has known issues.
  Please upgrade the `mongo' gem to at least 2.14.0 to use the improved driver query cache.
  Refer to: https://docs.mongodb.com/mongoid/current/tutorials/mongoid-queries/#the-improved-driver-query-cache
DOC

Class Method Summary collapse

Class Method Details

.cache(&block) ⇒ Object

Execute the block while using the query cache.

Examples:

Execute with the cache.

QueryCache.cache { collection.find }

Returns:

  • (Object)

    The result of the block.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mongoid/query_cache.rb', line 78

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

.cache_tableHash

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.

Examples:

Get the cached queries from the current thread.

QueryCache.cache_table

Returns:

  • (Hash)

    The hash of cached queries.



22
23
24
25
26
27
28
# File 'lib/mongoid/query_cache.rb', line 22

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_cachenil

Clear the query cache.

Examples:

Clear the cache.

QueryCache.clear_cache

Returns:

  • (nil)

    Always nil.



36
37
38
39
40
41
42
# File 'lib/mongoid/query_cache.rb', line 36

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.

Examples:

Set if the cache is enabled.

QueryCache.enabled = true

Parameters:

  • value (true, false)

    The enabled value.



50
51
52
53
54
55
56
# File 'lib/mongoid/query_cache.rb', line 50

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?

Examples:

Is the query cache enabled?

QueryCache.enabled?

Returns:

  • (true, false)

    If the cache is enabled.



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

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.

Examples:

Execute without the cache.

QueryCache.uncached { collection.find }

Returns:

  • (Object)

    The result of the block.



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/mongoid/query_cache.rb', line 102

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