pub struct CacheBuilder<B, C, CM, O = DisabledOffload> { /* private fields */ }Expand description
Fluent builder for constructing a Cache layer.
Use Cache::builder() to create a new builder. Both backend()
and config() must be called before build().
§Type Parameters
The type parameters change as you call builder methods:
B- Backend type, set bybackend()C- Configuration type, set byconfig()CM- Concurrency manager type, set byconcurrency_manager()O- Offload type, set byoffload()
§Examples
use std::time::Duration;
use hitbox_tower::Cache;
use hitbox_moka::MokaBackend;
use hitbox::Config;
use hitbox::policy::PolicyConfig;
use hitbox_http::extractors::Method;
use hitbox_http::predicates::{NeutralRequestPredicate, NeutralResponsePredicate};
use http::header::HeaderName;
let config = Config::builder()
.request_predicate(NeutralRequestPredicate::new())
.response_predicate(NeutralResponsePredicate::new())
.extractor(Method::new())
.policy(PolicyConfig::builder().ttl(Duration::from_secs(300)).build())
.build();
let layer = Cache::builder()
.backend(MokaBackend::builder().max_entries(10_000).build())
.config(config)
.cache_status_header(HeaderName::from_static("x-custom-cache"))
.build();Implementations§
Source§impl CacheBuilder<NotSet, NotSet, NoopConcurrencyManager, DisabledOffload>
impl CacheBuilder<NotSet, NotSet, NoopConcurrencyManager, DisabledOffload>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new builder.
Prefer using Cache::builder() instead of calling this directly.
Source§impl<B, C, CM, O> CacheBuilder<B, C, CM, O>
impl<B, C, CM, O> CacheBuilder<B, C, CM, O>
Sourcepub fn backend<NB: CacheBackend>(
self,
backend: NB,
) -> CacheBuilder<NB, C, CM, O>
pub fn backend<NB: CacheBackend>( self, backend: NB, ) -> CacheBuilder<NB, C, CM, O>
Sets the cache backend for storing responses.
Common backends:
MokaBackend— In-memory cache (fromhitbox-moka)RedisBackend— Distributed cache via Redis (fromhitbox-redis)
§Examples
use hitbox_tower::Cache;
use hitbox_moka::MokaBackend;
let builder = Cache::builder()
.backend(MokaBackend::builder().max_entries(1000).build());Sourcepub fn config<NC>(self, configuration: NC) -> CacheBuilder<B, NC, CM, O>
pub fn config<NC>(self, configuration: NC) -> CacheBuilder<B, NC, CM, O>
Sets the cache configuration with predicates, extractors, and policy.
Use Config::builder() to create a configuration with:
- Request predicates (which requests to cache)
- Response predicates (which responses to cache)
- Extractors (how to generate cache keys)
- Policy (TTL, stale handling)
§Examples
use std::time::Duration;
use hitbox_tower::Cache;
use hitbox_moka::MokaBackend;
use hitbox::Config;
use hitbox::policy::PolicyConfig;
use hitbox_http::extractors::Method;
use hitbox_http::predicates::{NeutralRequestPredicate, NeutralResponsePredicate};
let config = Config::builder()
.request_predicate(NeutralRequestPredicate::new())
.response_predicate(NeutralResponsePredicate::new())
.extractor(Method::new())
.policy(PolicyConfig::builder().ttl(Duration::from_secs(60)).build())
.build();
let layer = Cache::builder()
.backend(MokaBackend::builder().max_entries(1000).build())
.config(config)
.build();Sourcepub fn concurrency_manager<NCM>(
self,
concurrency_manager: NCM,
) -> CacheBuilder<B, C, NCM, O>
pub fn concurrency_manager<NCM>( self, concurrency_manager: NCM, ) -> CacheBuilder<B, C, NCM, O>
Sets the concurrency manager for dogpile prevention.
The dogpile effect occurs when a cache entry expires and multiple concurrent requests all try to refresh it simultaneously. A concurrency manager prevents this by coordinating requests.
Options:
NoopConcurrencyManager— No coordination (default)BroadcastConcurrencyManager— One request fetches, others wait
Sourcepub fn offload<NO>(self, offload: NO) -> CacheBuilder<B, C, CM, NO>
pub fn offload<NO>(self, offload: NO) -> CacheBuilder<B, C, CM, NO>
Sets the offload strategy for background revalidation.
When serving stale content, the offload strategy determines how background refresh is performed.
Defaults to DisabledOffload (synchronous revalidation).
Sourcepub fn cache_status_header(self, header_name: HeaderName) -> Self
pub fn cache_status_header(self, header_name: HeaderName) -> Self
Sets the header name for cache status.
The cache status header indicates whether a response was served from cache.
Possible values are HIT, MISS, or STALE.
Defaults to DEFAULT_CACHE_STATUS_HEADER (x-cache-status).
§Examples
use hitbox_tower::Cache;
use hitbox_moka::MokaBackend;
use http::header::HeaderName;
let builder = Cache::builder()
.backend(MokaBackend::builder().max_entries(1000).build())
.cache_status_header(HeaderName::from_static("x-custom-cache"));