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) 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> { async fn get_response(&mut self) -> Result<Bytes> {
self.connection self.connection
.read_bytes() .read_bytes()

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 })
}
}

View File

@@ -1,4 +1,5 @@
mod delete; mod delete;
mod expire;
mod get; mod get;
mod has; mod has;
mod set; mod set;
@@ -8,6 +9,7 @@ use std::io::Cursor;
use bytes::{Buf, BytesMut}; use bytes::{Buf, BytesMut};
use delete::Delete; use delete::Delete;
use expire::Expire;
use get::Get; use get::Get;
use has::Has; use has::Has;
use set::Set; use set::Set;
@@ -22,6 +24,7 @@ pub enum Command {
Delete(Delete), Delete(Delete),
Has(Has), Has(Has),
Ttl(Ttl), Ttl(Ttl),
Expire(Expire),
} }
impl Command { impl Command {
@@ -32,6 +35,7 @@ impl Command {
Command::Delete(delete) => delete.execute(db, connection).await, Command::Delete(delete) => delete.execute(db, connection).await,
Command::Has(has) => has.execute(db, connection).await, Command::Has(has) => has.execute(db, connection).await,
Command::Ttl(ttl) => ttl.execute(db, connection).await, Command::Ttl(ttl) => ttl.execute(db, connection).await,
Command::Expire(expire) => expire.execute(db, connection).await,
} }
} }
@@ -56,6 +60,7 @@ impl Command {
"delete" => Self::Delete(Delete::parse(bytes)?), "delete" => Self::Delete(Delete::parse(bytes)?),
"has" => Self::Has(Has::parse(bytes)?), "has" => Self::Has(Has::parse(bytes)?),
"ttl" => Self::Ttl(Ttl::parse(bytes)?), "ttl" => Self::Ttl(Ttl::parse(bytes)?),
"expire" => Self::Expire(Expire::parse(bytes)?),
_ => return Err(AppError::UnknownCommand(command_name)), _ => return Err(AppError::UnknownCommand(command_name)),
}; };

View File

@@ -1,6 +1,7 @@
use std::{ use std::{
collections::{BTreeMap, BTreeSet}, collections::{BTreeMap, BTreeSet},
sync::Arc, sync::Arc,
time::Duration,
}; };
use bytes::{BufMut, Bytes, BytesMut}; use bytes::{BufMut, Bytes, BytesMut};
@@ -138,6 +139,44 @@ impl Database {
.flatten() .flatten()
} }
pub async fn expire(&self, key: &str, seconds: u64) -> Result<bool> {
let mut state = self.state.lock().await;
let expiration = Instant::now() + Duration::from_secs(seconds);
let notify =
state
.expirations
.iter()
.next()
.is_none_or(|&(instant, ref next_expiration_key)| {
next_expiration_key == &key || instant > expiration
});
let key = Yarn::copy(key);
let Some(value) = state.entries.get_mut(&key) else {
return Ok(false);
};
let previous_expiration = value.expiration.take();
value.expiration = Some(expiration);
if let Some(previous_expiration) = previous_expiration {
state
.expirations
.remove(&(previous_expiration, key.clone()));
};
state.expirations.insert((expiration, key));
if notify {
self.notify.notify_one();
}
Ok(true)
}
pub async fn shutdown(&mut self) { pub async fn shutdown(&mut self) {
self.state.lock().await.shutdown = true; self.state.lock().await.shutdown = true;
self.notify.notify_one(); self.notify.notify_one();

View File

@@ -27,9 +27,13 @@ async fn expiration() -> Result<(), Box<dyn std::error::Error>> {
assert!(client.has("test-key").await.unwrap()); assert!(client.has("test-key").await.unwrap());
assert_eq!(client.ttl("test-key").await.unwrap(), Some(1)); assert_eq!(client.ttl("test-key").await.unwrap(), Some(1));
assert!(client.expire("test-key", 2).await?);
tokio::time::sleep(Duration::from_secs(2)).await; tokio::time::sleep(Duration::from_secs(2)).await;
assert!(!client.has("test-key").await.unwrap()); assert!(!client.has("test-key").await.unwrap());
assert!(!client.expire("test-key", 10).await?);
shutdown_tx.send(()).unwrap(); shutdown_tx.send(()).unwrap();
server_handle.await??; server_handle.await??;