Redis.Protocol.RESP3 (Redis v0.8.0)

Copy Markdown View Source

RESP3 protocol encoder and decoder.

Pure functions — no processes, no side effects. Encodes commands to iodata and decodes binary wire data to Elixir terms.

RESP3 Type Mapping

Wire prefixRESP3 TypeElixir Type
+Simple stringString.t()
$Blob stringString.t()
-Simple error%Redis.Error{}
!Blob error%Redis.Error{}
:Numberinteger()
,Doublefloat()
#Booleanboolean()
_Nullnil
*Arraylist()
%Mapmap()
~SetMapSet.t()
>Push{:push, list()}
=Verbatim stringString.t() (encoding stripped)
(Big numberinteger()

Summary

Functions

Decodes a RESP3 response from binary data. Returns {:ok, term, rest} or {:continuation, fun} if more data is needed.

Encodes a command (list of strings/binaries) into RESP3 wire format. Returns iodata for efficient socket writing.

Encodes multiple commands for pipelining. Returns iodata.

Functions

decode(data)

@spec decode(binary()) ::
  {:ok, term(), binary()} | {:continuation, (binary() -> term())}

Decodes a RESP3 response from binary data. Returns {:ok, term, rest} or {:continuation, fun} if more data is needed.

encode(args)

@spec encode([binary()]) :: iodata()

Encodes a command (list of strings/binaries) into RESP3 wire format. Returns iodata for efficient socket writing.

iex> Redis.Protocol.RESP3.encode(["SET", "key", "value"]) |> IO.iodata_to_binary()
"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n"

encode_pipeline(commands)

@spec encode_pipeline([[binary()]]) :: iodata()

Encodes multiple commands for pipelining. Returns iodata.