Class: Mongocore::Cache
- Inherits:
-
Object
- Object
- Mongocore::Cache
- 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
-
#cache ⇒ Object
Accessors.
-
#key ⇒ Object
Accessors.
-
#query ⇒ Object
Accessors.
-
#type ⇒ Object
Accessors.
Instance Method Summary collapse
-
#get(t) ⇒ Object
Get the cache key.
-
#initialize(q) ⇒ Cache
constructor
Init.
-
#set(t, v = nil) ⇒ Object
Set the cache key.
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
#cache ⇒ Object
Accessors
13 14 15 |
# File 'lib/mongocore/cache.rb', line 13 def cache @cache end |
#key ⇒ Object
Accessors
13 14 15 |
# File 'lib/mongocore/cache.rb', line 13 def key @key end |
#query ⇒ Object
Accessors
13 14 15 |
# File 'lib/mongocore/cache.rb', line 13 def query @query end |
#type ⇒ Object
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 |