use super::super::super::{cache::*, configuration::*, key::*, response::*};
use {
http::*,
http_body::*,
kutil::{
http::transcoding::*,
std::{error::*, immutable::*},
transcoding::*,
},
};
#[allow(async_fn_in_trait)]
pub trait ToTranscodingResponse {
async fn to_transcoding_response<ResponseBodyT, CacheT, CacheKeyT>(
self,
encoding: &Encoding,
is_new: bool,
cache: CacheT,
key: CacheKeyT,
configuration: &EncodingConfiguration,
) -> Response<TranscodingBody<ResponseBodyT>>
where
ResponseBodyT: 'static + Body + From<ImmutableBytes> + Send + Unpin,
ResponseBodyT::Data: From<ImmutableBytes> + Send,
ResponseBodyT::Error: Into<CapturedError>,
CacheT: Cache<CacheKeyT>,
CacheKeyT: CacheKey;
}
impl ToTranscodingResponse for CachedResponseRef {
async fn to_transcoding_response<ResponseBodyT, CacheT, CacheKeyT>(
self,
encoding: &Encoding,
is_new: bool,
cache: CacheT,
key: CacheKeyT,
configuration: &EncodingConfiguration,
) -> Response<TranscodingBody<ResponseBodyT>>
where
ResponseBodyT: 'static + Body + From<ImmutableBytes> + Send + Unpin,
ResponseBodyT::Data: From<ImmutableBytes>,
ResponseBodyT::Error: Into<CapturedError>,
CacheT: Cache<CacheKeyT>,
CacheKeyT: CacheKey,
{
match self.to_response(&encoding, configuration).await {
Ok((response, modified)) => {
if is_new {
cache.put(key, self).await;
} else if let Some(modified) = modified {
assert!(!is_new);
cache.put(key, modified.into()).await;
}
response
}
Err(error) => {
tracing::error!("could not create response from cache: {} {}", key, error);
error_transcoding_response()
}
}
}
}