Class: Friendly::Memcached

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly/memcached.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache) ⇒ Memcached

Returns a new instance of Memcached.



5
6
7
# File 'lib/friendly/memcached.rb', line 5

def initialize(cache)
  @cache = cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



3
4
5
# File 'lib/friendly/memcached.rb', line 3

def cache
  @cache
end

Instance Method Details

#delete(key) ⇒ Object



36
37
38
39
# File 'lib/friendly/memcached.rb', line 36

def delete(key)
  cache.delete(key)
rescue ::Memcached::NotFound
end

#get(key) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/friendly/memcached.rb', line 13

def get(key)
  @cache.get(key)
rescue ::Memcached::NotFound
  if block_given?
    miss(key) { yield }
  end
end

#multiget(keys) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/friendly/memcached.rb', line 21

def multiget(keys)
  return {} if keys.empty?

  hits         = @cache.get(keys)
  missing_keys = keys - hits.keys

  if !missing_keys.empty? && block_given?
    missing_keys.each do |missing_key|
      hits.merge!(missing_key => miss(missing_key) { yield(missing_key) })
    end
  end

  hits
end

#set(key, value) ⇒ Object



9
10
11
# File 'lib/friendly/memcached.rb', line 9

def set(key, value)
  @cache.set(key, value)
end