Class: BrainzLab::Rails::Analyzers::CacheEfficiency

Inherits:
Object
  • Object
show all
Defined in:
lib/brainzlab/rails/analyzers/cache_efficiency.rb

Overview

Tracks cache efficiency metrics and provides insights

Constant Summary collapse

WINDOW_SIZE =

Rolling window for efficiency calculation

1000

Instance Method Summary collapse

Constructor Details

#initializeCacheEfficiency

Returns a new instance of CacheEfficiency.



10
11
12
13
14
15
16
# File 'lib/brainzlab/rails/analyzers/cache_efficiency.rb', line 10

def initialize
  @hits = 0
  @misses = 0
  @reads = []
  @writes = 0
  @generates = 0
end

Instance Method Details

#efficiency_reportObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/brainzlab/rails/analyzers/cache_efficiency.rb', line 43

def efficiency_report
  {
    hit_rate: hit_rate,
    total_hits: @hits,
    total_misses: @misses,
    total_writes: @writes,
    total_generates: @generates,
    recent_reads: recent_reads_stats
  }
end

#hit_rateObject



36
37
38
39
40
41
# File 'lib/brainzlab/rails/analyzers/cache_efficiency.rb', line 36

def hit_rate
  total = @hits + @misses
  return 0.0 if total == 0

  (@hits.to_f / total * 100).round(2)
end

#reset!Object



54
55
56
57
58
59
60
# File 'lib/brainzlab/rails/analyzers/cache_efficiency.rb', line 54

def reset!
  @hits = 0
  @misses = 0
  @reads = []
  @writes = 0
  @generates = 0
end

#track(event_data) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/brainzlab/rails/analyzers/cache_efficiency.rb', line 18

def track(event_data)
  case event_data[:name]
  when 'cache_read.active_support'
    track_read(event_data)
  when 'cache_read_multi.active_support'
    track_read_multi(event_data)
  when 'cache_write.active_support', 'cache_write_multi.active_support'
    @writes += 1
  when 'cache_generate.active_support'
    @generates += 1
  when 'cache_fetch_hit.active_support'
    @hits += 1
  end

  # Trim old reads to maintain window
  trim_reads if @reads.size > WINDOW_SIZE * 2
end