summaryrefslogtreecommitdiff
path: root/redis/crc.py
diff options
context:
space:
mode:
authorChayim I. Kirshen <c@kirshen.com>2021-11-29 20:07:20 +0200
committerChayim I. Kirshen <c@kirshen.com>2021-11-29 20:07:20 +0200
commit39fc550251d238cdba7966ff153321ca9e488508 (patch)
treee79360ec70feac7f0ab992813f8b2d43f7c67bab /redis/crc.py
parenta924269502b96dc71339cca3dfb20aaa3899a9d0 (diff)
parent4db85ef574a64a2b230a3ae1ff19c9d04065a114 (diff)
downloadredis-py-ck-linkdocs.tar.gz
merging masterck-linkdocs
Diffstat (limited to 'redis/crc.py')
-rw-r--r--redis/crc.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/redis/crc.py b/redis/crc.py
new file mode 100644
index 0000000..7d2ee50
--- /dev/null
+++ b/redis/crc.py
@@ -0,0 +1,24 @@
+from binascii import crc_hqx
+
+# Redis Cluster's key space is divided into 16384 slots.
+# For more information see: https://github.com/redis/redis/issues/2576
+REDIS_CLUSTER_HASH_SLOTS = 16384
+
+__all__ = [
+ "key_slot",
+ "REDIS_CLUSTER_HASH_SLOTS"
+]
+
+
+def key_slot(key, bucket=REDIS_CLUSTER_HASH_SLOTS):
+ """Calculate key slot for a given key.
+ See Keys distribution model in https://redis.io/topics/cluster-spec
+ :param key - bytes
+ :param bucket - int
+ """
+ start = key.find(b"{")
+ if start > -1:
+ end = key.find(b"}", start + 1)
+ if end > -1 and end != start + 1:
+ key = key[start + 1: end]
+ return crc_hqx(key, 0) % bucket