3 releases
0.1.2 | May 14, 2022 |
---|---|
0.1.1 | May 14, 2022 |
0.1.0 | May 14, 2022 |
#1615 in Database interfaces
Used in redizone
33KB
601 lines
Redis compatible server framework for Rust
Features
- Create a fast custom Redis compatible server in Rust
- Simple API.
- Support for pipelining and telnet commands.
- Works with Redis clients such as redis-rs, redigo, redis-py, node_redis, and jedis
- Multithreaded
This library is also avaliable for Go.
Example
Here's a full example of a Redis clone that accepts:
- SET key value
- GET key
- DEL key
- PING
- QUIT
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();
}