@@ -959,6 +959,9 @@ pub type CacheBust = Arc<
959959 + Sync ,
960960> ;
961961
962+ /// A closure that takes a mutable reference to [`HttpResponse`] and modifies it before caching.
963+ pub type ModifyResponse = Arc < dyn Fn ( & mut HttpResponse ) + Send + Sync > ;
964+
962965/// Configuration options for customizing HTTP cache behavior on a per-request basis.
963966///
964967/// This struct allows you to override default caching behavior for individual requests
@@ -1081,6 +1084,8 @@ pub struct HttpCacheOptions {
10811084 pub response_cache_mode_fn : Option < ResponseCacheModeFn > ,
10821085 /// Bust the caches of the returned keys.
10831086 pub cache_bust : Option < CacheBust > ,
1087+ /// Modifies the response before storing it in the cache.
1088+ pub modify_response : Option < ModifyResponse > ,
10841089 /// Determines if the cache status headers should be added to the response.
10851090 pub cache_status_headers : bool ,
10861091 /// Maximum time-to-live for cached responses.
@@ -1103,6 +1108,7 @@ impl Default for HttpCacheOptions {
11031108 cache_mode_fn : None ,
11041109 response_cache_mode_fn : None ,
11051110 cache_bust : None ,
1111+ modify_response : None ,
11061112 cache_status_headers : true ,
11071113 max_ttl : None ,
11081114 #[ cfg( feature = "rate-limiting" ) ]
@@ -1124,6 +1130,7 @@ impl Debug for HttpCacheOptions {
11241130 & "Fn(&request::Parts, &HttpResponse) -> Option<CacheMode>" ,
11251131 )
11261132 . field ( "cache_bust" , & "Fn(&request::Parts) -> Vec<String>" )
1133+ . field ( "modify_response" , & "Fn(&mut ModifyResponse)" )
11271134 . field ( "cache_status_headers" , & self . cache_status_headers )
11281135 . field ( "max_ttl" , & self . max_ttl )
11291136 . field ( "rate_limiter" , & "Option<CacheAwareRateLimiter>" )
@@ -1141,6 +1148,7 @@ impl Debug for HttpCacheOptions {
11411148 & "Fn(&request::Parts, &HttpResponse) -> Option<CacheMode>" ,
11421149 )
11431150 . field ( "cache_bust" , & "Fn(&request::Parts) -> Vec<String>" )
1151+ . field ( "modify_response" , & "Fn(&mut ModifyResponse)" )
11441152 . field ( "cache_status_headers" , & self . cache_status_headers )
11451153 . field ( "max_ttl" , & self . max_ttl )
11461154 . finish ( )
@@ -1238,6 +1246,13 @@ impl HttpCacheOptions {
12381246 original_mode
12391247 }
12401248
1249+ /// Modifies the response before caching if a modifier function is provided
1250+ fn modify_response_before_caching ( & self , response : & mut HttpResponse ) {
1251+ if let Some ( modify_response) = & self . modify_response {
1252+ modify_response ( response) ;
1253+ }
1254+ }
1255+
12411256 /// Creates a cache policy for the given request and response
12421257 fn create_cache_policy (
12431258 & self ,
@@ -1600,6 +1615,7 @@ impl<T: CacheManager> HttpCache<T> {
16001615 ) ;
16011616
16021617 if is_cacheable {
1618+ self . options . modify_response_before_caching ( & mut res) ;
16031619 Ok ( self
16041620 . manager
16051621 . put ( self . options . create_cache_key ( & parts, None ) , res, policy)
@@ -1676,6 +1692,8 @@ impl<T: CacheManager> HttpCache<T> {
16761692 cached_res. cache_status ( HitOrMiss :: HIT ) ;
16771693 cached_res. cache_lookup_status ( HitOrMiss :: HIT ) ;
16781694 }
1695+ self . options
1696+ . modify_response_before_caching ( & mut cached_res) ;
16791697 let res = self
16801698 . manager
16811699 . put (
@@ -1695,6 +1713,7 @@ impl<T: CacheManager> HttpCache<T> {
16951713 cond_res. cache_status ( HitOrMiss :: MISS ) ;
16961714 cond_res. cache_lookup_status ( HitOrMiss :: HIT ) ;
16971715 }
1716+ self . options . modify_response_before_caching ( & mut cond_res) ;
16981717 let res = self
16991718 . manager
17001719 . put (
0 commit comments