-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a39b21
commit 118fb52
Showing
6 changed files
with
95 additions
and
15 deletions.
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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
mod raw_client; | ||
|
||
#[allow(clippy::all)] | ||
#[rustfmt::skip] | ||
pub mod pb; | ||
|
||
pub use raw_client::RawClient; | ||
|
||
#[cfg(test)] | ||
mod tests {} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use tonic::{transport::Channel, Status}; | ||
|
||
use crate::pb::{cache_rpc_client::CacheRpcClient, GetRequest, SetRequest}; | ||
|
||
type StdError = Box<dyn std::error::Error + Send + Sync + 'static>; | ||
|
||
pub struct RawClient { | ||
inner: CacheRpcClient<Channel>, | ||
} | ||
|
||
impl RawClient { | ||
pub async fn connect<D>(endpoint: D) -> Result<Self, tonic::transport::Error> | ||
where | ||
D: TryInto<tonic::transport::Endpoint>, | ||
D::Error: Into<StdError>, | ||
{ | ||
let inner = CacheRpcClient::connect(endpoint).await?; | ||
Ok(Self { inner }) | ||
} | ||
pub async fn get(&self, key: impl Into<String>) -> Result<Option<Vec<u8>>, Status> { | ||
let key = key.into(); | ||
let msg = GetRequest { key }; | ||
let req = tonic::Request::new(msg); | ||
let resp = self.inner.clone().get(req).await?; | ||
Ok(resp.into_inner().value) | ||
} | ||
pub async fn set(&self, key: impl Into<String>, value: Vec<u8>) -> Result<(), Status> { | ||
let key = key.into(); | ||
let msg = SetRequest { key, value }; | ||
let req = tonic::Request::new(msg); | ||
self.inner.clone().set(req).await?; | ||
Ok(()) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use memcrab::RawClient; | ||
|
||
async fn _test_raw_client() -> anyhow::Result<()> { | ||
let addr = "http://[::1]:50051"; | ||
let client = RawClient::connect(addr).await?; | ||
|
||
client.set("age", vec![0, 21]).await?; | ||
client.set("year", "2024".into()).await?; | ||
|
||
let name = client.get("name").await?; | ||
match name { | ||
Some(val) => println!("got {:?} from cache", val), | ||
None => println!("cache miss for name"), | ||
} | ||
Ok(()) | ||
} |
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