Skip to main content

Module adaptive

Module adaptive 

Source
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 at max_capacity) and TTL is extended by ttl_extension.
  • Hit rate comfortably high → capacity is decreased by shrink_factor (floored at min_capacity) and TTL is shortened by ttl_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§

AdaptiveConfig
Configuration for AdaptivePolicy.
AdaptivePolicy
Adaptive cache threshold policy.
Adjustment
A recommended adjustment emitted by [AdaptivePolicy::evaluate].
AdjustmentRecord
A record stored in the adjustment history.

Enums§

AdaptiveError
Errors returned by AdaptivePolicy operations.
AdjustmentKind
The direction of an adaptive adjustment.