Skip to content

Commit

Permalink
Add caching debug info to better spot volatile cache keys that need i…
Browse files Browse the repository at this point in the history
…mproving
  • Loading branch information
jkeen committed Mar 27, 2024
1 parent 8ceba3d commit fe50f7d
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/graphiti.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def self.cache
require "graphiti/serializer"
require "graphiti/query"
require "graphiti/debugger"
require "graphiti/util/cache_debug"

if defined?(ActiveRecord)
require "graphiti/adapters/active_record"
Expand Down
25 changes: 22 additions & 3 deletions lib/graphiti/debugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,29 @@ def on_render(name, start, stop, id, payload)
took = ((stop - start) * 1000.0).round(2)
logs << [""]
logs << ["=== Graphiti Debug", :green, true]
logs << if payload[:proxy]&.cached? && Graphiti.config.cache_rendering?
["Rendering (cached):", :green, true]
if payload[:proxy]&.cached? && Graphiti.config.cache_rendering?
logs << ["Rendering (cached):", :green, true]

Graphiti::Util::CacheDebug.new(payload[:proxy]).analyze do |cache_debug|
logs << ["Cache key for #{cache_debug.name}", :blue, true]
if cache_debug.volatile?
logs << [" \\_ volatile | Request count: #{cache_debug.request_count} | Hit count: #{cache_debug.hit_count}", :red, true]
else
logs << [" \\_ stable | Request count: #{cache_debug.request_count} | Hit count: #{cache_debug.hit_count}", :blue, true]
end

if cache_debug.changed_key?
logs << [" [x] cache key changed #{cache_debug.last_version[:etag]} -> #{cache_debug.current_version[:etag]}", :red]
logs << [" removed: #{cache_debug.removed_segments}", :red]
logs << [" added: #{cache_debug.added_segments}", :red]
elsif cache_debug.new_key?
logs << [" [+] cache key added #{cache_debug.current_version[:etag]}", :red, true]
else
logs << [" [✓] #{cache_debug.current_version[:etag]}", :green, true]
end
end
else
["Rendering:", :green, true]
logs << ["Rendering:", :green, true]
end
logs << ["Took: #{took}ms", :magenta, true]
end
Expand Down
91 changes: 91 additions & 0 deletions lib/graphiti/util/cache_debug.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
module Graphiti
module Util
class CacheDebug
attr_reader :proxy

def initialize(proxy)
@proxy = proxy
end

def last_version
@last_version ||= Graphiti.cache.read(key) || {}
end

def name
"#{Graphiti.context[:object].request.method} #{Graphiti.context[:object].request.url}"
end

def key
"graphiti:debug/#{name}"
end

def current_version
@current_version ||= {
cache_key: proxy.cache_key_with_version,
version: proxy.updated_at,
expires_in: proxy.cache_expires_in,
etag: proxy.etag,
miss_count: last_version[:miss_count].to_i + (changed_key? ? 1 : 0),
hit_count: last_version[:hit_count].to_i + (!changed_key? && !new_key? ? 1 : 0),
request_count: last_version[:request_count].to_i + (last_version.present? ? 1 : 0)
}
end

def analyze
yield self
save
end

def request_count
current_version[:request_count]
end

def miss_count
current_version[:miss_count]
end

def hit_count
current_version[:hit_count]
end

def change_percentage
return 0 if request_count == 0

(miss_count.to_i / request_count.to_f * 100).round(1)
rescue Exception => e
puts e.inspect
end

def volatile?
change_percentage > 50
end

def new_key?
last_version[:cache_key].blank? && proxy.cache_key_with_version
end

def changed_key?
last_version[:cache_key] != proxy.cache_key_with_version && !new_key?
end

def removed_segments
changes[1] - changes[0]
end

def added_segments
changes[0] - changes[1]
end

def changes
sub_keys_old = last_version[:cache_key]&.scan(/\w+\/query-[a-z0-9-]+\/args-[a-z0-9-]+/).to_a || []
sub_keys_new = current_version[:cache_key]&.scan(/\w+\/query-[a-z0-9-]+\/args-[a-z0-9-]+/).to_a || []

[sub_keys_old, sub_keys_new]
end

def save
Graphiti.cache.write(key, current_version)
end
end
end
end

0 comments on commit fe50f7d

Please sign in to comment.