Cache Metrics
The Azu performance metrics system includes comprehensive caching statistics to help monitor and optimize cache performance. This feature tracks cache operations, hit/miss ratios, timing data, and error rates across different cache stores.
Overview
Cache metrics are automatically collected when using the Azu::PerformanceMetrics class alongside the Azu cache system. The metrics include:
Hit/Miss Ratios: Track cache effectiveness
Operation Timing: Monitor cache performance
Error Tracking: Identify cache operation failures
Data Volume: Monitor cache storage usage
Operation Breakdown: Analyze specific cache operations
Recording Cache Metrics
Manual Recording
metrics = Azu::PerformanceMetrics.new
# Record a cache operation manually
metrics.record_cache(
key: "user:123",
operation: "get",
store_type: "memory",
processing_time: 1.5,
hit: true,
key_size: 8,
value_size: 256,
ttl: Time::Span.new(minutes: 10)
)Automatic Timing
Use the helper method to automatically time and record operations:
Cache Statistics
Overall Cache Stats
Operation Breakdown
Store-Specific Stats
Tracked Operations
The cache metrics system tracks the following operations:
get: Cache retrieval operations (tracks hits/misses)
set: Cache storage operations (tracks data volume)
delete: Cache deletion operations
exists: Cache existence checks
clear: Cache clearing operations
increment: Atomic increment operations
decrement: Atomic decrement operations
Metrics Data Structure
CacheMetric
Each cache operation creates a CacheMetric with the following properties:
Integration Example
Here's a complete example of integrating cache metrics:
JSON Export
Cache metrics are included in the JSON export:
The exported JSON includes:
recent_caches: Recent cache operationscache_stats: Overall cache statisticscache_breakdown: Per-operation statistics
Performance Considerations
Metrics collection has minimal overhead (< 1ms per operation)
Memory usage is bounded by
MAX_METRICS(default: 10,000 operations)Old metrics are automatically evicted to prevent memory bloat
Thread-safe for concurrent cache operations
Best Practices
Enable Selectively: Only enable cache metrics in development or when monitoring is needed
Monitor Hit Rates: Aim for hit rates > 80% for effective caching
Watch Error Rates: Error rates > 5% may indicate cache connectivity issues
Optimize Slow Operations: Operations > 10ms may need investigation
Review Data Volume: Monitor
total_data_writtento understand cache load
Example Output
This comprehensive cache metrics system helps identify performance bottlenecks and optimize cache configuration for better application performance.
Last updated
Was this helpful?
