Class: ReVIEW::Book::Cache

Inherits:
Object show all
Defined in:
lib/review/book/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

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

Returns:

  • (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

Raises:

  • (ArgumentError)


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

#resetObject



18
19
20
# File 'lib/review/book/cache.rb', line 18

def reset
  @store.clear
end