ChunkCache

Struct ChunkCache 

Source
pub struct ChunkCache { /* private fields */ }
Expand description

Chunk cache manager.

Manages multiple chunked entries, providing efficient lookup and storage of large files split into chunks.

Implementations§

Source§

impl ChunkCache

Source

pub fn new(default_chunk_size: usize) -> Self

Creates a new chunk cache with the given default chunk size.

§Arguments
  • default_chunk_size - Default size of each chunk in bytes
§Examples
use tower_http_cache::chunks::ChunkCache;

// Create cache with 1MB chunks
let cache = ChunkCache::new(1024 * 1024);
Source

pub fn get_or_create( &self, key: String, metadata: ChunkMetadata, ) -> Arc<ChunkedEntry>

Gets or creates a chunked entry.

If an entry exists for the key, returns it. Otherwise, creates a new entry with the given metadata.

§Arguments
  • key - Cache key
  • metadata - File metadata (used only if creating new entry)
§Examples
let cache = ChunkCache::new(1024 * 1024);

let metadata = ChunkMetadata {
    total_size: 10_000_000,
    content_type: "video/mp4".to_string(),
    etag: None,
    last_modified: None,
    status: StatusCode::OK,
    version: http::Version::HTTP_11,
    headers: vec![],
};

let entry = cache.get_or_create("video.mp4".to_string(), metadata);
Source

pub fn get(&self, key: &str) -> Option<Arc<ChunkedEntry>>

Gets an entry if it exists.

§Arguments
  • key - Cache key
§Examples
let cache = ChunkCache::new(1024 * 1024);

assert!(cache.get("nonexistent").is_none());

cache.get_or_create("exists".to_string(), metadata);
assert!(cache.get("exists").is_some());
Source

pub fn remove(&self, key: &str) -> Option<Arc<ChunkedEntry>>

Removes an entry from the cache.

§Arguments
  • key - Cache key
§Examples
let cache = ChunkCache::new(1024 * 1024);

cache.get_or_create("temp".to_string(), metadata);
assert!(cache.get("temp").is_some());

cache.remove("temp");
assert!(cache.get("temp").is_none());
Source

pub fn stats(&self) -> ChunkCacheStats

Gets cache statistics.

§Examples
let cache = ChunkCache::new(1024);

let entry = cache.get_or_create("file1".to_string(), metadata);
entry.add_chunk(0, Bytes::from(vec![0u8; 1024]));

let stats = cache.stats();
assert_eq!(stats.total_entries, 1);
assert_eq!(stats.total_chunks, 1);
assert_eq!(stats.total_bytes, 1024);

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.