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
-
#cache_hit_rate ⇒ Float
Returns the overall cache hit rate as a percentage (0.0–100.0).
-
#cache_metrics_reset(method_name = nil) ⇒ void
Resets hit/miss counters, either for one method or for all methods.
-
#cache_miss_rate ⇒ Float
Returns the overall cache miss rate as a percentage (0.0–100.0).
-
#cache_stats ⇒ Hash
Returns aggregate metrics across all memoized methods on this instance.
-
#cache_stats_for(method_name) ⇒ Hash
Returns metrics for a single memoized method.
Instance Method Details
#cache_hit_rate ⇒ Float
Returns the overall cache hit rate as a percentage (0.0–100.0).
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.
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_rate ⇒ Float
Returns the overall cache miss rate as a percentage (0.0–100.0).
44 45 46 |
# File 'lib/safe_memoize/public_metrics_methods.rb', line 44 def cache_miss_rate cache_stats[:miss_rate] end |
#cache_stats ⇒ Hash
Returns aggregate metrics across all memoized methods on this instance.
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.
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 |