Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions http-cache-quickcache/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ async fn default_mode_with_options() -> Result<()> {
cache_bust: None,
cache_status_headers: true,
max_ttl: None,
modify_response: None,
},
}))
.build();
Expand Down
19 changes: 19 additions & 0 deletions http-cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,9 @@ pub type CacheBust = Arc<
+ Sync,
>;

/// A closure that takes a mutable reference to [`HttpResponse`] and modifies it before caching.
pub type ModifyResponse = Arc<dyn Fn(&mut HttpResponse) + Send + Sync>;

/// Configuration options for customizing HTTP cache behavior on a per-request basis.
///
/// This struct allows you to override default caching behavior for individual requests
Expand Down Expand Up @@ -1081,6 +1084,8 @@ pub struct HttpCacheOptions {
pub response_cache_mode_fn: Option<ResponseCacheModeFn>,
/// Bust the caches of the returned keys.
pub cache_bust: Option<CacheBust>,
/// Modifies the response before storing it in the cache.
pub modify_response: Option<ModifyResponse>,
/// Determines if the cache status headers should be added to the response.
pub cache_status_headers: bool,
/// Maximum time-to-live for cached responses.
Expand All @@ -1103,6 +1108,7 @@ impl Default for HttpCacheOptions {
cache_mode_fn: None,
response_cache_mode_fn: None,
cache_bust: None,
modify_response: None,
cache_status_headers: true,
max_ttl: None,
#[cfg(feature = "rate-limiting")]
Expand All @@ -1124,6 +1130,7 @@ impl Debug for HttpCacheOptions {
&"Fn(&request::Parts, &HttpResponse) -> Option<CacheMode>",
)
.field("cache_bust", &"Fn(&request::Parts) -> Vec<String>")
.field("modify_response", &"Fn(&mut ModifyResponse)")
.field("cache_status_headers", &self.cache_status_headers)
.field("max_ttl", &self.max_ttl)
.field("rate_limiter", &"Option<CacheAwareRateLimiter>")
Expand All @@ -1141,6 +1148,7 @@ impl Debug for HttpCacheOptions {
&"Fn(&request::Parts, &HttpResponse) -> Option<CacheMode>",
)
.field("cache_bust", &"Fn(&request::Parts) -> Vec<String>")
.field("modify_response", &"Fn(&mut ModifyResponse)")
.field("cache_status_headers", &self.cache_status_headers)
.field("max_ttl", &self.max_ttl)
.finish()
Expand Down Expand Up @@ -1238,6 +1246,13 @@ impl HttpCacheOptions {
original_mode
}

/// Modifies the response before caching if a modifier function is provided
fn modify_response_before_caching(&self, response: &mut HttpResponse) {
if let Some(modify_response) = &self.modify_response {
modify_response(response);
}
}

/// Creates a cache policy for the given request and response
fn create_cache_policy(
&self,
Expand Down Expand Up @@ -1600,6 +1615,7 @@ impl<T: CacheManager> HttpCache<T> {
);

if is_cacheable {
self.options.modify_response_before_caching(&mut res);
Ok(self
.manager
.put(self.options.create_cache_key(&parts, None), res, policy)
Expand Down Expand Up @@ -1676,6 +1692,8 @@ impl<T: CacheManager> HttpCache<T> {
cached_res.cache_status(HitOrMiss::HIT);
cached_res.cache_lookup_status(HitOrMiss::HIT);
}
self.options
.modify_response_before_caching(&mut cached_res);
let res = self
.manager
.put(
Expand All @@ -1695,6 +1713,7 @@ impl<T: CacheManager> HttpCache<T> {
cond_res.cache_status(HitOrMiss::MISS);
cond_res.cache_lookup_status(HitOrMiss::HIT);
}
self.options.modify_response_before_caching(&mut cond_res);
let res = self
.manager
.put(
Expand Down
Loading