Class: Logster::Cache

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

Instance Method Summary collapse

Constructor Details

#initialize(age = 2) ⇒ Cache

Returns a new instance of Cache.



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

def initialize(age = 2)
  @age = age
  @hash = {}
end

Instance Method Details

#clear(key) ⇒ Object



17
18
19
# File 'lib/logster/cache.rb', line 17

def clear(key)
  @hash.delete(key)
end

#fetch(key) ⇒ Object



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

def fetch(key)
  if !@hash.key?(key) || @hash[key][:created_at] + @age < Process.clock_gettime(Process::CLOCK_MONOTONIC)
    @hash[key] = { data: yield, created_at: Process.clock_gettime(Process::CLOCK_MONOTONIC) }
  end
  @hash[key][:data]
end