Class: Fino::Cache::Memory
- Inherits:
-
Object
- Object
- Fino::Cache::Memory
- Includes:
- Fino::Cache
- Defined in:
- lib/fino/cache/memory.rb
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #exist?(key) ⇒ Boolean
- #fetch(key, &block) ⇒ Object
- #fetch_multi(*keys, &block) ⇒ Object
-
#initialize(expires_in:) ⇒ Memory
constructor
A new instance of Memory.
- #read(key) ⇒ Object
- #write(key, value) ⇒ Object
Constructor Details
Instance Method Details
#clear ⇒ Object
59 60 61 |
# File 'lib/fino/cache/memory.rb', line 59 def clear hash.clear end |
#delete(key) ⇒ Object
55 56 57 |
# File 'lib/fino/cache/memory.rb', line 55 def delete(key) hash.delete(key) end |
#exist?(key) ⇒ Boolean
11 12 13 14 15 |
# File 'lib/fino/cache/memory.rb', line 11 def exist?(key) expire_if_ready hash.key?(key) end |
#fetch(key, &block) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/fino/cache/memory.rb', line 27 def fetch(key, &block) raise ArgumentError, "no block provided to #{self.class.name}#fetch" unless block expire_if_ready hash.fetch(key) do write(key, block.call) end end |
#fetch_multi(*keys, &block) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/fino/cache/memory.rb', line 37 def fetch_multi(*keys, &block) raise ArgumentError, "no block provided to #{self.class.name}#fetch_multi" unless block expire_if_ready missing_keys = keys - hash.keys if missing_keys.any? results = block.call(missing_keys) results.each do |key, value| write(key, value) end end hash.values_at(*keys) end |
#read(key) ⇒ Object
17 18 19 20 21 |
# File 'lib/fino/cache/memory.rb', line 17 def read(key) expire_if_ready hash[key] end |
#write(key, value) ⇒ Object
23 24 25 |
# File 'lib/fino/cache/memory.rb', line 23 def write(key, value) hash[key] = value end |