ttl command

This commit is contained in:
2025-06-17 02:12:15 +02:00
parent 20e3fbd5d3
commit bd19ddd6cb
5 changed files with 93 additions and 4 deletions

View File

@@ -144,6 +144,32 @@ impl Client {
Ok(r.try_get_u8()? == 1)
}
pub async fn ttl(&mut self, key: &str) -> Result<Option<u64>> {
let mut bytes = BytesMut::new();
bytes.put_u16(3);
bytes.put_slice(b"ttl");
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());
self.connection.write(bytes.into()).await?;
let mut r = self.get_response().await?;
let ttl = match r.try_get_u8()? {
1 => Some(r.try_get_u64()?),
0 => None,
_ => return Err(AppError::InvalidCommandResponse),
};
Ok(ttl)
}
async fn get_response(&mut self) -> Result<Bytes> {
self.connection
.read_bytes()