pub struct Cache { /* private fields */ }Expand description
Fixed-capacity LRU cache keyed by PageId.
Implementations§
Source§impl Cache
impl Cache
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Construct a cache with capacity pre-allocated frames.
capacity must be at least 1. Eviction kicks in once all
frames are bound to a page.
Sourcepub fn get(&mut self, id: PageId) -> Option<&Page>
pub fn get(&mut self, id: PageId) -> Option<&Page>
Get a read-only reference to a cached page, if present, and mark it as most-recently-used. No allocation occurs.
Sourcepub fn peek(&self, id: PageId) -> Option<&Page>
pub fn peek(&self, id: PageId) -> Option<&Page>
Read-only peek that does NOT touch the LRU order. Used by
Pager::read_cache_or_main (which holds a shared &Pager
borrow on the snapshot-read path); a mutating get would
be a borrow-checker error there.
Sourcepub fn get_mut(&mut self, id: PageId) -> Option<&mut Page>
pub fn get_mut(&mut self, id: PageId) -> Option<&mut Page>
Get a mutable reference to a cached page, marking it dirty and most-recently-used.
Sourcepub fn insert(
&mut self,
id: PageId,
buffer: Page,
dirty: bool,
) -> Option<Evicted>
pub fn insert( &mut self, id: PageId, buffer: Page, dirty: bool, ) -> Option<Evicted>
Insert (id, buffer) into the cache. If the cache is full,
evicts the LRU frame; the returned Evicted is the caller’s
responsibility to write back if dirty is set.
dirty controls whether the newly-inserted frame is considered
modified. Pages loaded from disk should pass false; pages
allocated fresh by alloc_page should pass true.
Sourcepub fn evict(&mut self, id: PageId) -> Option<Evicted>
pub fn evict(&mut self, id: PageId) -> Option<Evicted>
Force-evict id if present, returning its buffer + dirty flag.
Used by free_page to invalidate the cache entry.
Sourcepub fn drain_dirty(&mut self) -> impl Iterator<Item = (PageId, Page)> + '_
pub fn drain_dirty(&mut self) -> impl Iterator<Item = (PageId, Page)> + '_
Iterate over (page_id, buffer) for every dirty bound frame,
returning ownership of each buffer. Used at close to flush dirty
pages. The iterator is bounded by self.capacity() (Rule 2).