Created
September 17, 2014 01:16
-
-
Save etscrivner/fcda4a6af6af6216d363 to your computer and use it in GitHub Desktop.
Simple redis client in rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::TcpStream; | |
use std::str; | |
fn main() { | |
let mut client = match TcpStream::connect("127.0.0.1", 6379) { | |
Ok(c) => c, | |
Err(e) => fail!("Failed: {}", e) | |
}; | |
let command_parts = ["keys", "*"]; | |
let mut full_command = format!("*{}\r\n", command_parts.len()); | |
for &part in command_parts.iter() { | |
full_command = format!("{}${}\r\n{}\r\n", full_command, part.len(), part) | |
} | |
println!("{}", full_command); | |
match client.write(full_command.as_bytes()) { | |
Err(e) => fail!("{}", e), | |
_ => {} | |
}; | |
let mut buf = [0u8, .. 256]; | |
match client.read(buf) { | |
Err(e) => fail!("Failed to read from redis client: {}", e), | |
Ok(r) => r | |
}; | |
match str::from_utf8(buf) { | |
Some(x) => println!("{}", x), | |
None => { } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment