Class: Stale::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/stale/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Cache

Returns a new instance of Cache.



5
6
7
# File 'lib/stale/cache.rb', line 5

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/stale/cache.rb', line 3

def data
  @data
end

Instance Method Details

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



17
18
19
# File 'lib/stale/cache.rb', line 17

def add(key, value, ttl = nil)
  data.add(key_with_prefix(key), value, ttl)
end

#delete(*keys) ⇒ Object



21
22
23
24
25
# File 'lib/stale/cache.rb', line 21

def delete(*keys)
  keys.each do |key|
    data.delete(key_with_prefix(key))
  end
end

#get(key) ⇒ Object



9
10
11
# File 'lib/stale/cache.rb', line 9

def get(key)
  data.get(key_with_prefix(key))
end

#modify(key, ttl = nil, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/stale/cache.rb', line 27

def modify(key, ttl = nil, &block)
  stored = false
  retries = 0

  while !stored
    stored = data.cas(key_with_prefix(key), ttl, &block)

    if stored.nil?
      stored = add(key, block.call(nil), ttl)
    elsif stored == false
      retries += 1
    end
  end

  stored
end

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



13
14
15
# File 'lib/stale/cache.rb', line 13

def set(key, value, ttl = nil)
  data.set(key_with_prefix(key), value, ttl)
end