hitbox_core/request.rs
1//! Cacheable request types and traits.
2//!
3//! This module provides types for determining whether requests should be
4//! cached and extracting cache keys from them:
5//!
6//! - [`CacheableRequest`] - Trait for request types that can participate in caching
7//! - [`CacheablePolicyData`] - Request bundled with its cache key
8//! - [`RequestCachePolicy`] - Type alias for request cache decisions
9//!
10//! ## Request Processing Flow
11//!
12//! When a request is processed:
13//!
14//! 1. **Predicates** evaluate whether the request should be cached
15//! 2. **Extractors** generate the cache key from request components
16//! 3. The result is either `Cacheable` (with key) or `NonCacheable`
17
18use std::future::Future;
19
20use crate::{CacheKey, CachePolicy, extractor::Extractor, predicate::Predicate};
21
22/// A cacheable request bundled with its generated cache key.
23///
24/// Created when a request passes predicate evaluation and has its
25/// cache key extracted. Contains both the original request and the
26/// key used for cache lookup/storage.
27pub struct CacheablePolicyData<T> {
28 /// The generated cache key for this request.
29 pub key: CacheKey,
30 /// The original request.
31 pub request: T,
32}
33
34impl<T> CacheablePolicyData<T> {
35 /// Creates a new cacheable policy data with the given key and request.
36 pub fn new(key: CacheKey, request: T) -> Self {
37 CacheablePolicyData { key, request }
38 }
39}
40
41/// Cache policy for requests.
42///
43/// Type alias that specializes [`CachePolicy`] for request caching:
44/// - `Cacheable` variant contains [`CacheablePolicyData`] with the request and its key
45/// - `NonCacheable` variant contains the original request
46pub type RequestCachePolicy<T> = CachePolicy<CacheablePolicyData<T>, T>;
47
48/// Trait for request types that can participate in caching.
49///
50/// Implementations determine whether a request should be cached by
51/// applying predicates and extracting cache keys.
52///
53/// # Type Requirements
54///
55/// Request types must be `Sized` to allow ownership transfer through
56/// the caching pipeline.
57///
58/// # Processing
59///
60/// The `cache_policy` method:
61/// 1. Applies predicates to determine if the request is cacheable
62/// 2. If cacheable, extracts a cache key using the provided extractors
63/// 3. Returns either `Cacheable` with the key or `NonCacheable`
64pub trait CacheableRequest: Sized {
65 /// Determine if this request should be cached and extract its key.
66 ///
67 /// # Arguments
68 ///
69 /// * `predicates` - Predicates to evaluate whether the request is cacheable
70 /// * `extractors` - Extractors to generate the cache key
71 fn cache_policy<P, E>(
72 self,
73 predicates: P,
74 extractors: E,
75 ) -> impl Future<Output = RequestCachePolicy<Self>> + Send
76 where
77 P: Predicate<Subject = Self> + Send + Sync,
78 E: Extractor<Subject = Self> + Send + Sync;
79}