Module: SafeMemoize::PublicMetricsMethods

Included in:
InstanceMethods
Defined in:
lib/safe_memoize/public_metrics_methods.rb

Overview

Per-instance cache metrics: hit/miss counts and average computation time.

Instance Method Summary collapse

Instance Method Details

#cache_hit_rateFloat

Returns the overall cache hit rate as a percentage (0.0–100.0).

Returns:

  • (Float)


38
39
40
# File 'lib/safe_memoize/public_metrics_methods.rb', line 38

def cache_hit_rate
  cache_stats[:hit_rate]
end

#cache_metrics_reset(method_name = nil) ⇒ void

This method returns an undefined value.

Resets hit/miss counters, either for one method or for all methods.

Parameters:

  • method_name (Symbol, String, nil) (defaults to: nil)

    when given, resets only that method's metrics; when +nil+, resets all



53
54
55
56
57
58
59
60
61
# File 'lib/safe_memoize/public_metrics_methods.rb', line 53

def cache_metrics_reset(method_name = nil)
  with_memo_lock do
    if method_name
      _reset_cache_metrics_for(method_name.to_sym)
    else
      _reset_cache_metrics
    end
  end
end

#cache_miss_rateFloat

Returns the overall cache miss rate as a percentage (0.0–100.0).

Returns:

  • (Float)


44
45
46
# File 'lib/safe_memoize/public_metrics_methods.rb', line 44

def cache_miss_rate
  cache_stats[:miss_rate]
end

#cache_statsHash

Returns aggregate metrics across all memoized methods on this instance.

Returns:

  • (Hash)

    with keys +:total_hits+, +:total_misses+, +:hit_rate+, +:miss_rate+, +:average_computation_time+, and +:entries+ (one entry per cached argument combination)



11
12
13
14
15
16
17
18
# File 'lib/safe_memoize/public_metrics_methods.rb', line 11

def cache_stats
  with_memo_lock do
    metrics = memo_metrics_store
    return empty_stats if metrics.empty?

    aggregate_metrics(metrics, include_method: true)
  end
end

#cache_stats_for(method_name) ⇒ Hash

Returns metrics for a single memoized method.

Parameters:

  • method_name (Symbol, String)

Returns:

  • (Hash)

    same shape as #cache_stats but scoped to one method, with an extra +:method+ key



25
26
27
28
29
30
31
32
33
34
# File 'lib/safe_memoize/public_metrics_methods.rb', line 25

def cache_stats_for(method_name)
  method_name = method_name.to_sym

  with_memo_lock do
    metrics = memo_metrics_store.select { |key, _| key[0] == method_name }
    return empty_stats.merge(method: method_name) if metrics.empty?

    aggregate_metrics(metrics, include_method: false).merge(method: method_name)
  end
end