Class: RailsHealthChecks::ResultCache

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_health_checks/result_cache.rb

Instance Method Summary collapse

Constructor Details

#initializeResultCache

Returns a new instance of ResultCache.



5
6
7
8
# File 'lib/rails_health_checks/result_cache.rb', line 5

def initialize
  @store = {}
  @mutex = Mutex.new
end

Instance Method Details

#clearObject



21
22
23
# File 'lib/rails_health_checks/result_cache.rb', line 21

def clear
  @mutex.synchronize { @store.clear }
end

#fetch(key, ttl:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/rails_health_checks/result_cache.rb', line 10

def fetch(key, ttl:)
  @mutex.synchronize do
    entry = @store[key]
    return entry[:results] if entry && (Process.clock_gettime(Process::CLOCK_MONOTONIC) - entry[:at]) < ttl

    results = yield
    @store[key] = { results: results, at: Process.clock_gettime(Process::CLOCK_MONOTONIC) }
    results
  end
end