Skip to main content

SegmentCache

Struct SegmentCache 

Source
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

Source

pub fn new(config: SegmentCacheConfig) -> Self

Create a new SegmentCache with the given configuration.

Source

pub fn insert(&mut self, segment: MediaSegment) -> Result<(), SegmentCacheError>

Insert a segment into the cache.

§Errors
Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn stats(&self) -> SegmentCacheStats

Return a statistics snapshot.

Source

pub fn total_bytes(&self) -> u64

Current total bytes used by all cached segments.

Source

pub fn segment_count(&self) -> usize

Current number of segments in the cache.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.