Skip to content

Commit

Permalink
wip: experimental logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jkeen committed Mar 26, 2024
1 parent 8ed5130 commit 296b80d
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/graphiti/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,27 @@ def render(renderer)
options[:proxy] = proxy

if proxy.cache? && Graphiti.config.cache
Graphiti.cache("graphiti:render/#{proxy.cache_key}", version: proxy.updated_at, expires_in: proxy.cache_expires_in) do
Graphiti.cache.fetch("graphiti:render/#{proxy.cache_key}", version: proxy.updated_at, expires_in: proxy.cache_expires_in) do
# Try to get some debug information about the cache key to find out if a query cache isn't stable
url = Graphiti.context[:object].request.url
Graphiti.log("URL: #{url}", :cyan)
if url.present?
key = "graphiti:debug/#{url}"
old_results = ::Rails.cache.fetch(key) || {}
new_results = { cache_key: proxy.cache_key, version: proxy.updated_at, expires_in: proxy.cache_expires_in }
Graphiti.cache.fetch(key) do
new_results
end

sub_keys_old = old_results[:cache_key]&.scan(/\w+\/query-[a-z0-9]+\/args-[a-z0-9]+/).to_a
sub_keys_new = new_results[:cache_key]&.scan(/\w+\/query-[a-z0-9]+\/args-[a-z0-9]+/).to_a
if ((sub_keys_new - sub_keys_old)&.size > 0)
Graphiti.log "Cache key changed for #{url}", :red, bold: true
Graphiti.log "Old Cache Keys: #{sub_keys_old - sub_keys_new}", :red, bold: true
Graphiti.log "New Cache Keys: #{sub_keys_new - sub_keys_old}", :cyan, bold: true
end
end

options.delete(:cache) # ensure that we don't use JSONAPI-Resources's built-in caching logic
renderer.render(records, options)
end
Expand Down

0 comments on commit 296b80d

Please sign in to comment.