Class: Polyrun::Data::CachedFixtures::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/polyrun/data/cached_fixtures.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



17
18
19
20
# File 'lib/polyrun/data/cached_fixtures.rb', line 17

def initialize
  @store = {}
  @stats = {}
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



15
16
17
# File 'lib/polyrun/data/cached_fixtures.rb', line 15

def store
  @store
end

Instance Method Details

#clearObject



38
39
40
41
# File 'lib/polyrun/data/cached_fixtures.rb', line 38

def clear
  store.clear
  @stats.clear
end

#fetch(key, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/polyrun/data/cached_fixtures.rb', line 22

def fetch(key, &block)
  k = key.to_s
  if store.key?(k)
    @stats[k][:hits] += 1
    return store[k]
  end

  t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  store[k] = yield
  @stats[k] = {
    build_time: Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0,
    hits: 0
  }
  store[k]
end

#stats_snapshotObject



43
44
45
# File 'lib/polyrun/data/cached_fixtures.rb', line 43

def stats_snapshot
  @stats.transform_values(&:dup)
end