Class: Caches::Memcached

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

Constant Summary collapse

@@max_size =
1000000

Instance Method Summary collapse

Constructor Details

#initializeMemcached

Returns a new instance of Memcached.



10
11
12
# File 'lib/store/caches/memcached.rb', line 10

def initialize
  @cache = EM::P::Memcache.connect
end

Instance Method Details

#invalidateObject



14
15
16
# File 'lib/store/caches/memcached.rb', line 14

def invalidate
  set 'invalidated_at', Time.new
end

#load(key) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/store/caches/memcached.rb', line 18

def load(key)
  key_date = "#{key}_date"

  time_set = get(key_date) 
  invalidated_at = get('invalidated_at')

  if time_set && (!invalidated_at || time_set > invalidated_at)
    data = get(key)
    data
  end
end

#save(key, data) ⇒ Object



30
31
32
33
34
35
# File 'lib/store/caches/memcached.rb', line 30

def save key, data
  key_date = "#{key}_date"

  set(key, data)
  set(key_date, Time.new)
end