StreamingPolicy

Struct StreamingPolicy 

Source
pub struct StreamingPolicy {
    pub enabled: bool,
    pub max_cacheable_size: Option<usize>,
    pub excluded_content_types: HashSet<String>,
    pub force_cache_content_types: HashSet<String>,
    pub stream_threshold: usize,
    pub range_handling: RangeHandling,
    pub enable_chunk_cache: bool,
    pub chunk_size: usize,
    pub min_chunk_file_size: u64,
}
Expand description

Configuration for smart streaming and large file handling.

The streaming policy allows you to configure how the cache handles large response bodies and specific content types. This prevents memory exhaustion and cache pollution from large files.

§Examples

use tower_http_cache::streaming::StreamingPolicy;
use tower_http_cache::range::RangeHandling;
use std::collections::HashSet;

let policy = StreamingPolicy {
    enabled: true,
    max_cacheable_size: Some(1024 * 1024), // 1MB
    excluded_content_types: HashSet::from([
        "application/pdf".to_string(),
        "video/*".to_string(),
    ]),
    range_handling: RangeHandling::PassThrough,
    ..Default::default()
};

Fields§

§enabled: bool

Enable smart streaming (default: true)

§max_cacheable_size: Option<usize>

Skip caching bodies larger than this (default: 1MB) Set to None to disable size-based filtering

§excluded_content_types: HashSet<String>

Content types to never cache (PDFs, videos, archives, etc.) Supports wildcards like “video/*”

§force_cache_content_types: HashSet<String>

Always cache these content types regardless of size Useful for API responses that should always be cached

§stream_threshold: usize

Use streaming for bodies above this size (default: 512KB) Currently used for decision making; actual streaming not yet implemented

§range_handling: RangeHandling

How to handle HTTP Range requests (default: PassThrough)

§enable_chunk_cache: bool

Enable chunk caching for large files (default: false, opt-in)

When enabled, large files are split into chunks and cached separately, allowing efficient range request handling without caching the entire file.

§chunk_size: usize

Chunk size for large files (default: 1MB)

Files are split into chunks of this size when chunk caching is enabled. Larger chunks use less memory overhead but may waste bandwidth for small range requests. Smaller chunks are more granular but have more overhead.

§min_chunk_file_size: u64

Minimum file size for chunking (default: 10MB)

Files smaller than this will not be chunked even if chunk caching is enabled. This prevents overhead for files that don’t benefit from chunking.

Implementations§

Source§

impl StreamingPolicy

Source

pub fn disabled() -> Self

Creates a new streaming policy with all features disabled. Useful for gradually migrating existing code.

Source

pub fn size_only(max_size: usize) -> Self

Creates a streaming policy that only filters by size.

Source

pub fn content_type_only(excluded: HashSet<String>) -> Self

Creates a streaming policy that only filters by content type.

Trait Implementations§

Source§

impl Clone for StreamingPolicy

Source§

fn clone(&self) -> StreamingPolicy

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for StreamingPolicy

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for StreamingPolicy

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.