Crates.io | redcon |
lib.rs | redcon |
version | 0.1.2 |
source | src |
created_at | 2022-05-14 14:18:20.138526 |
updated_at | 2022-05-14 17:21:41.74446 |
description | Redis compatible server framework for Rust |
homepage | |
repository | https://github.com/tidwall/redcon.rs |
max_upload_size | |
id | 586683 |
size | 51,953 |
Redis compatible server framework for Rust
This library is also avaliable for Go.
Here's a full example of a Redis clone that accepts:
use std::collections::HashMap;
use std::sync::Mutex;
fn main() {
let db: Mutex<HashMap<Vec<u8>, Vec<u8>>> = Mutex::new(HashMap::new());
let mut s = redcon::listen("127.0.0.1:6380", db).unwrap();
s.command = Some(|conn, db, args|{
let name = String::from_utf8_lossy(&args[0]).to_lowercase();
match name.as_str() {
"ping" => conn.write_string("PONG"),
"set" => {
if args.len() < 3 {
conn.write_error("ERR wrong number of arguments");
return;
}
let mut db = db.lock().unwrap();
db.insert(args[1].to_owned(), args[2].to_owned());
conn.write_string("OK");
}
"get" => {
if args.len() < 2 {
conn.write_error("ERR wrong number of arguments");
return;
}
let db = db.lock().unwrap();
match db.get(&args[1]) {
Some(val) => conn.write_bulk(val),
None => conn.write_null(),
}
}
_ => conn.write_error("ERR unknown command"),
}
});
println!("Serving at {}", s.local_addr());
s.serve().unwrap();
}