tower_etag_cache/
cache_provider.rs

1use http::HeaderMap;
2use tower_service::Service;
3
4/// Struct returned by a [`CacheProvider`]'s first cache-lookup `Service`
5#[derive(Debug)]
6pub struct CacheGetResponse<ReqBody, Key> {
7    /// The original request passed on so that it can be processed by the inner service
8    pub req: http::Request<ReqBody>,
9    pub result: CacheGetResponseResult<Key>,
10}
11
12/// Result of the cache-lookup `Service`
13///
14/// Either
15/// - calculated cache key if entry not in cache, so that the key can be used to put later on
16/// - HTTP response headers to send along with the HTTP 304 response if entry in cache
17#[derive(Debug, Clone)]
18pub enum CacheGetResponseResult<Key> {
19    Miss(Key),
20    Hit(HeaderMap),
21}
22
23/// Typical type args for use in axum 0.6:
24///
25/// ```ignore
26/// ReqBody = hyper::body::Body
27/// ResBody = axum::body::BoxBody
28/// ```
29pub trait CacheProvider<ReqBody, ResBody>:
30    Service<http::Request<ReqBody>, Response = CacheGetResponse<ReqBody, Self::Key>> // Get
31    + Service<(Self::Key, http::Response<ResBody>), Response = http::Response<Self::TResBody>> // Put
32{
33    type Key;
34    type TResBody;
35}