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§
impl Copy for CacheMode
impl Eq for CacheMode
impl StructuralPartialEq for CacheMode
Auto Trait Implementations§
impl Freeze for CacheMode
impl RefUnwindSafe for CacheMode
impl Send for CacheMode
impl Sync for CacheMode
impl Unpin for CacheMode
impl UnwindSafe for CacheMode
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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