-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ add automatic typed json ser/de operations #142
Open
Shahab96
wants to merge
3
commits into
aisk:master
Choose a base branch
from
Shahab96:feat/json
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ use crate::stream::Stream; | |
use crate::value::{FromMemcacheValueExt, ToMemcacheValue}; | ||
use r2d2::Pool; | ||
|
||
#[cfg(feature = "json")] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
pub type Stats = HashMap<String, String>; | ||
|
||
pub trait Connectable { | ||
|
@@ -266,6 +269,47 @@ impl Client { | |
return Ok(result); | ||
} | ||
|
||
/// Get a key from memcached server and deserialize it as JSON. This function is only available when the `json` feature is enabled. | ||
/// The value type should implement `FromMemcacheValueExt` and `serde::Deserialize`. | ||
/// | ||
/// Example: | ||
/// ```rust | ||
/// use serde::{Serialize, Deserialize}; | ||
/// | ||
/// #[derive(Debug, Serialize, Deserialize)] | ||
/// struct MyStruct { | ||
/// value: String, | ||
/// number: i32, | ||
/// flag: bool, | ||
/// array: Vec<i32>, | ||
/// } | ||
/// | ||
/// let client = memcache::Client::connect("memcache://localhost:12345").unwrap(); | ||
/// let value = MyStruct { | ||
/// value: "hello".to_string(), | ||
/// number: 42, | ||
/// flag: true, | ||
/// array: vec![1, 2, 3], | ||
/// }; | ||
/// client.set_json("foo", value, 10).unwrap(); | ||
/// let result: MyStruct = client.get_json("foo").unwrap().unwrap(); | ||
/// | ||
/// assert_eq!(result.value, "hello"); | ||
/// assert_eq!(result.number, 42); | ||
/// assert_eq!(result.flag, true); | ||
/// assert_eq!(result.array, vec![1, 2, 3]); | ||
/// ``` | ||
#[cfg(feature = "json")] | ||
pub fn get_json<V: for<'a> Deserialize<'a>>(&self, key: &str) -> Result<Option<V>, MemcacheError> { | ||
let value: Option<String> = self.get(key)?; | ||
let value = match value { | ||
Some(value) => value, | ||
None => return Ok(None), | ||
}; | ||
|
||
return serde_json::from_str(&value).map_err(|e| MemcacheError::JsonError(e)); | ||
} | ||
|
||
/// Set a key with associate value into memcached server with expiration seconds. | ||
/// | ||
/// Example: | ||
|
@@ -280,6 +324,32 @@ impl Client { | |
return self.get_connection(key).get()?.set(key, value, expiration); | ||
} | ||
|
||
/// Set a key with associate value into memcached server with expiration seconds. The value will be serialized as JSON. This function is only available when the `json` feature is enabled. | ||
/// | ||
/// Example: | ||
/// ```rust | ||
/// use serde::Serialize; | ||
/// | ||
/// #[derive(Serialize)] | ||
/// struct MyStruct { | ||
/// value: String, | ||
/// number: i32, | ||
/// } | ||
/// | ||
/// let client = memcache::Client::connect("memcache://localhost:12345").unwrap(); | ||
/// let value = MyStruct { | ||
/// value: "hello".to_string(), | ||
/// number: 42, | ||
/// }; | ||
/// client.set_json("foo", value, 10).unwrap(); | ||
/// client.flush().unwrap(); | ||
/// ``` | ||
#[cfg(feature = "json")] | ||
pub fn set_json<V: Serialize>(&self, key: &str, value: V, expiration: u32) -> Result<(), MemcacheError> { | ||
let value = serde_json::to_string(&value).map_err(|e| MemcacheError::JsonError(e))?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By doing it this way we can leverage the existing |
||
return self.set(key, value, expiration); | ||
} | ||
|
||
/// Compare and swap a key with the associate value into memcached server with expiration seconds. | ||
/// `cas_id` should be obtained from a previous `gets` call. | ||
/// | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This tests the
set_json
function below as well, ensuring that the value is actually set correctly