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.



3
4
5
6
# File 'lib/logster/cache.rb', line 3

def initialize(age = 2)
  @age = age
  @hash = { created_at: Process.clock_gettime(Process::CLOCK_MONOTONIC) }
end

Instance Method Details

#clearObject



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

def clear
  @hash.delete(:data)
end

#fetchObject



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

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