ttl command
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user