Class: Graphiti::Util::CacheDebug

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiti/util/cache_debug.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy) ⇒ CacheDebug

Returns a new instance of CacheDebug.



6
7
8
# File 'lib/graphiti/util/cache_debug.rb', line 6

def initialize(proxy)
  @proxy = proxy
end

Instance Attribute Details

#proxyObject (readonly)

Returns the value of attribute proxy.



4
5
6
# File 'lib/graphiti/util/cache_debug.rb', line 4

def proxy
  @proxy
end

Instance Method Details

#added_segmentsObject



74
75
76
# File 'lib/graphiti/util/cache_debug.rb', line 74

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

#analyze {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



36
37
38
39
# File 'lib/graphiti/util/cache_debug.rb', line 36

def analyze
  yield self
  save
end

#change_percentageObject



53
54
55
56
# File 'lib/graphiti/util/cache_debug.rb', line 53

def change_percentage
  return 0 if request_count == 0
  (miss_count.to_i / request_count.to_f * 100).round(1)
end

#changed_key?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/graphiti/util/cache_debug.rb', line 66

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

#changesObject



78
79
80
81
82
83
# File 'lib/graphiti/util/cache_debug.rb', line 78

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_versionObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/graphiti/util/cache_debug.rb', line 24

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_countObject



49
50
51
# File 'lib/graphiti/util/cache_debug.rb', line 49

def hit_count
  current_version[:hit_count]
end

#keyObject



20
21
22
# File 'lib/graphiti/util/cache_debug.rb', line 20

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

#last_versionObject



10
11
12
# File 'lib/graphiti/util/cache_debug.rb', line 10

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

#miss_countObject



45
46
47
# File 'lib/graphiti/util/cache_debug.rb', line 45

def miss_count
  current_version[:miss_count]
end

#nameObject



14
15
16
17
18
# File 'lib/graphiti/util/cache_debug.rb', line 14

def name
  tag = proxy.resource_cache_tag

  "#{::Graphiti.context[:object]&.request&.method} #{::Graphiti.context[:object]&.request&.url} #{tag}"
end

#new_key?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/graphiti/util/cache_debug.rb', line 62

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

#removed_segmentsObject



70
71
72
# File 'lib/graphiti/util/cache_debug.rb', line 70

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

#request_countObject



41
42
43
# File 'lib/graphiti/util/cache_debug.rb', line 41

def request_count
  current_version[:request_count]
end

#saveObject



85
86
87
# File 'lib/graphiti/util/cache_debug.rb', line 85

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

#volatile?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/graphiti/util/cache_debug.rb', line 58

def volatile?
  change_percentage > 50
end