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 7 |
# File 'lib/caching/storage.rb', line 4 def initialize @storage = Concurrent::Hash.new @lock = Mutex.new end |
Instance Method Details
#clear(*keys) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/caching/storage.rb', line 21 def clear(*keys) if keys.empty? @storage = {} else keys.each { |k| @storage.delete k } end end |
#fetch(key) ⇒ Object
17 18 19 |
# File 'lib/caching/storage.rb', line 17 def fetch(key) read(key) || write(key, yield) end |
#read(key) ⇒ Object
9 10 11 |
# File 'lib/caching/storage.rb', line 9 def read(key) @storage[key] end |
#write(key, value) ⇒ Object
13 14 15 |
# File 'lib/caching/storage.rb', line 13 def write(key, value) @storage[key] = value end |