Class: RailsErrorDashboard::Services::CacheAnalyzer
- Inherits:
-
Object
- Object
- RailsErrorDashboard::Services::CacheAnalyzer
- Defined in:
- lib/rails_error_dashboard/services/cache_analyzer.rb
Overview
Pure algorithm: Analyze cache breadcrumbs at display time
Operates on already-captured breadcrumb data — zero runtime cost. Called at display time only. Similar pattern to NplusOneDetector.
Class Method Summary collapse
-
.call(breadcrumbs) ⇒ Hash?
Cache analysis summary, or nil if no cache breadcrumbs.
Class Method Details
.call(breadcrumbs) ⇒ Hash?
Returns Cache analysis summary, or nil if no cache breadcrumbs.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rails_error_dashboard/services/cache_analyzer.rb', line 18 def self.call() return nil unless .is_a?(Array) cache_crumbs = .select { |c| c["c"] == "cache" } return nil if cache_crumbs.empty? reads = 0 writes = 0 hits = 0 misses = 0 unknown = 0 total_duration = 0.0 slowest = nil cache_crumbs.each do |crumb| = crumb["m"].to_s duration = crumb["d"].to_f if .start_with?("cache read:") reads += 1 hit_status = crumb.dig("meta", "hit") if hit_status.nil? unknown += 1 elsif hit_status == true || hit_status == "true" hits += 1 else misses += 1 end elsif .start_with?("cache write:") writes += 1 end total_duration += duration if slowest.nil? || duration > slowest[:duration_ms] slowest = { message: , duration_ms: duration } end end # Only calculate hit_rate if we have read breadcrumbs with known hit status known_reads = hits + misses hit_rate = known_reads > 0 ? (hits.to_f / known_reads * 100).round(1) : nil { reads: reads, writes: writes, hits: hits, misses: misses, unknown: unknown, hit_rate: hit_rate, total_duration_ms: total_duration.round(1), slowest: slowest } rescue => e nil end |