Class: RockautoApi::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/rockauto_api/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(store: nil) ⇒ Cache

Returns a new instance of Cache.



5
6
7
8
# File 'lib/rockauto_api/cache.rb', line 5

def initialize(store: nil)
  @store = store
  @memory = {}
end

Instance Method Details

#clearObject



26
27
28
29
30
31
32
# File 'lib/rockauto_api/cache.rb', line 26

def clear
  if @store
    @store.clear
  else
    @memory.clear
  end
end

#delete(key) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/rockauto_api/cache.rb', line 18

def delete(key)
  if @store
    @store.delete(key)
  else
    @memory.delete(key)
  end
end

#fetch(key, ttl: 3600) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/rockauto_api/cache.rb', line 10

def fetch(key, ttl: 3600)
  if @store
    @store.fetch(key, expires_in: ttl) { yield }
  else
    @memory[key] ||= yield
  end
end