pub struct Cache<B, C, CM, O = DisabledOffload> {
pub backend: Arc<B>,
pub configuration: C,
pub offload: O,
pub concurrency_manager: CM,
pub cache_status_header: HeaderName,
}Expand description
Tower Layer that adds HTTP caching to a service.
Cache wraps any Tower service with caching behavior. When a request arrives,
the layer evaluates predicates to determine cacheability, generates a cache key
using extractors, and either returns a cached response or forwards to the
upstream service.
§Type Parameters
B- Cache backend (e.g.,MokaBackend,RedisBackend). Must implementCacheBackend.C- Configuration providing predicates, extractors, and policy. Usehitbox::Configto build custom configuration.CM- Concurrency manager for dogpile prevention. UseNoopConcurrencyManagerto disable orBroadcastConcurrencyManagerto enable.O- Offload strategy for background revalidation. UseDisabledOffloadfor synchronous behavior.
§Examples
Create with the builder pattern:
use hitbox_tower::Cache;
use hitbox_moka::MokaBackend;
use hitbox::Config;
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())
.build();
let cache_layer = Cache::builder()
.backend(MokaBackend::builder().max_entries(1000).build())
.config(config)
.build();Fields§
§backend: Arc<B>The cache backend for storing and retrieving responses.
configuration: CConfiguration with predicates, extractors, and cache policy.
offload: OOffload strategy for background tasks.
concurrency_manager: CMConcurrency manager for dogpile prevention.
cache_status_header: HeaderNameHeader name for cache status (HIT/MISS/STALE).
Implementations§
Source§impl Cache<NotSet, NotSet, NoopConcurrencyManager, DisabledOffload>
impl Cache<NotSet, NotSet, NoopConcurrencyManager, DisabledOffload>
Sourcepub fn builder() -> CacheBuilder<NotSet, NotSet, NoopConcurrencyManager, DisabledOffload>
pub fn builder() -> CacheBuilder<NotSet, NotSet, NoopConcurrencyManager, DisabledOffload>
Creates a new CacheBuilder.
Both backend() and config()
must be called before build().
§Examples
use hitbox_tower::Cache;
use hitbox_moka::MokaBackend;
use hitbox::Config;
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())
.build();
let cache_layer = Cache::builder()
.backend(MokaBackend::builder().max_entries(1000).build())
.config(config)
.build();Trait Implementations§
Auto Trait Implementations§
impl<B, C, CM, O = DisabledOffload> !Freeze for Cache<B, C, CM, O>
impl<B, C, CM, O> RefUnwindSafe for Cache<B, C, CM, O>
impl<B, C, CM, O> Send for Cache<B, C, CM, O>
impl<B, C, CM, O> Sync for Cache<B, C, CM, O>
impl<B, C, CM, O> Unpin for Cache<B, C, CM, O>
impl<B, C, CM, O> UnwindSafe for Cache<B, C, CM, O>
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
Mutably borrows from an owned value. Read more