pub struct CachingByteSource<S> { /* private fields */ }Expand description
A ByteSource wrapper that caches fixed-size pages of the bytes it
reads, evicting the least-recently-used page once a configured byte
budget is exceeded.
This is how a crate::codec::SampleProvider backed by a container
track and a plain ByteSource (such as a file) keeps the memory for
compressed sample bytes bounded: samples near the playhead are read
through overlapping pages that stay cached, and pages behind the budget
are dropped rather than held for the life of the source.
Reads are not required to be page-aligned or page-sized; a read spanning
several pages fetches and caches each of them. Internal caching uses a
RefCell, so a CachingByteSource is usable from one thread at a time,
matching how a reader built over it is used.
Implementations§
Source§impl<S: ByteSource> CachingByteSource<S>
impl<S: ByteSource> CachingByteSource<S>
Sourcepub fn new(inner: S, page_size: u64, budget_bytes: u64) -> Result<Self>
pub fn new(inner: S, page_size: u64, budget_bytes: u64) -> Result<Self>
page_size and budget_bytes must both be nonzero; a page larger
than the budget is still cached (so a single read always succeeds)
but is evicted again on the very next page fetch.
pub fn into_inner(self) -> S
Sourcepub fn resident_bytes(&self) -> u64
pub fn resident_bytes(&self) -> u64
Total bytes currently cached, for tests and diagnostics.
Trait Implementations§
Source§impl<S: ByteSource> ByteSource for CachingByteSource<S>
impl<S: ByteSource> ByteSource for CachingByteSource<S>
Source§fn read_at<'a>(
&'a self,
offset: u64,
destination: &'a mut [u8],
) -> IoFuture<'a, usize>
fn read_at<'a>( &'a self, offset: u64, destination: &'a mut [u8], ) -> IoFuture<'a, usize>
Answers each cached page of the read from the cache, and fetches each run of neighboring pages that is not with one read of the inner source, so a read spanning many pages, such as a large index element, costs one request however large it is (issue #692).