Skip to content

atlas-2192/Redis_Server_Rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 and C.

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();
}

About

Redis Server Framework With Rust

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages