Redis.Cluster.Router (Redis v0.8.0)

Copy Markdown View Source

Hash slot router for Redis Cluster.

Computes CRC16 hash slots and extracts keys from commands for routing.

Summary

Functions

Extracts the first key from a Redis command for slot routing.

Extracts the keys referenced by a command.

Computes the hash slot for a key.

Extracts all keys from a command. For multi-key commands, validates they all hash to the same slot. Returns {:ok, slot} or {:error, :cross_slot}.

Validates that all commands in a pipeline target the same slot.

Functions

key_from_command(command)

@spec key_from_command([String.t()]) :: String.t() | nil

Extracts the first key from a Redis command for slot routing.

keys_from_command(list)

@spec keys_from_command([String.t()]) :: [String.t()]

Extracts the keys referenced by a command.

Commands with dynamic key positions (such as EVAL, XREAD, and the *MPOP family) and common multi-key commands are handled explicitly. Unknown commands fall back to Redis' usual first-argument key convention.

slot(key)

@spec slot(String.t()) :: non_neg_integer()

Computes the hash slot for a key.

slot_for_command(command)

@spec slot_for_command([String.t()]) ::
  {:ok, non_neg_integer()} | {:error, :no_key} | {:error, :cross_slot}

Extracts all keys from a command. For multi-key commands, validates they all hash to the same slot. Returns {:ok, slot} or {:error, :cross_slot}.

validate_pipeline(commands)

@spec validate_pipeline([[String.t()]]) ::
  {:ok, non_neg_integer()}
  | {:error, :cross_slot}
  | {:error, :empty}
  | {:error, :no_key}

Validates that all commands in a pipeline target the same slot.