Skip to content

Commit

Permalink
Minor improvement cache avoid clone (#479)
Browse files Browse the repository at this point in the history
Avoid cloning to serialize to JSON; instead, dereference the object, as Serde
needs a reference to the object.
  • Loading branch information
crodas authored Nov 30, 2024
1 parent af2fe58 commit 7d15587
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
10 changes: 6 additions & 4 deletions crates/cdk-axum/src/router_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ macro_rules! post_cache_wrapper {
state: State<MintState>,
payload: Json<$request_type>
) -> Result<Json<$response_type>, Response> {
let Json(json_extracted_payload) = payload.clone();
use std::ops::Deref;

let json_extracted_payload = payload.deref();
let State(mint_state) = state.clone();
let cache_key = serde_json::to_string(&json_extracted_payload).map_err(|err| {
into_response(Error::from(err))
Expand All @@ -37,11 +39,11 @@ macro_rules! post_cache_wrapper {
.expect("Shouldn't panic: response is json-deserializable.")));
}

let Json(response) = $handler(state, payload).await?;
mint_state.cache.insert(cache_key, serde_json::to_string(&response)
let response = $handler(state, payload).await?;
mint_state.cache.insert(cache_key, serde_json::to_string(response.deref())
.expect("Shouldn't panic: response is json-serializable.")
).await;
Ok(Json(response))
Ok(response)
}
}
};
Expand Down
7 changes: 1 addition & 6 deletions crates/cdk-axum/src/ws/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ impl WsHandle for Method {
return Err(WsError::InvalidParams);
}

let mut subscription = context
.state
.mint
.pubsub_manager
.subscribe(self.0.clone())
.await;
let mut subscription = context.state.mint.pubsub_manager.subscribe(self.0).await;
let publisher = context.publisher.clone();
context.subscriptions.insert(
sub_id.clone(),
Expand Down

0 comments on commit 7d15587

Please sign in to comment.