Class: RPass::Cache

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

Constant Summary collapse

CACHE_KEY =
".cache"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage) ⇒ Cache

Returns a new instance of Cache.



14
15
16
# File 'lib/rpass/cache.rb', line 14

def initialize(storage)
  @storage = storage
end

Instance Attribute Details

#storageObject (readonly)

Returns the value of attribute storage.



12
13
14
# File 'lib/rpass/cache.rb', line 12

def storage
  @storage
end

Instance Method Details

#dataObject



35
36
37
# File 'lib/rpass/cache.rb', line 35

def data
  YAML.load(storage.read(CACHE_KEY))
end

#read(name) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/rpass/cache.rb', line 25

def read(name)
  map = data
  if !data[name] || Time.now < data[name]
    storage.read(name)
  else
    storage.delete(name)
    nil
  end
end

#write(name, value, duration: 0) ⇒ Object



18
19
20
21
22
23
# File 'lib/rpass/cache.rb', line 18

def write(name, value, duration: 0)
  storage.write(name, value)
  map = data
  data[name] = Time.now + duration
  storage.write(CACHE_KEY, data)
end