Class: ReVIEW::Book::Cache
Instance Method Summary collapse
- #cached?(key) ⇒ Boolean
-
#fetch(key, &block) ⇒ Object
key should be Symbol, not String.
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #reset ⇒ Object
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
14 15 16 |
# File 'lib/review/book/cache.rb', line 14 def initialize @store = {} end |
Instance Method Details
#cached?(key) ⇒ Boolean
33 34 35 |
# File 'lib/review/book/cache.rb', line 33 def cached?(key) @store.key?(key) end |
#fetch(key, &block) ⇒ Object
key should be Symbol, not String
23 24 25 26 27 28 29 30 31 |
# File 'lib/review/book/cache.rb', line 23 def fetch(key, &block) raise ArgumentError, 'Key should be Symbol' unless key.is_a?(Symbol) if cached?(key) read(key) else exec_block_and_save(key, &block) end end |
#reset ⇒ Object
18 19 20 |
# File 'lib/review/book/cache.rb', line 18 def reset @store.clear end |