expire command

This commit is contained in:
2025-06-17 21:00:40 +02:00
parent 8ac4dac2f0
commit c51c90b597
5 changed files with 113 additions and 0 deletions

37
src/commands/expire.rs Normal file
View File

@@ -0,0 +1,37 @@
use std::io::Cursor;
use bytes::{Buf as _, Bytes};
use crate::{Result, connection::Connection, database::Database, errors::AppError};
#[derive(Debug, Clone)]
pub struct Expire {
key: String,
seconds: u64,
}
impl Expire {
pub async fn execute(self, db: &Database, connection: &mut Connection) -> Result<()> {
let value = db.expire(&self.key, self.seconds).await?;
connection
.write(Bytes::from_static(if value { &[1] } else { &[0] }))
.await?;
Ok(())
}
pub fn parse(bytes: &mut Cursor<&[u8]>) -> Result<Self> {
let key_length = bytes.try_get_u16()? as usize;
if bytes.remaining() < key_length {
return Err(AppError::IncompleteCommandBuffer);
}
let key = String::from_utf8(bytes.copy_to_bytes(key_length).to_vec())?;
let seconds = bytes.try_get_u64()?;
Ok(Self { key, seconds })
}
}