class Graphiti::Util::CacheDebug

Attributes

proxy[R]

Public Class Methods

new(proxy) click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 6
def initialize(proxy)
  @proxy = proxy
end

Public Instance Methods

added_segments() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 72
def added_segments
  changes[0] - changes[1]
end
analyze() { |self| ... } click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 34
def analyze
  yield self
  save
end
change_percentage() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 51
def change_percentage
  return 0 if request_count == 0
  (miss_count.to_i / request_count.to_f * 100).round(1)
end
changed_key?() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 64
def changed_key?
  last_version[:cache_key] != proxy.cache_key_with_version && !new_key?
end
changes() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 76
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
current_version() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 22
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
hit_count() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 47
def hit_count
  current_version[:hit_count]
end
key() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 18
def key
  "graphiti:debug/#{name}"
end
last_version() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 10
def last_version
  @last_version ||= Graphiti.cache.read(key) || {}
end
miss_count() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 43
def miss_count
  current_version[:miss_count]
end
name() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 14
def name
  "#{Graphiti.context[:object]&.request&.method} #{Graphiti.context[:object]&.request&.url}"
end
new_key?() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 60
def new_key?
  last_version[:cache_key].blank? && proxy.cache_key_with_version
end
removed_segments() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 68
def removed_segments
  changes[1] - changes[0]
end
request_count() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 39
def request_count
  current_version[:request_count]
end
save() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 83
def save
  Graphiti.cache.write(key, current_version)
end
volatile?() click to toggle source
# File lib/graphiti/util/cache_debug.rb, line 56
def volatile?
  change_percentage > 50
end