pub fn del(&self, key: &str) -> bool self.inner.lock().unwrap().remove(key).is_some()
pub fn dbsize(&self) -> usize let map = self.inner.lock().unwrap(); let now = SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() .as_millis() as u64; map.iter() .filter(
fn handle_del(store: &Store, args: &[RespValue]) -> RespValue let mut count = 0; for arg in args if let RespValue::BulkString(Some(key_bytes)) = arg let key = String::from_utf8_lossy(key_bytes); if store.del(&key) count += 1; Giordani L. Rust Projects. Write a Redis Clone....
match command.as_str() { "SET" => handle_set(store, args), "GET" => handle_get(store, args), "DEL" => handle_del(store, args), "EXISTS" => handle_exists(store, args), "KEYS" => handle_keys(store, args), "EXPIRE" => handle_expire(store, args), "TTL" => handle_ttl(store, args), "DBSIZE" => handle_dbsize(store, args), "FLUSHALL" => handle_flushall(store, args), "PING" => handle_ping(args), _ => RespValue::Error(format!("ERR unknown command '{}'", command)), } } else RespValue::Error("ERR invalid command format".to_string()) } _ => RespValue::Error("ERR invalid request".to_string()), } }
fn handle_ping(_args: &[RespValue]) -> RespValue RespValue::SimpleString("PONG".to_string()) pub fn del(&self, key: &str) -> bool self
let server = Server::new(listener, store); server.run().await?;
let key = match &args[0] RespValue::BulkString(Some(k)) => String::from_utf8_lossy(k).to_string(), _ => return RespValue::Error("ERR invalid key".to_string()), ; pub fn del(&self
fn handle_keys(store: &Store, args: &[RespValue]) -> RespValue RespValue::BulkString(Some(k.into_bytes()))) .collect();