Module: Mongoid::QueryCache

Defined in:
lib/mongoid/query_cache.rb,
lib/mongoid/query_cache/railtie.rb,
lib/mongoid/query_cache/version.rb

Overview

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

Defined Under Namespace

Modules: Base, Cacheable, Collection, Query Classes: CachedCursor, Middleware, Railtie

Constant Summary collapse

VERSION =
'0.0.1'

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.



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

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.



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.



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

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.



40
41
42
# File 'lib/mongoid/query_cache.rb', line 40

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.



51
52
53
# File 'lib/mongoid/query_cache.rb', line 51

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