read expiration from Set command + tests

This commit is contained in:
2025-06-17 01:52:11 +02:00
parent 28b42c786c
commit 20e3fbd5d3
8 changed files with 119 additions and 41 deletions

View File

@@ -1,7 +1,7 @@
use bytes::{Buf, BufMut as _, Bytes, BytesMut};
use tokio::net::{TcpStream, ToSocketAddrs};
use crate::{Result, connection::Connection, database::Value, errors::AppError};
use crate::{Result, connection::Connection, errors::AppError};
pub struct Client {
connection: Connection,
@@ -50,7 +50,12 @@ impl Client {
Ok(response)
}
pub async fn set(&mut self, key: &str, value: Value) -> Result<()> {
pub async fn set(
&mut self,
key: &str,
data: &[u8],
expiration_secs: Option<u64>,
) -> Result<()> {
let mut bytes = BytesMut::new();
bytes.put_u16(3);
@@ -64,7 +69,18 @@ impl Client {
bytes.put_u16(key_length);
bytes.put_slice(key.as_bytes());
value.write_to_bytes(&mut bytes);
bytes.put_u32(data.len() as u32);
bytes.put_slice(data);
match expiration_secs {
Some(seconds) => {
bytes.put_u8(1);
bytes.put_u64(seconds);
}
None => {
bytes.put_u8(0);
}
}
self.connection.write(bytes.into()).await?;