pub struct SegmentCache { /* private fields */ }Expand description
Segment-aware cache for HLS/DASH media segments.
Capacity is managed by two independent budgets: segment count
(max_segments) and total byte size (max_bytes). When either budget is
exceeded, the cache evicts the oldest played segment first (if
evict_played is set), then falls back to the globally oldest segment
across all streams.
Implementations§
Source§impl SegmentCache
impl SegmentCache
Sourcepub fn new(config: SegmentCacheConfig) -> Self
pub fn new(config: SegmentCacheConfig) -> Self
Create a new SegmentCache with the given configuration.
Sourcepub fn insert(&mut self, segment: MediaSegment) -> Result<(), SegmentCacheError>
pub fn insert(&mut self, segment: MediaSegment) -> Result<(), SegmentCacheError>
Insert a segment into the cache.
§Errors
SegmentCacheError::SegmentTooLarge— segment’s byte size exceeds the entiremax_bytesbudget.SegmentCacheError::DuplicateSegment— a segment with the same(stream_id, sequence)is already present.SegmentCacheError::CacheFull— the cache cannot be freed enough to accommodate the new segment (should not happen under normal operation but is returned as a safeguard).
Sourcepub fn get(&mut self, ref_: &SegmentRef) -> Option<&MediaSegment>
pub fn get(&mut self, ref_: &SegmentRef) -> Option<&MediaSegment>
Retrieve the segment identified by ref_.
Returns None if the segment is not in the cache.
Sourcepub fn mark_played(&mut self, ref_: &SegmentRef)
pub fn mark_played(&mut self, ref_: &SegmentRef)
Mark ref_ as played/consumed.
Played segments remain in the cache until eviction but are prioritised
for removal when evict_played is enabled in the configuration.
No-ops if the segment is not in the cache.
Sourcepub fn prefetch_hints(
&self,
current_seq: u64,
stream_id: &str,
) -> Vec<SegmentRef>
pub fn prefetch_hints( &self, current_seq: u64, stream_id: &str, ) -> Vec<SegmentRef>
Generate prefetch hints for stream_id starting at current_seq + 1.
Returns up to prefetch_ahead SegmentRefs for sequences that are
not yet cached. The caller uses these to drive background fetches.
Sourcepub fn evict_oldest_stream(&mut self) -> usize
pub fn evict_oldest_stream(&mut self) -> usize
Evict the oldest sequence from the stream that has the most bytes invested in played segments (or globally oldest if no played segments exist).
Returns the number of bytes freed. Returns 0 if the cache is empty.
Sourcepub fn stats(&self) -> SegmentCacheStats
pub fn stats(&self) -> SegmentCacheStats
Return a statistics snapshot.
Sourcepub fn total_bytes(&self) -> u64
pub fn total_bytes(&self) -> u64
Current total bytes used by all cached segments.
Sourcepub fn segment_count(&self) -> usize
pub fn segment_count(&self) -> usize
Current number of segments in the cache.