Expand description
Adaptive cache threshold management.
Dynamically adjusts cache eviction thresholds based on rolling hit-rate statistics. When the observed hit rate drops below a configurable target the policy widens the cache (increases capacity or extends TTL); when the hit rate exceeds the target by a comfortable margin the policy contracts the cache to free resources.
§Design
AdaptivePolicy maintains a circular ring buffer of recent hit/miss
observations. Every adjustment_interval operations it computes the
rolling hit rate over the window and compares it against the configured
target_hit_rate.
- Hit rate too low → capacity is increased by
growth_factor(capped atmax_capacity) and TTL is extended byttl_extension. - Hit rate comfortably high → capacity is decreased by
shrink_factor(floored atmin_capacity) and TTL is shortened byttl_reduction. - Hit rate within band → no change.
The policy itself does not own a cache; it emits Adjustment
recommendations that the caller applies to their cache of choice.
§Example
use oximedia_cache::adaptive::{AdaptiveConfig, AdaptivePolicy};
use std::time::Duration;
let cfg = AdaptiveConfig {
target_hit_rate: 0.80,
tolerance: 0.05,
adjustment_interval: 20,
min_capacity: 16,
max_capacity: 4096,
growth_factor: 1.5,
shrink_factor: 0.75,
ttl_extension: Duration::from_secs(10),
ttl_reduction: Duration::from_secs(5),
window_size: 100,
};
let mut policy = AdaptivePolicy::new(cfg).expect("valid config");
// Simulate 20 cache hits.
for _ in 0..20 {
let _ = policy.record_hit();
}Structs§
- Adaptive
Config - Configuration for
AdaptivePolicy. - Adaptive
Policy - Adaptive cache threshold policy.
- Adjustment
- A recommended adjustment emitted by [
AdaptivePolicy::evaluate]. - Adjustment
Record - A record stored in the adjustment history.
Enums§
- Adaptive
Error - Errors returned by
AdaptivePolicyoperations. - Adjustment
Kind - The direction of an adaptive adjustment.