Class: ObjectCache::MemoryStore

Inherits:
CacheStore show all
Defined in:
lib/object_cache/memory_store.rb

Constant Summary collapse

@@cache =
{}
@@hash =
{}

Class Method Summary collapse

Methods inherited from CacheStore

array_or_value, fetch_fresh_results, fetch_from_array

Class Method Details

.fetch(klass, ids) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/object_cache/memory_store.rb', line 8

def self.fetch(klass, ids)
  results = []
  to_set = []
  [*ids].each do |id|
    if @@cache[klass] && @@cache[klass][id] && (Time.now - @@cache[klass][id].last) < ObjectCache.ttl
      results << get(klass, id)
    else
      to_set << id
    end
  end

  fresh_results = fetch_fresh_results(klass, to_set)
  results += fresh_results if fresh_results
  results.compact!

  # if we're asking for one object, we want it,
  # if asking for array, we expect array of objects back
  if ids.class == Array
    results
  else
    results.first
  end
end

.get(klass, id) ⇒ Object



32
33
34
# File 'lib/object_cache/memory_store.rb', line 32

def self.get(klass, id)
  @@cache[klass][id].first
end

.hash_empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/object_cache/memory_store.rb', line 42

def self.hash_empty?
  @@hash.empty?
end

.hash_get(key) ⇒ Object



46
47
48
# File 'lib/object_cache/memory_store.rb', line 46

def self.hash_get(key)
  @@hash[key]
end

.hash_set(key, value) ⇒ Object



50
51
52
# File 'lib/object_cache/memory_store.rb', line 50

def self.hash_set(key, value)
  @@hash[key] = value
end

.set(klass, id, item) ⇒ Object



36
37
38
39
40
# File 'lib/object_cache/memory_store.rb', line 36

def self.set(klass, id, item)
  @@cache[klass] ||= {}
  @@cache[klass][id] = [item, Time.now]
  @@cache[klass][id].first
end