Class: SwitchmanInstJobs::TimedCache

Inherits:
Object
  • Object
show all
Defined in:
lib/switchman_inst_jobs/timed_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(timeout, &block) ⇒ TimedCache

Returns a new instance of TimedCache.



3
4
5
6
7
# File 'lib/switchman_inst_jobs/timed_cache.rb', line 3

def initialize(timeout, &block)
  @timeout = timeout
  @block = block
  @cached_at = Time.zone.now
end

Instance Method Details

#clear(force: false) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/switchman_inst_jobs/timed_cache.rb', line 9

def clear(force: false)
  if force || @cached_at < @timeout.call
    @block.call
    @cached_at = Time.zone.now
    true
  else
    false
  end
end