pub fn handle_command(store: &Store, cmd: &RespValue) -> RespValue { match cmd { RespValue::Array(args) if !args.is_empty() => { if let RespValue::BulkString(Some(cmd_bytes)) = &args[0] { let command = String::from_utf8_lossy(cmd_bytes).to_uppercase(); let args = &args[1..];
impl Server { pub fn new(listener: TcpListener, store: Store) -> Self Self listener, store
This project forces you to grapple with the core pillars of systems programming: concurrency, network protocols, and memory management Why Build a Redis Clone?
if self.buffer.len() < total_needed return Ok(None); Giordani L. Rust Projects. Write a Redis Clone....
pub fn get(&self, key: &str) -> Option<Vec<u8>> let map = self.inner.lock().unwrap(); map.get(key).cloned()
Create src/resp.rs :
fn handle_flushall(store: &Store, _args: &[RespValue]) -> RespValue store.flushall(); RespValue::SimpleString("OK".to_string()) pub fn handle_command(store: &Store
Notice the Vec<u8> : Rust forces us to think about binary safety, unlike Redis which stores strings.
((expires_at - now) / 1000) as i64 else -1
if store.expire(&key, seconds) RespValue::Integer(1) else RespValue::Integer(0) cmd: &RespValue) ->
use bytes::Bytes, BytesMut; use std::collections::VecDeque;
#[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let listener = TcpListener::bind("127.0.0.1:6379").await?; let store = Store::new();