123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- use tokio::net::{TcpListener, TcpStream};
- use mini_redis::{Connection, Frame};
- use bytes::Bytes;
- use std::collections::HashMap;
- use std::sync::{Arc, Mutex};
- type Db = Arc<Mutex<HashMap<String, Bytes>>>;
- #[tokio::main]
- async fn main() {
- // Bind the listener to the address
- let listener = TcpListener::bind("127.0.0.1:6379").await.unwrap();
- println!("Listening");
- let db = Arc::new(Mutex::new(HashMap::new()));
- loop {
- let (socket, _) = listener.accept().await.unwrap();
- // Clone the handle to the hash map.
- let db = db.clone();
- println!("Accepted");
- tokio::spawn(async move {
- process(socket, db).await;
- });
- }
- }
- async fn process(socket: TcpStream, db: Db) {
- use mini_redis::Command::{self, Get, Set};
- // Connection, provided by `mini-redis`, handles parsing frames from
- // the socket
- let mut connection = Connection::new(socket);
- while let Some(frame) = connection.read_frame().await.unwrap() {
- let response = match Command::from_frame(frame).unwrap() {
- Set(cmd) => {
- let mut db = db.lock().unwrap();
- db.insert(cmd.key().to_string(), cmd.value().clone());
- Frame::Simple("OK".to_string())
- }
- Get(cmd) => {
- let db = db.lock().unwrap();
- if let Some(value) = db.get(cmd.key()) {
- Frame::Bulk(value.clone())
- } else {
- Frame::Null
- }
- }
- cmd => panic!("unimplemented {:?}", cmd),
- };
- // Write the response to the client
- connection.write_frame(&response).await.unwrap();
- }
- }
|