Redis (Redis v0.7.1)

Copy Markdown View Source

Modern, full-featured Redis client for Elixir.

This module provides the top-level API for single-connection usage. For other deployment modes and features, see:

Quick Start

{:ok, conn} = Redis.start_link()
{:ok, "OK"} = Redis.command(conn, ["SET", "key", "value"])
{:ok, "value"} = Redis.command(conn, ["GET", "key"])

Pipelining

{:ok, results} = Redis.pipeline(conn, [
  ["SET", "a", "1"],
  ["SET", "b", "2"],
  ["MGET", "a", "b"]
])

Transactions

{:ok, [1, 2, 3]} = Redis.transaction(conn, [
  ["INCR", "counter"],
  ["INCR", "counter"],
  ["INCR", "counter"]
])

Optimistic Locking

Redis.watch_transaction(conn, ["balance"], fn conn ->
  {:ok, bal} = Redis.command(conn, ["GET", "balance"])
  new_bal = String.to_integer(bal) + 100
  [["SET", "balance", to_string(new_bal)]]
end)

Summary

Functions

Returns a child spec for supervision trees.

Sends a single command.

Sends a single command, raises on error.

Sends a command without waiting for a reply (CLIENT REPLY OFF/ON).

Sends multiple commands without waiting for replies.

Sends multiple commands in a single pipeline.

Starts a connection. Accepts a keyword list or a Redis URI string.

Executes commands in a MULTI/EXEC transaction.

Executes a WATCH-based optimistic locking transaction.

Types

conn()

@type conn() :: GenServer.server()

Functions

child_spec(opts)

Returns a child spec for supervision trees.

command(conn, args, opts \\ [])

@spec command(conn(), [String.t()], keyword()) :: {:ok, term()} | {:error, term()}

Sends a single command.

command!(conn, args, opts \\ [])

@spec command!(conn(), [String.t()], keyword()) :: term()

Sends a single command, raises on error.

noreply_command(conn, args, opts \\ [])

@spec noreply_command(conn(), [String.t()], keyword()) :: :ok | {:error, term()}

Sends a command without waiting for a reply (CLIENT REPLY OFF/ON).

noreply_pipeline(conn, commands, opts \\ [])

@spec noreply_pipeline(conn(), [[String.t()]], keyword()) :: :ok | {:error, term()}

Sends multiple commands without waiting for replies.

pipeline(conn, commands, opts \\ [])

@spec pipeline(conn(), [[String.t()]], keyword()) ::
  {:ok, [term()]} | {:error, term()}

Sends multiple commands in a single pipeline.

start_link(opts \\ [])

@spec start_link(keyword() | String.t()) :: GenServer.on_start()

Starts a connection. Accepts a keyword list or a Redis URI string.

transaction(conn, commands, opts \\ [])

@spec transaction(conn(), [[String.t()]], keyword()) ::
  {:ok, [term()]} | {:error, term()}

Executes commands in a MULTI/EXEC transaction.

watch_transaction(conn, keys, fun, opts \\ [])

@spec watch_transaction(
  conn(),
  [String.t()],
  (conn() -> [[String.t()]] | {:abort, term()}),
  keyword()
) ::
  {:ok, [term()]} | {:error, term()}

Executes a WATCH-based optimistic locking transaction.

Watches the given keys, calls fun to read values and build commands, then executes in MULTI/EXEC. Retries automatically on conflict.

Redis.watch_transaction(conn, ["balance"], fn conn ->
  {:ok, bal} = Redis.command(conn, ["GET", "balance"])
  new_bal = String.to_integer(bal) + 100
  [["SET", "balance", to_string(new_bal)]]
end)