RedisCluster.Key (redis_cluster v0.8.0)

View Source

This module handles the key operations, namely hashing the keys and finding the appropriate hash slots. For more information on how Redis uses hash slots, see the Redis Cluster documentation.

Summary

Types

The range of hash values in a Redis cluster.

Functions

Computes the hash slot for the given key. If the :compute_hash_tag option is given then looks for a hash tag in the key and uses it, if present, to compute the hash slot. This function doesn't look for hash tags by default. This saves some overhead when not needed.

Computes the hash tag of the key, if any.

Types

hash()

@type hash() :: 0..16383

The range of hash values in a Redis cluster.

Functions

hash_slot(key, opts \\ [])

@spec hash_slot(binary(), Keyword.t()) :: hash()

Computes the hash slot for the given key. If the :compute_hash_tag option is given then looks for a hash tag in the key and uses it, if present, to compute the hash slot. This function doesn't look for hash tags by default. This saves some overhead when not needed.

Options

  • :compute_hash_tag - If set to true, the hash tag will be computed from the key (default false). This is useful for cluster mode, where keys with the same hash tag are stored in the same slot.

Examples

iex> RedisCluster.Key.hash_slot("my_key")
13711

iex> RedisCluster.Key.hash_slot("my_key", compute_hash_tag: true)
13711

iex> RedisCluster.Key.hash_slot("{user1234}:contact", compute_hash_tag: true)
14020

iex> RedisCluster.Key.hash_slot("{user1234}:search_history", compute_hash_tag: true)
14020

hashtag(key)

@spec hashtag(binary()) :: binary() | nil

Computes the hash tag of the key, if any.

Examples

iex> RedisCluster.Key.hashtag("{user1234}:contact")
"user1234"

iex> RedisCluster.Key.hashtag("search_history:{user1234}")
"user1234"

iex> RedisCluster.Key.hashtag("{user1234}:orders:{not_a_tag}")
"user1234"

iex> RedisCluster.Key.hashtag("{}:" <> <<0xDE, 0xAD, 0xC0, 0xDE>>)
nil

iex> RedisCluster.Key.hashtag("{}:some_key:{not_a_tag}")
nil

iex> RedisCluster.Key.hashtag("my_key")
nil