Module: SolidCacheDashboard::Instrumentation

Defined in:
lib/solid_cache_dashboard/instrumentation.rb

Class Method Summary collapse

Class Method Details

.calculate_key_hash(key) ⇒ Object

Calculate the key hash in the same way SolidCache::Entry does



59
60
61
62
63
64
65
# File 'lib/solid_cache_dashboard/instrumentation.rb', line 59

def self.calculate_key_hash(key)
  require 'digest'
  
  # Need to unpack this as a signed integer - Same method used in SolidCache::Entry
  # See: Digest::SHA256.digest(key.to_s).unpack("q>").first
  Digest::SHA256.digest(key.to_s).unpack("q>").first
end

.installObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/solid_cache_dashboard/instrumentation.rb', line 7

def self.install
  ActiveSupport::Notifications.subscribe("cache_read.active_support") do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    key = event.payload[:key].to_s
    key_hash = calculate_key_hash(key)
    hit = event.payload[:hit]
    
    SolidCacheDashboard::CacheEvent.create!(
      event_type: hit ? SolidCacheDashboard::CacheEvent::HIT : SolidCacheDashboard::CacheEvent::MISS,
      key_hash: key_hash,
      key_string: key.truncate(100),
      duration: event.duration / 1000.0, # Convert from ms to seconds
      created_at: Time.current
    )
  end

  ActiveSupport::Notifications.subscribe("cache_write.active_support") do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    key = event.payload[:key].to_s
    key_hash = calculate_key_hash(key)

    entry_size = nil
    if event.payload[:entry]
      entry_size = event.payload[:entry].bytesize
    end
    
    SolidCacheDashboard::CacheEvent.create!(
      event_type: SolidCacheDashboard::CacheEvent::WRITE,
      key_hash: key_hash,
      key_string: key.truncate(100),
      byte_size: entry_size,
      duration: event.duration / 1000.0, # Convert from ms to seconds
      created_at: Time.current
    )
  end

  ActiveSupport::Notifications.subscribe("cache_delete.active_support") do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    key = event.payload[:key].to_s
    key_hash = calculate_key_hash(key)
    
    SolidCacheDashboard::CacheEvent.create!(
      event_type: SolidCacheDashboard::CacheEvent::DELETE,
      key_hash: key_hash,
      key_string: key.truncate(100),
      duration: event.duration / 1000.0, # Convert from ms to seconds
      created_at: Time.current
    )
  end
end