Skip to content

Latest commit

 

History

History
71 lines (52 loc) · 2.1 KB

README.md

File metadata and controls

71 lines (52 loc) · 2.1 KB

Redis Protocol

LICENSE Build Status Crates.io Coverage Status API docs

Structs and functions for implementing the Redis protocol, built on nom and designed to work easily with Tokio.

Install

With cargo edit.

cargo add redis-protocol

Features

  • Encode and decode with BytesMut or slices.
  • Parse publish-subscribe messages.
  • Support MOVED and ASK errors.
  • Implements cluster key hashing.

Examples

extern crate redis_protocol;
extern crate bytes;

use redis_protocol::prelude::*;
use bytes::BytesMut;

fn main() {
  let frame = Frame::BulkString("foobar".into());
  let mut buf = BytesMut::new();
  
  let len = match encode_bytes(&mut buf, &frame) {
    Ok(l) => l,
    Err(e) => panic!("Error encoding frame: {:?}", e)
  };
  println!("Encoded {} bytes into buffer with contents {:?}", len, buf);
  
  let buf: BytesMut = "*3\r\n$3\r\nFoo\r\n$-1\r\n$3\r\nBar\r\n".into();
  let (frame, consumed) = match decode_bytes(&buf) {
    Ok((f, c)) => (f, c),
    Err(e) => panic!("Error parsing bytes: {:?}", e)
  };
  
  if let Some(frame) = frame {
    println!("Parsed frame {:?} and consumed {} bytes", frame, consumed);
  }else{
    println!("Incomplete frame, parsed {} bytes", consumed);
  }
  
  let key = "foobarbaz";
  println!("Hash slot for {}: {}", key, redis_keyslot(key));
}

See the encode and decode tests for more examples.

Tests

To run the unit tests:

cargo test