pub struct TileCache { /* private fields */ }Expand description
LRU cache for encoded JPEG tiles with size-based capacity.
This cache stores encoded tile data and evicts least-recently-used entries when the total cached size exceeds capacity.
§Thread Safety
The cache is thread-safe and can be shared across async tasks via Arc.
§Example
use wsi_streamer::tile::{TileCache, TileCacheKey};
use bytes::Bytes;
use std::sync::Arc;
#[tokio::main]
async fn main() {
let cache = TileCache::new();
let key = TileCacheKey::new("slides/sample.svs", 0, 1, 2, 80);
let tile_data = Bytes::from(vec![0xFF, 0xD8, 0xFF, 0xE0]); // JPEG header
// Store tile
cache.put(key.clone(), tile_data.clone()).await;
// Retrieve tile
let cached = cache.get(&key).await;
assert_eq!(cached, Some(tile_data));
}Implementations§
Source§impl TileCache
impl TileCache
Sourcepub fn with_capacity(max_size: usize) -> Self
pub fn with_capacity(max_size: usize) -> Self
Create a new tile cache with the specified capacity in bytes.
§Arguments
max_size- Maximum total size of cached tiles in bytes
Sourcepub fn with_capacity_and_entries(max_size: usize, max_entries: usize) -> Self
pub fn with_capacity_and_entries(max_size: usize, max_entries: usize) -> Self
Create a new tile cache with specified capacity and maximum entries.
§Arguments
max_size- Maximum total size of cached tiles in bytesmax_entries- Maximum number of entries in the cache
Sourcepub async fn get(&self, key: &TileCacheKey) -> Option<Bytes>
pub async fn get(&self, key: &TileCacheKey) -> Option<Bytes>
Get a tile from the cache.
Returns Some(data) if the tile is cached, None otherwise.
This operation marks the entry as recently used.
Sourcepub async fn contains(&self, key: &TileCacheKey) -> bool
pub async fn contains(&self, key: &TileCacheKey) -> bool
Check if a tile is in the cache without updating LRU order.
Returns true if the tile is cached, false otherwise.
Sourcepub async fn put(&self, key: TileCacheKey, data: Bytes)
pub async fn put(&self, key: TileCacheKey, data: Bytes)
Store a tile in the cache.
If the cache is over capacity after insertion, least-recently-used entries are evicted until the cache is within capacity.
If the tile already exists, it is updated and marked as recently used.
Sourcepub async fn remove(&self, key: &TileCacheKey) -> Option<Bytes>
pub async fn remove(&self, key: &TileCacheKey) -> Option<Bytes>
Remove a tile from the cache.
Returns the cached data if it existed, None otherwise.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for TileCache
impl !RefUnwindSafe for TileCache
impl Send for TileCache
impl Sync for TileCache
impl Unpin for TileCache
impl !UnwindSafe for TileCache
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
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