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

View File

@@ -170,6 +170,34 @@ impl Client {
Ok(ttl)
}
pub async fn expire(&mut self, key: &str, seconds: u64) -> Result<bool> {
let mut bytes = BytesMut::new();
bytes.put_u16(6);
bytes.put_slice(b"expire");
let key_length: u16 = key
.len()
.try_into()
.map_err(|_| AppError::KeyLength(key.len()))?;
bytes.put_u16(key_length);
bytes.put_slice(key.as_bytes());
bytes.put_u64(seconds);
self.connection.write(bytes.into()).await?;
let mut r = self.get_response().await?;
let success = match r.try_get_u8()? {
1 => true,
0 => false,
_ => return Err(AppError::InvalidCommandResponse),
};
Ok(success)
}
async fn get_response(&mut self) -> Result<Bytes> {
self.connection
.read_bytes()