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



72
73
74
# File 'lib/graphiti/util/cache_debug.rb', line 72

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

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

Yields:

  • (_self)

Yield Parameters:



34
35
36
37
# File 'lib/graphiti/util/cache_debug.rb', line 34

def analyze
  yield self
  save
end

#change_percentageObject



51
52
53
54
# 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?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/graphiti/util/cache_debug.rb', line 64

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

#changesObject



76
77
78
79
80
81
# 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_versionObject



22
23
24
25
26
27
28
29
30
31
32
# 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_countObject



47
48
49
# File 'lib/graphiti/util/cache_debug.rb', line 47

def hit_count
  current_version[:hit_count]
end

#keyObject



18
19
20
# File 'lib/graphiti/util/cache_debug.rb', line 18

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



43
44
45
# File 'lib/graphiti/util/cache_debug.rb', line 43

def miss_count
  current_version[:miss_count]
end

#nameObject



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

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

#new_key?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/graphiti/util/cache_debug.rb', line 60

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

#removed_segmentsObject



68
69
70
# File 'lib/graphiti/util/cache_debug.rb', line 68

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

#request_countObject



39
40
41
# File 'lib/graphiti/util/cache_debug.rb', line 39

def request_count
  current_version[:request_count]
end

#saveObject



83
84
85
# File 'lib/graphiti/util/cache_debug.rb', line 83

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

#volatile?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/graphiti/util/cache_debug.rb', line 56

def volatile?
  change_percentage > 50
end