infrastructure.cache.cache_metrics¶
src.infrastructure.cache.cache_metrics
¶
Cache metrics tracking for observability.
This module provides lightweight in-memory metrics tracking for cache operations. Metrics include hit/miss counts and hit rates for monitoring cache effectiveness.
Usage
from src.infrastructure.cache.cache_metrics import CacheMetrics
metrics = CacheMetrics() metrics.record_hit("user") metrics.record_miss("user")
stats = metrics.get_stats("user") print(f"Hit rate: {stats['hit_rate']:.2%}")
Classes¶
CacheStats
dataclass
¶
Cache statistics for a specific cache namespace.
Attributes:
| Name | Type | Description |
|---|---|---|
hits |
int
|
Number of cache hits (value found in cache). |
misses |
int
|
Number of cache misses (value not in cache). |
errors |
int
|
Number of cache operation errors. |
Source code in src/infrastructure/cache/cache_metrics.py
CacheMetrics
¶
In-memory cache metrics tracker.
Thread-safe metrics tracking for cache operations. Tracks hits, misses, and errors per cache namespace (e.g., "user", "provider", "accounts").
This is a lightweight, in-memory tracker suitable for development and production monitoring. For production, metrics can be exported to observability platforms (Prometheus, Datadog, etc.).
Example
metrics = CacheMetrics()
Record operations¶
metrics.record_hit("user") metrics.record_miss("provider") metrics.record_error("accounts")
Get stats¶
stats = metrics.get_stats("user") print(f"User cache hit rate: {stats['hit_rate']:.2%}")
Get all stats¶
all_stats = metrics.get_all_stats()
Source code in src/infrastructure/cache/cache_metrics.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
Functions¶
__init__
¶
record_hit
¶
Record a cache hit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str
|
Cache namespace (e.g., "user", "provider"). |
required |
record_miss
¶
Record a cache miss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str
|
Cache namespace (e.g., "user", "provider"). |
required |
record_error
¶
Record a cache operation error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str
|
Cache namespace (e.g., "user", "provider"). |
required |
get_stats
¶
Get statistics for a specific namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str
|
Cache namespace to query. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with hits, misses, errors, total_requests, hit_rate. |
Source code in src/infrastructure/cache/cache_metrics.py
get_all_stats
¶
Get statistics for all namespaces.
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]]
|
Dictionary mapping namespace to stats dictionary. |
Source code in src/infrastructure/cache/cache_metrics.py
reset
¶
Reset metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str | None
|
Optional namespace to reset. If None, reset all. |
None
|
Source code in src/infrastructure/cache/cache_metrics.py
Functions¶
get_cache_metrics
¶
Get global cache metrics singleton.
Returns:
| Type | Description |
|---|---|
CacheMetrics
|
Global CacheMetrics instance. |