pub struct SegmentCache { /* private fields */ }Expand description
LRU segment cache with configurable memory bound.
Stores decoded Arrow arrays from segment files. When the cache exceeds its memory bound, the least-recently-used entries are evicted via an O(1) linked-list removal.
Singleflight: When multiple threads request the same uncached key simultaneously, only one thread invokes the loader while the others wait. This prevents thundering-herd cache stampedes.
Thread-safe via RwLock<CacheInner> with atomic counters for stats.
Implementations§
Source§impl SegmentCache
impl SegmentCache
Sourcepub fn new(max_bytes: usize) -> Self
pub fn new(max_bytes: usize) -> Self
Create a new segment cache with the given maximum size in bytes.
Sourcepub async fn get_or_load<F, E>(
&self,
key: SegmentCacheKey,
loader: F,
) -> Result<ArrayRef, E>
pub async fn get_or_load<F, E>( &self, key: SegmentCacheKey, loader: F, ) -> Result<ArrayRef, E>
Get a cached array, or load it using the provided closure on miss.
Uses singleflight dedup: if another task is already loading the
same key, this task awaits the result instead of invoking the
loader again. The wait uses tokio::sync::Notify so the
calling tokio worker thread is not blocked.
Cache hits use a read lock (no contention). LRU promotion is probabilistic — only every 16th hit acquires a write lock to reorder the LRU list, amortising lock contention on the hot read path.
§Errors
Returns an error if the loader function fails.
Sourcepub fn invalidate_segment(&self, segment_id: SegmentId)
pub fn invalidate_segment(&self, segment_id: SegmentId)
Invalidate all entries for a given segment.
Sourcepub fn size_bytes(&self) -> u64
pub fn size_bytes(&self) -> u64
Returns the current cache size in bytes.
Sourcepub fn entry_count(&self) -> usize
pub fn entry_count(&self) -> usize
Returns the number of cached entries.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for SegmentCache
impl !RefUnwindSafe for SegmentCache
impl !UnwindSafe for SegmentCache
impl Send for SegmentCache
impl Sync for SegmentCache
impl Unpin for SegmentCache
impl UnsafeUnpin for SegmentCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more