domain.protocols.cache_metrics_protocol¶
src.domain.protocols.cache_metrics_protocol
¶
Cache metrics protocol for performance tracking.
Defines the port for cache metrics collection. Infrastructure layer implements this protocol to provide observability.
Architecture
- Domain layer protocol (port)
- Infrastructure adapter: src/infrastructure/cache/cache_metrics.py
- Used by handlers to track cache hit/miss rates
Reference
- docs/architecture/hexagonal.md
- docs/architecture/observability-architecture.md
Classes¶
CacheMetricsProtocol
¶
Bases: Protocol
Protocol for tracking cache performance metrics.
Abstracts cache metrics tracking used by application layer handlers. Infrastructure layer provides concrete implementation.
Thread-safe metrics tracking for cache operations. Tracks hits, misses, and errors per cache namespace (e.g., "user", "provider", "accounts").
Example
class ListAccountsByUserHandler: def init( self, cache_metrics: CacheMetricsProtocol | None = None, ... ) -> None: self._cache_metrics = cache_metrics
async def handle(self, query: ListAccountsByUser) -> Result[...]:
if cached_value:
self._cache_metrics.record_hit("accounts")
else:
self._cache_metrics.record_miss("accounts")
Source code in src/domain/protocols/cache_metrics_protocol.py
Functions¶
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/domain/protocols/cache_metrics_protocol.py
get_all_stats
¶
Get statistics for all namespaces.
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]]
|
Dictionary mapping namespace to stats dictionary. |
reset
¶
Reset metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
str | None
|
Optional namespace to reset. If None, reset all. |
None
|