Class: RPass::Cache
- Inherits:
-
Object
- Object
- RPass::Cache
- Defined in:
- lib/rpass/cache.rb
Constant Summary collapse
- CACHE_KEY =
".cache"
Instance Attribute Summary collapse
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
Instance Method Summary collapse
- #data ⇒ Object
-
#initialize(storage) ⇒ Cache
constructor
A new instance of Cache.
- #read(name) ⇒ Object
- #write(name, value, duration: 0) ⇒ Object
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
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
12 13 14 |
# File 'lib/rpass/cache.rb', line 12 def storage @storage end |
Instance Method Details
#data ⇒ Object
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 |