Class: Mongocore::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/mongocore/cache.rb

Overview

# # # # # # # 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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(q) ⇒ Cache

Init



16
17
18
19
20
# File 'lib/mongocore/cache.rb', line 16

def initialize(q)
  @query = q
  @cache = (RequestStore[:cache] ||= {})
  @key = Digest::MD5.hexdigest(@query.key)
end

Instance Attribute Details

#cacheObject

Accessors



13
14
15
# File 'lib/mongocore/cache.rb', line 13

def cache
  @cache
end

#keyObject

Accessors



13
14
15
# File 'lib/mongocore/cache.rb', line 13

def key
  @key
end

#queryObject

Accessors



13
14
15
# File 'lib/mongocore/cache.rb', line 13

def query
  @query
end

#typeObject

Accessors



13
14
15
# File 'lib/mongocore/cache.rb', line 13

def type
  @type
end

Instance Method Details

#get(t) ⇒ Object

Get the cache key



23
24
25
# 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) ⇒ Object

Set the cache key



28
29
30
# 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