Class: Coin::Vault

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
MonitorMixin, Singleton
Defined in:
lib/coin/vault.rb

Instance Method Summary collapse

Instance Method Details

#clearObject


51
52
53
# File 'lib/coin/vault.rb', line 51

def clear
  synchronize { @dict = {} }
end

#delete(key) ⇒ Object


27
28
29
# File 'lib/coin/vault.rb', line 27

def delete(key)
  synchronize { @dict.delete(key) }
end

#ok?Boolean

Returns:

  • (Boolean)

55
56
57
# File 'lib/coin/vault.rb', line 55

def ok?
  true
end

#read(key) ⇒ Object


13
14
15
16
17
18
# File 'lib/coin/vault.rb', line 13

def read(key)
  value = @dict[key]
  value = nil if value && value_expired?(value)
  return value[:value] if value
  nil
end

#read_and_delete(key) ⇒ Object


31
32
33
34
35
36
37
38
# File 'lib/coin/vault.rb', line 31

def read_and_delete(key)
  value = nil
  synchronize do
    value = read(key)
    @dict.delete(key)
  end
  value
end

#read_and_write(key, lifetime = 300) ⇒ Object


40
41
42
43
44
45
46
47
48
49
# File 'lib/coin/vault.rb', line 40

def read_and_write(key, lifetime=300)
  orig_value = nil
  value = nil
  synchronize do
    orig_value = read(key)
    value = yield(orig_value)
    write key, value, lifetime
  end
  [orig_value, value]
end

#write(key, value, lifetime = 300) ⇒ Object


20
21
22
23
24
25
# File 'lib/coin/vault.rb', line 20

def write(key, value, lifetime=300)
  synchronize do
    @dict[key] = { :value => value, :cached_at => Time.now, :lifetime => lifetime }
  end
  value
end