CacheMode

Enum CacheMode 

Source
pub enum CacheMode {
    Default,
    NoStore,
    Reload,
    NoCache,
    ForceCache,
    OnlyIfCached,
    IgnoreRules,
}
Expand description

Cache mode determines how the HTTP cache behaves for requests.

These modes are similar to make-fetch-happen cache options and provide fine-grained control over caching behavior.

§Examples

use http_cache::{CacheMode, HttpCache, CACacheManager, HttpCacheOptions};

let manager = CACacheManager::new("./cache".into(), true);

// Use different cache modes for different scenarios
let default_cache = HttpCache {
    mode: CacheMode::Default,        // Standard HTTP caching rules
    manager: manager.clone(),
    options: HttpCacheOptions::default(),
};

let force_cache = HttpCache {
    mode: CacheMode::ForceCache,     // Cache everything, ignore staleness
    manager: manager.clone(),
    options: HttpCacheOptions::default(),
};

let no_cache = HttpCache {
    mode: CacheMode::NoStore,        // Never cache anything
    manager,
    options: HttpCacheOptions::default(),
};

Variants§

§

Default

Standard HTTP caching behavior (recommended for most use cases).

This mode:

  • Checks the cache for fresh responses and uses them
  • Makes conditional requests for stale responses (revalidation)
  • Makes normal requests when no cached response exists
  • Updates the cache with new responses
  • Falls back to stale responses if revalidation fails

This is the most common mode and follows HTTP caching standards closely.

§

NoStore

Completely bypasses the cache.

This mode:

  • Never reads from the cache
  • Never writes to the cache
  • Always makes fresh network requests

Use this when you need to ensure every request goes to the origin server.

§

Reload

Bypasses cache on request but updates cache with response.

This mode:

  • Ignores any cached responses
  • Always makes a fresh network request
  • Updates the cache with the response

Equivalent to a “hard refresh” - useful when you know the cache is stale.

§

NoCache

Always revalidates cached responses.

This mode:

  • Makes conditional requests if a cached response exists
  • Makes normal requests if no cached response exists
  • Updates the cache with responses

Use this when you want to ensure content freshness while still benefiting from conditional requests (304 Not Modified responses).

§

ForceCache

Uses cached responses regardless of staleness.

This mode:

  • Uses any cached response, even if stale
  • Makes network requests only when no cached response exists
  • Updates the cache with new responses

Useful for offline scenarios or when performance is more important than freshness.

§

OnlyIfCached

Only serves from cache, never makes network requests.

This mode:

  • Uses any cached response, even if stale
  • Returns an error if no cached response exists
  • Never makes network requests

Use this for offline-only scenarios or when you want to guarantee no network traffic.

§

IgnoreRules

Ignores HTTP caching rules and caches everything.

This mode:

  • Caches all 200 responses regardless of cache-control headers
  • Uses cached responses regardless of staleness
  • Makes network requests when no cached response exists

Use this when you want aggressive caching and don’t want to respect server cache directives.

Trait Implementations§

Source§

impl Clone for CacheMode

Source§

fn clone(&self) -> CacheMode

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 CacheMode

Source§

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

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

impl Default for CacheMode

Source§

fn default() -> CacheMode

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

impl PartialEq for CacheMode

Source§

fn eq(&self, other: &CacheMode) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for CacheMode

Source§

impl Eq for CacheMode

Source§

impl StructuralPartialEq for CacheMode

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,