implement client set (#11)
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
use crate::Connection;
|
||||
use crate::{
|
||||
cmd::{
|
||||
utils::{bytes_from_str, duration_from_ms_str},
|
||||
Set,
|
||||
},
|
||||
frame::Frame,
|
||||
Command, Connection,
|
||||
};
|
||||
|
||||
use bytes::Bytes;
|
||||
use std::io;
|
||||
use std::io::{Error, ErrorKind};
|
||||
use tokio::net::{TcpStream, ToSocketAddrs};
|
||||
|
||||
/// Mini asynchronous Redis client
|
||||
@@ -9,7 +16,7 @@ pub struct Client {
|
||||
conn: Connection,
|
||||
}
|
||||
|
||||
pub async fn connect<T: ToSocketAddrs>(addr: T) -> io::Result<Client> {
|
||||
pub async fn connect<T: ToSocketAddrs>(addr: T) -> Result<Client, Box<dyn std::error::Error>> {
|
||||
let socket = TcpStream::connect(addr).await?;
|
||||
let conn = Connection::new(socket);
|
||||
|
||||
@@ -17,11 +24,53 @@ pub async fn connect<T: ToSocketAddrs>(addr: T) -> io::Result<Client> {
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub async fn get(&mut self, key: &str) -> io::Result<Option<Bytes>> {
|
||||
pub async fn get(&mut self, key: &str) -> Result<Option<Bytes>, Box<dyn std::error::Error>> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
pub async fn set(&mut self, key: &str, val: Bytes) -> io::Result<()> {
|
||||
unimplemented!();
|
||||
pub async fn set(&mut self, key: &str, value: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let opts = Set {
|
||||
key: key.to_string(),
|
||||
value: bytes_from_str(value),
|
||||
expire: None,
|
||||
};
|
||||
self.set_with_opts(opts).await
|
||||
}
|
||||
|
||||
pub async fn set_with_expiration(
|
||||
&mut self,
|
||||
key: &str,
|
||||
value: &str,
|
||||
expiration: &str,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let opts = Set {
|
||||
key: key.to_string(),
|
||||
value: bytes_from_str(value),
|
||||
expire: Some(duration_from_ms_str(expiration)?),
|
||||
};
|
||||
self.set_with_opts(opts).await
|
||||
}
|
||||
|
||||
pub async fn set_with_opts(&mut self, opts: Set) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let frame = Command::Set(opts).into_frame()?;
|
||||
self.conn.write_frame(&frame).await?;
|
||||
let response = self.conn.read_frame().await?;
|
||||
if let Some(response) = response {
|
||||
match response {
|
||||
Frame::Simple(response) => {
|
||||
if response == "OK" {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("unexpected response from server".into())
|
||||
}
|
||||
}
|
||||
_ => Err("unexpected response from server".into()),
|
||||
}
|
||||
} else {
|
||||
Err(Box::new(Error::new(
|
||||
ErrorKind::ConnectionReset,
|
||||
"connection reset by server",
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user