Skip to main content

Module weighted_cache

Module weighted_cache 

Source
Expand description

Weighted cache scoring with configurable per-media-type weight factors.

Standard LRU eviction treats all entries equally. For multimedia workloads different content types benefit from different retention policies. This module provides WeightConfig — a table of per-type weights (recency, size-efficiency, priority) — and WeightedCache, an LRU-ordered cache that uses the blended score to decide which entry to evict next.

§Scoring formula

For each candidate entry the eviction score is:

score = recency_weight * recency_factor
      + priority_weight * priority_factor
      - size_weight * size_factor

where

  • recency_factor is 1.0 - (age_ns / max_age_ns) clamped to [0, 1]
  • priority_factor is the entry’s priority normalised to [0, 1]
  • size_factor is entry_bytes / max_entry_bytes normalised to [0, 1]

The entry with the lowest score is the best eviction candidate.

§Example

use oximedia_cache::weighted_cache::{WeightConfig, WeightedCache, CacheMediaType};

let weights = WeightConfig::default();
let mut cache = WeightedCache::new(256, weights);
cache.insert("seg-001", vec![0u8; 64], CacheMediaType::VideoSegment, 7);
assert!(cache.get("seg-001").is_some());

Structs§

TypeWeights
Weight factors for one media type.
WeightConfig
Table of per-media-type weight overrides.
WeightedCache
Capacity-bounded cache that uses per-media-type weight factors to choose eviction candidates.

Enums§

CacheMediaType
The media type label attached to a cached entry.
WeightedCacheError
Errors produced by WeightedCache operations.