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
72 73 74 |
# File 'lib/graphiti/util/cache_debug.rb', line 72 def added_segments changes[0] - changes[1] end |
#analyze {|_self| ... } ⇒ Object
34 35 36 37 |
# File 'lib/graphiti/util/cache_debug.rb', line 34 def analyze yield self save end |
#change_percentage ⇒ Object
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
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 |
#changes ⇒ Object
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_version ⇒ Object
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_count ⇒ Object
47 48 49 |
# File 'lib/graphiti/util/cache_debug.rb', line 47 def hit_count current_version[:hit_count] end |
#key ⇒ Object
18 19 20 |
# File 'lib/graphiti/util/cache_debug.rb', line 18 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
43 44 45 |
# File 'lib/graphiti/util/cache_debug.rb', line 43 def miss_count current_version[:miss_count] end |
#name ⇒ Object
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
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_segments ⇒ Object
68 69 70 |
# File 'lib/graphiti/util/cache_debug.rb', line 68 def removed_segments changes[1] - changes[0] end |
#request_count ⇒ Object
39 40 41 |
# File 'lib/graphiti/util/cache_debug.rb', line 39 def request_count current_version[:request_count] end |
#save ⇒ Object
83 84 85 |
# File 'lib/graphiti/util/cache_debug.rb', line 83 def save Graphiti.cache.write(key, current_version) end |
#volatile? ⇒ Boolean
56 57 58 |
# File 'lib/graphiti/util/cache_debug.rb', line 56 def volatile? change_percentage > 50 end |