1 use std::collections::HashMap;
2 use std::sync::{Arc, Mutex};
5 use mini_redis::{Connection, Frame};
6 use mini_redis::Command::{self, Get, Set};
7 use tokio::net::{TcpListener, TcpStream};
9 type Db = Arc<Mutex<HashMap<String, Bytes>>>;
13 let listener = TcpListener::bind("127.0.0.1:6379").await.unwrap();
14 let db: Db = Arc::new(Mutex::new(HashMap::new()));
17 // The second item contains the IP and port of the new connection
18 let (socket, _) = listener.accept().await.unwrap();
19 let db_i = db.clone();
20 tokio::spawn(async move { process(socket, db_i).await });
24 async fn process(socket: TcpStream, db: Db) {
25 let mut connection = Connection::new(socket);
27 while let Some(frame) = connection.read_frame().await.unwrap() {
28 let response = match Command::from_frame(frame).unwrap() {
30 // The value is stored as `Vec<u8>`
31 db.lock().unwrap().insert(cmd.key().to_string(), cmd.value().clone());
32 Frame::Simple("OK".to_string())
35 if let Some(value) = db.lock().unwrap().get(cmd.key()) {
36 Frame::Bulk(value.clone())
41 cmd => panic!("unimplemented {:?}", cmd),
44 // Write the response to the client
45 connection.write_frame(&response).await.unwrap();