Skip to content

Commit 2b81447

Browse files
committed
fixed metadata handling with bincode
1 parent c7acbe5 commit 2b81447

File tree

3 files changed

+100
-4
lines changed

3 files changed

+100
-4
lines changed

http-cache/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,10 @@ pub use error::{
436436
HttpCacheError, HttpCacheResult, Result, StreamingError,
437437
};
438438

439-
#[cfg(feature = "manager-cacache")]
439+
#[cfg(any(
440+
feature = "manager-cacache",
441+
feature = "manager-cacache-bincode"
442+
))]
440443
pub use managers::cacache::CACacheManager;
441444

442445
#[cfg(feature = "streaming")]

http-cache/src/managers/cacache.rs

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,84 @@ impl std::fmt::Debug for CACacheManager {
2020
}
2121
}
2222

23+
// Modern store format (postcard) - includes metadata field
24+
#[cfg(feature = "postcard")]
2325
#[derive(Debug, Deserialize, Serialize)]
2426
struct Store {
2527
response: HttpResponse,
2628
policy: CachePolicy,
2729
}
2830

31+
// Legacy store format (bincode) - HttpResponse without metadata field
32+
// The metadata field was added alongside postcard, so bincode cache
33+
// data will never contain it.
34+
#[cfg(all(feature = "bincode", not(feature = "postcard")))]
35+
#[derive(Debug, Deserialize, Serialize)]
36+
struct Store {
37+
response: LegacyHttpResponse,
38+
policy: CachePolicy,
39+
}
40+
41+
#[cfg(all(feature = "bincode", not(feature = "postcard")))]
42+
use crate::{HttpHeaders, HttpVersion, Url};
43+
44+
#[cfg(all(feature = "bincode", not(feature = "postcard")))]
45+
#[derive(Debug, Clone, Deserialize, Serialize)]
46+
struct LegacyHttpResponse {
47+
body: Vec<u8>,
48+
#[cfg(feature = "http-headers-compat")]
49+
headers: std::collections::HashMap<String, String>,
50+
#[cfg(not(feature = "http-headers-compat"))]
51+
headers: std::collections::HashMap<String, Vec<String>>,
52+
status: u16,
53+
url: Url,
54+
version: HttpVersion,
55+
}
56+
57+
#[cfg(all(feature = "bincode", not(feature = "postcard")))]
58+
impl From<LegacyHttpResponse> for HttpResponse {
59+
fn from(legacy: LegacyHttpResponse) -> Self {
60+
#[cfg(feature = "http-headers-compat")]
61+
let headers = HttpHeaders::Legacy(legacy.headers);
62+
#[cfg(not(feature = "http-headers-compat"))]
63+
let headers = HttpHeaders::Modern(legacy.headers);
64+
65+
HttpResponse {
66+
body: legacy.body,
67+
headers,
68+
status: legacy.status,
69+
url: legacy.url,
70+
version: legacy.version,
71+
metadata: None,
72+
}
73+
}
74+
}
75+
76+
#[cfg(all(feature = "bincode", not(feature = "postcard")))]
77+
impl From<HttpResponse> for LegacyHttpResponse {
78+
fn from(response: HttpResponse) -> Self {
79+
#[cfg(feature = "http-headers-compat")]
80+
let headers = match response.headers {
81+
HttpHeaders::Legacy(h) => h,
82+
HttpHeaders::Modern(h) => {
83+
h.into_iter().map(|(k, v)| (k, v.join(", "))).collect()
84+
}
85+
};
86+
#[cfg(not(feature = "http-headers-compat"))]
87+
let headers = match response.headers {
88+
HttpHeaders::Modern(h) => h,
89+
};
90+
91+
LegacyHttpResponse {
92+
body: response.body,
93+
headers,
94+
status: response.status,
95+
url: response.url,
96+
version: response.version,
97+
}
98+
}
99+
}
100+
29101
#[allow(dead_code)]
30102
impl CACacheManager {
31103
/// Creates a new [`CACacheManager`] with the given path.
@@ -64,7 +136,15 @@ impl CacheManager for CACacheManager {
64136
return Ok(None);
65137
}
66138
};
67-
Ok(Some((store.response, store.policy)))
139+
140+
#[cfg(feature = "postcard")]
141+
{
142+
Ok(Some((store.response, store.policy)))
143+
}
144+
#[cfg(all(feature = "bincode", not(feature = "postcard")))]
145+
{
146+
Ok(Some((store.response.into(), store.policy)))
147+
}
68148
}
69149

70150
async fn put(
@@ -73,13 +153,26 @@ impl CacheManager for CACacheManager {
73153
response: HttpResponse,
74154
policy: CachePolicy,
75155
) -> Result<HttpResponse> {
156+
#[cfg(feature = "postcard")]
76157
let data = Store { response, policy };
158+
#[cfg(all(feature = "bincode", not(feature = "postcard")))]
159+
let data = Store { response: response.into(), policy };
160+
77161
#[cfg(feature = "postcard")]
78162
let bytes = postcard::to_allocvec(&data)?;
79163
#[cfg(all(feature = "bincode", not(feature = "postcard")))]
80164
let bytes = bincode::serialize(&data)?;
165+
81166
cacache::write(&self.path, cache_key, bytes).await?;
82-
Ok(data.response)
167+
168+
#[cfg(feature = "postcard")]
169+
{
170+
Ok(data.response)
171+
}
172+
#[cfg(all(feature = "bincode", not(feature = "postcard")))]
173+
{
174+
Ok(data.response.into())
175+
}
83176
}
84177

85178
async fn delete(&self, cache_key: &str) -> Result<()> {

http-cache/src/managers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[cfg(feature = "manager-cacache")]
1+
#[cfg(any(feature = "manager-cacache", feature = "manager-cacache-bincode"))]
22
pub mod cacache;
33

44
#[cfg(feature = "manager-foyer")]

0 commit comments

Comments
 (0)