diff options
| author | omagdy <omar.professional8777@gmail.com> | 2025-07-16 22:08:45 +0300 |
|---|---|---|
| committer | omagdy <omar.professional8777@gmail.com> | 2025-07-16 22:08:45 +0300 |
| commit | e814746602adf42369abf91882d03b3e16c7c7f0 (patch) | |
| tree | d9e1c35e80dcbb41d98e3c7e21ec88f9805a5a6f /src/main.rs | |
| parent | 18b7911c656b531fc5d7fe15245e765951f3e65e (diff) | |
| download | redis-rust-e814746602adf42369abf91882d03b3e16c7c7f0.tar.xz redis-rust-e814746602adf42369abf91882d03b3e16c7c7f0.zip | |
feat: Added support for simple commands like PING, ECHO, GET and SET
- Implemented a simple parsing logic for parsing commmands now
- Created a HashMap that will act as our storage and shared it across
threads using `Arc<Mutex<()>>` magic
- Wrote some macros to make instantiating RESP types eaiser for myself
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs index 7ddd478..a382f15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,21 @@ #![allow(unused_imports)] use std::{ + collections::HashMap, io::{Read, Write}, net::{TcpListener, TcpStream}, + sync::{Arc, Mutex}, thread, }; mod resp_commands; mod resp_parser; -use resp_commands::RespCommands; -use resp_parser::parse; +use resp_commands::RedisCommands; +use resp_parser::{parse, RespType}; -fn handle_client(mut stream: TcpStream) { +pub type SharedCache = Arc<Mutex<HashMap<String, String>>>; + +fn handle_client(mut stream: TcpStream, cache: SharedCache) { let mut buffer = [0; 512]; loop { let bytes_read = match stream.read(&mut buffer) { @@ -21,7 +25,7 @@ fn handle_client(mut stream: TcpStream) { }; let parsed_resp = parse(&buffer).unwrap(); - let response = RespCommands::from(parsed_resp.0).execute(); + let response = RedisCommands::from(parsed_resp.0).execute(cache.clone()); // Hardcode PONG response for now stream.write(&response).unwrap(); @@ -35,12 +39,14 @@ fn handle_client(mut stream: TcpStream) { fn main() -> std::io::Result<()> { let listener = TcpListener::bind("127.0.0.1:6379").unwrap(); + let cache: SharedCache = Arc::new(Mutex::new(HashMap::new())); for stream in listener.incoming() { match stream { Ok(stream) => { + let cache_clone = cache.clone(); thread::spawn(|| { - handle_client(stream); + handle_client(stream, cache_clone); }); } Err(e) => { |
