Class: QaServer::PerformanceCache

Inherits:
Object
  • Object
show all
Defined in:
app/cache_processors/qa_server/performance_cache.rb

Instance Method Summary collapse

Constructor Details

#initializePerformanceCache

Returns a new instance of PerformanceCache.



7
8
9
# File 'app/cache_processors/qa_server/performance_cache.rb', line 7

def initialize
  @cache = {}
end

Instance Method Details

#complete_entry(id:) ⇒ Object



26
27
28
29
30
# File 'app/cache_processors/qa_server/performance_cache.rb', line 26

def complete_entry(id:)
  log(id: id)
  QaServer.config.performance_cache_logger.debug("#{self.class}##{__method__} - id: #{id}   cache memory: #{ObjectSpace.memsize_of @cache}")
  write_all if ObjectSpace.memsize_of(@cache) > QaServer.config.max_performance_cache_size
end

#destroy(id) ⇒ Object



32
33
34
# File 'app/cache_processors/qa_server/performance_cache.rb', line 32

def destroy(id)
  @cache.delete(id) # WARNING: doesn't change the size of the cache
end

#new_entry(authority:, action:) ⇒ Object



11
12
13
14
15
16
17
18
# File 'app/cache_processors/qa_server/performance_cache.rb', line 11

def new_entry(authority:, action:)
  entry = { dt_stamp: QaServer::TimeService.current_time,
            authority: authority,
            action: action }
  id = SecureRandom.uuid
  @cache[id] = entry
  id
end

#update(id:, updates: {}) ⇒ Object



20
21
22
23
24
# File 'app/cache_processors/qa_server/performance_cache.rb', line 20

def update(id:, updates: {})
  return false unless id && @cache.key?(id)
  entry = @cache[id]
  @cache[id] = entry.merge(updates)
end

#write_allObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/cache_processors/qa_server/performance_cache.rb', line 36

def write_all
  cache_to_write = swap_cache_hash
  size_before = cache_to_write.size
  cache_to_write.each do |id, entry|
    next if incomplete? entry
    QaServer::PerformanceHistory.create(dt_stamp: entry[:dt_stamp], authority: entry[:authority],
                                        action: entry[:action], action_time_ms: entry[:action_time_ms],
                                        size_bytes: entry[:size_bytes], retrieve_time_ms: entry[:retrieve_time_ms],
                                        graph_load_time_ms: entry[:graph_load_time_ms],
                                        normalization_time_ms: entry[:normalization_time_ms])
    cache_to_write.delete(id)
  end
  log_write_all("(#{self.class}##{__method__})", size_before, cache_to_write.size)
  cache_to_write = nil # free cache for garbage collection
end