Class: Kashmir::Caching::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/kashmir/plugins/memory_caching.rb

Instance Method Summary collapse

Instance Method Details

#bulk_from_cache(definitions, instances) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/kashmir/plugins/memory_caching.rb', line 14

def bulk_from_cache(definitions, instances)
  keys = instances.map do |instance|
    presenter_key(definitions, instance) if instance.respond_to?(:id)
  end

  keys.map do |key|
    get(key)
  end
end

#bulk_write(definitions, representations, objects, ttl) ⇒ Object



29
30
31
32
33
# File 'lib/kashmir/plugins/memory_caching.rb', line 29

def bulk_write(definitions, representations, objects, ttl)
  objects.each_with_index do |instance, index|
    store_presenter(definitions, representations[index], instance, ttl)
  end
end

#clear(definition, instance) ⇒ Object



51
52
53
54
55
# File 'lib/kashmir/plugins/memory_caching.rb', line 51

def clear(definition, instance)
  key = presenter_key(definition, instance)
  @@cache ||= {}
  @@cache.delete(key)
end

#flush!Object



57
58
59
# File 'lib/kashmir/plugins/memory_caching.rb', line 57

def flush!
  @@cache = {}
end

#from_cache(definitions, instance) ⇒ Object



7
8
9
10
11
12
# File 'lib/kashmir/plugins/memory_caching.rb', line 7

def from_cache(definitions, instance)
  key = presenter_key(definitions, instance)
  if cached_data = get(key)
    return cached_data
  end
end

#get(key) ⇒ Object



39
40
41
42
43
44
# File 'lib/kashmir/plugins/memory_caching.rb', line 39

def get(key)
  @@cache ||= {}
  if data = @@cache[key]
    SymbolizeHelper.symbolize_recursive JSON.parse(data)
  end
end

#keysObject



61
62
63
# File 'lib/kashmir/plugins/memory_caching.rb', line 61

def keys
  @@cache.keys
end

#presenter_key(definition_name, instance) ⇒ Object



35
36
37
# File 'lib/kashmir/plugins/memory_caching.rb', line 35

def presenter_key(definition_name, instance)
  "presenter:#{instance.class}:#{instance.id}:#{definition_name}"
end

#set(key, value) ⇒ Object



46
47
48
49
# File 'lib/kashmir/plugins/memory_caching.rb', line 46

def set(key, value)
  @@cache ||= {}
  @@cache[key] = value.to_json
end

#store_presenter(definitions, representation, instance, ttl = 0) ⇒ Object



24
25
26
27
# File 'lib/kashmir/plugins/memory_caching.rb', line 24

def store_presenter(definitions, representation, instance, ttl=0)
  key = presenter_key(definitions, instance)
  set(key, representation)
end