Class: Caching::Storage
- Inherits:
-
Object
- Object
- Caching::Storage
- Defined in:
- lib/caching/storage.rb
Instance Method Summary collapse
- #clear(*keys) ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize ⇒ Storage
constructor
A new instance of Storage.
- #read(key) ⇒ Object
- #write(key, value) ⇒ Object
Constructor Details
#initialize ⇒ Storage
Returns a new instance of Storage.
4 5 6 |
# File 'lib/caching/storage.rb', line 4 def initialize @storage = {} end |
Instance Method Details
#clear(*keys) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/caching/storage.rb', line 20 def clear(*keys) if keys.empty? @storage = {} else keys.each { |k| @storage.delete k } end end |
#fetch(key) ⇒ Object
16 17 18 |
# File 'lib/caching/storage.rb', line 16 def fetch(key) read(key) || write(key, yield) end |
#read(key) ⇒ Object
8 9 10 |
# File 'lib/caching/storage.rb', line 8 def read(key) @storage[key] end |
#write(key, value) ⇒ Object
12 13 14 |
# File 'lib/caching/storage.rb', line 12 def write(key, value) @storage[key] = value end |