Module: Observ::PromptManager::CacheStatistics

Included in:
Observ::PromptManager
Defined in:
app/services/observ/prompt_manager/cache_statistics.rb

Overview

Concern for cache statistics tracking and reporting. Handles hit/miss tracking and statistics aggregation.

Instance Method Summary collapse

Instance Method Details

#cache_stats(name) ⇒ Hash

Get cache statistics for a prompt

Parameters:

  • name (String)

    The prompt name

Returns:

  • (Hash)

    Statistics hash with :name, :hits, :misses, :total, :hit_rate



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/observ/prompt_manager/cache_statistics.rb', line 15

def cache_stats(name)
  hits_key = "#{Observ.config.prompt_cache_namespace}:stats:#{name}:hits"
  misses_key = "#{Observ.config.prompt_cache_namespace}:stats:#{name}:misses"

  hits = Rails.cache.read(hits_key) || 0
  misses = Rails.cache.read(misses_key) || 0
  total = hits + misses
  hit_rate = total > 0 ? (hits.to_f / total * 100).round(2) : 0

  {
    name: name,
    hits: hits,
    misses: misses,
    total: total,
    hit_rate: hit_rate
  }
end

#clear_statsBoolean

Clear all cache statistics

Returns:

  • (Boolean)

    true if successful



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/observ/prompt_manager/cache_statistics.rb', line 35

def clear_stats
  Observ::Prompt.distinct.pluck(:name).each do |name|
    hits_key = "#{Observ.config.prompt_cache_namespace}:stats:#{name}:hits"
    misses_key = "#{Observ.config.prompt_cache_namespace}:stats:#{name}:misses"

    Rails.cache.delete(hits_key)
    Rails.cache.delete(misses_key)
  end

  Rails.logger.info("Cache statistics cleared")
  true
end