class Mongocore::Cache

# # # # # # # The Cache class keeps track of cache entries.

Every query is cached, used the state as the cache key. This is a very aggressive strategy, where arrays won't get updated on update or delete.

Attributes

cache[RW]

Accessors

key[RW]

Accessors

query[RW]

Accessors

type[RW]

Accessors

Public Class Methods

new(q) click to toggle source

Init

# File lib/mongocore/cache.rb, line 16
def initialize(q)
  @query = q
  @cache = (RequestStore[:cache] ||= {})
  @key = Digest::MD5.hexdigest(@query.key)
end

Public Instance Methods

get(t) click to toggle source

Get the cache key

# File lib/mongocore/cache.rb, line 23
def get(t)
  @cache[t = key + t.to_s].tap{|d| stat(d, t) if Mongocore.debug}
end
set(t, v = nil) click to toggle source

Set the cache key

# File lib/mongocore/cache.rb, line 28
def set(t, v = nil)
  t = key + t.to_s; v ? cache[t] = v : cache.delete(t)
end

Private Instance Methods

stat(d, t) click to toggle source

Stats for debug and cache

# File lib/mongocore/cache.rb, line 35
def stat(d, t)
  puts('Cache ' + (d ? 'Hit!' : 'Miss') + ': ' + t)
  RequestStore[d ? :h : :m] = (RequestStore[d ? :h : :m] || 0) + 1
end