Class: Kashmir::Caching::Memcached

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, default_ttl = 3600) ⇒ Memcached

Returns a new instance of Memcached.



9
10
11
12
# File 'lib/kashmir/plugins/memcached_caching.rb', line 9

def initialize(client, default_ttl = 3600)
  @client = client
  @default_ttl = default_ttl
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/kashmir/plugins/memcached_caching.rb', line 7

def client
  @client
end

Instance Method Details

#bulk_from_cache(definitions, instances) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kashmir/plugins/memcached_caching.rb', line 21

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


  # TODO improve this
  # Creates a hash with all the keys (sorted by the array sort order)
  # and points everything to null
  # ex: [a, b, c] -> { a: nil, b: nil, c: nil }
  results = Hash[keys.map {|x| [x, nil]}]

  # Get results from memcached
  # This will ONLY return cache hits as a Hash
  # ex: { a: cached_a, b: cached_b } note that C is not here
  from_cache = client.get_multi(keys)

  # This assigns each one of the cached values to its keys
  # preserving cache misses (that still point to nil)
  from_cache.each_pair do |key, value|
    results[key] = JSON.parse(value, symbolize_names: true)
  end

  # returns the cached results in the same order as requested.
  # this will also return nil values for cache misses
  results.values
end

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



49
50
51
52
53
54
55
56
# File 'lib/kashmir/plugins/memcached_caching.rb', line 49

def bulk_write(definitions, representations, instances, ttl)
  client.multi do
    instances.each_with_index do |instance, index|
      key = presenter_key(definitions, instance)
      set(key, representations[index], ttl)
    end
  end
end

#clear(definition, instance) ⇒ Object



77
78
79
80
# File 'lib/kashmir/plugins/memcached_caching.rb', line 77

def clear(definition, instance)
  key = presenter_key(definition, instance)
  client.delete(key)
end

#from_cache(definitions, instance) ⇒ Object



14
15
16
17
18
19
# File 'lib/kashmir/plugins/memcached_caching.rb', line 14

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

#get(key) ⇒ Object



67
68
69
70
71
# File 'lib/kashmir/plugins/memcached_caching.rb', line 67

def get(key)
  if data = client.get(key)
    JSON.parse(data, symbolize_names: true)
  end
end

#presenter_key(definition_name, instance) ⇒ Object



63
64
65
# File 'lib/kashmir/plugins/memcached_caching.rb', line 63

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

#set(key, value, ttl = nil) ⇒ Object



73
74
75
# File 'lib/kashmir/plugins/memcached_caching.rb', line 73

def set(key, value, ttl=nil)
  client.set(key, value.to_json, ttl || @default_ttl)
end

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



58
59
60
61
# File 'lib/kashmir/plugins/memcached_caching.rb', line 58

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