Crate redis_protocol

source ·
Expand description

Redis Protocol

Structs and functions for implementing the RESP2 and RESP3 protocol.

Examples


use redis_protocol::resp2::prelude::*;
use bytes::{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: Bytes = "*3\r\n$3\r\nFoo\r\n$-1\r\n$3\r\nBar\r\n".into();
  let (frame, consumed) = match decode(&buf) {
    Ok(Some((f, c))) => (f, c),
    Ok(None) => panic!("Incomplete frame."),
    Err(e) => panic!("Error parsing bytes: {:?}", e)
  };
  println!("Parsed frame {:?} and consumed {} bytes", frame, consumed);

  let key = "foobarbaz";
  println!("Hash slot for {}: {}", key, redis_keyslot(key.as_bytes()));
}

Note: if callers are not using the index-map feature then substitute std::collections::HashMap for any IndexMap types in these docs. rustdoc doesn’t have a great way to show type substitutions based on feature flags.

Modules

Types and functions for implementing the RESP2 protocol.
Types and functions for implementing the RESP3 protocol.
Error types and general redis protocol types.

Constants

A pre-defined zeroed out KB of data, used to speed up extending buffers while encoding.

Functions

Returns the number of bytes necessary to encode a string representation of d.
Map a Redis key to its cluster key slot.
Utility function to translate RESP2 frames to RESP3 frames.