Class: Graphiti::Util::CacheDebug
- Defined in:
- lib/graphiti/util/cache_debug.rb
Instance Attribute Summary collapse
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
Instance Method Summary collapse
- #added_segments ⇒ Object
- #analyze {|_self| ... } ⇒ Object
- #change_percentage ⇒ Object
- #changed_key? ⇒ Boolean
- #changes ⇒ Object
- #current_version ⇒ Object
- #hit_count ⇒ Object
-
#initialize(proxy) ⇒ CacheDebug
constructor
A new instance of CacheDebug.
- #key ⇒ Object
- #last_version ⇒ Object
- #miss_count ⇒ Object
- #name ⇒ Object
- #new_key? ⇒ Boolean
- #removed_segments ⇒ Object
- #request_count ⇒ Object
- #save ⇒ Object
- #volatile? ⇒ Boolean
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
#proxy ⇒ Object (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_segments ⇒ Object
74 75 76 |
# File 'lib/graphiti/util/cache_debug.rb', line 74 def added_segments changes[0] - changes[1] end |
#analyze {|_self| ... } ⇒ Object
36 37 38 39 |
# File 'lib/graphiti/util/cache_debug.rb', line 36 def analyze yield self save end |
#change_percentage ⇒ Object
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
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 |
#changes ⇒ Object
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_version ⇒ Object
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_count ⇒ Object
49 50 51 |
# File 'lib/graphiti/util/cache_debug.rb', line 49 def hit_count current_version[:hit_count] end |
#key ⇒ Object
20 21 22 |
# File 'lib/graphiti/util/cache_debug.rb', line 20 def key "graphiti:debug/#{name}" end |
#last_version ⇒ Object
10 11 12 |
# File 'lib/graphiti/util/cache_debug.rb', line 10 def last_version @last_version ||= Graphiti.cache.read(key) || {} end |
#miss_count ⇒ Object
45 46 47 |
# File 'lib/graphiti/util/cache_debug.rb', line 45 def miss_count current_version[:miss_count] end |
#name ⇒ Object
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
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_segments ⇒ Object
70 71 72 |
# File 'lib/graphiti/util/cache_debug.rb', line 70 def removed_segments changes[1] - changes[0] end |
#request_count ⇒ Object
41 42 43 |
# File 'lib/graphiti/util/cache_debug.rb', line 41 def request_count current_version[:request_count] end |
#save ⇒ Object
85 86 87 |
# File 'lib/graphiti/util/cache_debug.rb', line 85 def save Graphiti.cache.write(key, current_version) end |
#volatile? ⇒ Boolean
58 59 60 |
# File 'lib/graphiti/util/cache_debug.rb', line 58 def volatile? change_percentage > 50 end |