图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
729 B

  1. from binascii import crc_hqx
  2. from redis.typing import EncodedT
  3. # Redis Cluster's key space is divided into 16384 slots.
  4. # For more information see: https://github.com/redis/redis/issues/2576
  5. REDIS_CLUSTER_HASH_SLOTS = 16384
  6. __all__ = ["key_slot", "REDIS_CLUSTER_HASH_SLOTS"]
  7. def key_slot(key: EncodedT, bucket: int = REDIS_CLUSTER_HASH_SLOTS) -> int:
  8. """Calculate key slot for a given key.
  9. See Keys distribution model in https://redis.io/topics/cluster-spec
  10. :param key - bytes
  11. :param bucket - int
  12. """
  13. start = key.find(b"{")
  14. if start > -1:
  15. end = key.find(b"}", start + 1)
  16. if end > -1 and end != start + 1:
  17. key = key[start + 1 : end]
  18. return crc_hqx(key, 0) % bucket