RedisClusterHashStrategy.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Command\Hash;
  11. use Predis\Command\CommandInterface;
  12. use Predis\Distribution\HashGeneratorInterface;
  13. /**
  14. * Default class used by Predis to calculate hashes out of keys of
  15. * commands supported by redis-cluster.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class RedisClusterHashStrategy implements CommandHashStrategyInterface
  20. {
  21. private $commands;
  22. /**
  23. *
  24. */
  25. public function __construct()
  26. {
  27. $this->commands = $this->getDefaultCommands();
  28. }
  29. /**
  30. * Returns the default map of supported commands with their handlers.
  31. *
  32. * @return array
  33. */
  34. protected function getDefaultCommands()
  35. {
  36. $keyIsFirstArgument = array($this, 'getKeyFromFirstArgument');
  37. return array(
  38. /* commands operating on the key space */
  39. 'EXISTS' => $keyIsFirstArgument,
  40. 'DEL' => array($this, 'getKeyFromAllArguments'),
  41. 'TYPE' => $keyIsFirstArgument,
  42. 'EXPIRE' => $keyIsFirstArgument,
  43. 'EXPIREAT' => $keyIsFirstArgument,
  44. 'PERSIST' => $keyIsFirstArgument,
  45. 'PEXPIRE' => $keyIsFirstArgument,
  46. 'PEXPIREAT' => $keyIsFirstArgument,
  47. 'TTL' => $keyIsFirstArgument,
  48. 'PTTL' => $keyIsFirstArgument,
  49. 'SORT' => $keyIsFirstArgument, // TODO
  50. /* commands operating on string values */
  51. 'APPEND' => $keyIsFirstArgument,
  52. 'DECR' => $keyIsFirstArgument,
  53. 'DECRBY' => $keyIsFirstArgument,
  54. 'GET' => $keyIsFirstArgument,
  55. 'GETBIT' => $keyIsFirstArgument,
  56. 'MGET' => array($this, 'getKeyFromAllArguments'),
  57. 'SET' => $keyIsFirstArgument,
  58. 'GETRANGE' => $keyIsFirstArgument,
  59. 'GETSET' => $keyIsFirstArgument,
  60. 'INCR' => $keyIsFirstArgument,
  61. 'INCRBY' => $keyIsFirstArgument,
  62. 'SETBIT' => $keyIsFirstArgument,
  63. 'SETEX' => $keyIsFirstArgument,
  64. 'MSET' => array($this, 'getKeyFromInterleavedArguments'),
  65. 'MSETNX' => array($this, 'getKeyFromInterleavedArguments'),
  66. 'SETNX' => $keyIsFirstArgument,
  67. 'SETRANGE' => $keyIsFirstArgument,
  68. 'STRLEN' => $keyIsFirstArgument,
  69. 'SUBSTR' => $keyIsFirstArgument,
  70. 'BITCOUNT' => $keyIsFirstArgument,
  71. /* commands operating on lists */
  72. 'LINSERT' => $keyIsFirstArgument,
  73. 'LINDEX' => $keyIsFirstArgument,
  74. 'LLEN' => $keyIsFirstArgument,
  75. 'LPOP' => $keyIsFirstArgument,
  76. 'RPOP' => $keyIsFirstArgument,
  77. 'BLPOP' => array($this, 'getKeyFromBlockingListCommands'),
  78. 'BRPOP' => array($this, 'getKeyFromBlockingListCommands'),
  79. 'LPUSH' => $keyIsFirstArgument,
  80. 'LPUSHX' => $keyIsFirstArgument,
  81. 'RPUSH' => $keyIsFirstArgument,
  82. 'RPUSHX' => $keyIsFirstArgument,
  83. 'LRANGE' => $keyIsFirstArgument,
  84. 'LREM' => $keyIsFirstArgument,
  85. 'LSET' => $keyIsFirstArgument,
  86. 'LTRIM' => $keyIsFirstArgument,
  87. /* commands operating on sets */
  88. 'SADD' => $keyIsFirstArgument,
  89. 'SCARD' => $keyIsFirstArgument,
  90. 'SISMEMBER' => $keyIsFirstArgument,
  91. 'SMEMBERS' => $keyIsFirstArgument,
  92. 'SPOP' => $keyIsFirstArgument,
  93. 'SRANDMEMBER' => $keyIsFirstArgument,
  94. 'SREM' => $keyIsFirstArgument,
  95. /* commands operating on sorted sets */
  96. 'ZADD' => $keyIsFirstArgument,
  97. 'ZCARD' => $keyIsFirstArgument,
  98. 'ZCOUNT' => $keyIsFirstArgument,
  99. 'ZINCRBY' => $keyIsFirstArgument,
  100. 'ZRANGE' => $keyIsFirstArgument,
  101. 'ZRANGEBYSCORE' => $keyIsFirstArgument,
  102. 'ZRANK' => $keyIsFirstArgument,
  103. 'ZREM' => $keyIsFirstArgument,
  104. 'ZREMRANGEBYRANK' => $keyIsFirstArgument,
  105. 'ZREMRANGEBYSCORE' => $keyIsFirstArgument,
  106. 'ZREVRANGE' => $keyIsFirstArgument,
  107. 'ZREVRANGEBYSCORE' => $keyIsFirstArgument,
  108. 'ZREVRANK' => $keyIsFirstArgument,
  109. 'ZSCORE' => $keyIsFirstArgument,
  110. /* commands operating on hashes */
  111. 'HDEL' => $keyIsFirstArgument,
  112. 'HEXISTS' => $keyIsFirstArgument,
  113. 'HGET' => $keyIsFirstArgument,
  114. 'HGETALL' => $keyIsFirstArgument,
  115. 'HMGET' => $keyIsFirstArgument,
  116. 'HINCRBY' => $keyIsFirstArgument,
  117. 'HINCRBYFLOAT' => $keyIsFirstArgument,
  118. 'HKEYS' => $keyIsFirstArgument,
  119. 'HLEN' => $keyIsFirstArgument,
  120. 'HSET' => $keyIsFirstArgument,
  121. 'HSETNX' => $keyIsFirstArgument,
  122. 'HVALS' => $keyIsFirstArgument,
  123. );
  124. }
  125. /**
  126. * Returns the list of IDs for the supported commands.
  127. *
  128. * @return array
  129. */
  130. public function getSupportedCommands()
  131. {
  132. return array_keys($this->commands);
  133. }
  134. /**
  135. * Sets an handler for the specified command ID.
  136. *
  137. * The signature of the callback must have a single parameter
  138. * of type Predis\Command\CommandInterface.
  139. *
  140. * When the callback argument is omitted or NULL, the previously
  141. * associated handler for the specified command ID is removed.
  142. *
  143. * @param string $commandId The ID of the command to be handled.
  144. * @param mixed $callback A valid callable object or NULL.
  145. */
  146. public function setCommandHandler($commandId, $callback = null)
  147. {
  148. $commandId = strtoupper($commandId);
  149. if (!isset($callback)) {
  150. unset($this->commands[$commandId]);
  151. return;
  152. }
  153. if (!is_callable($callback)) {
  154. throw new \InvalidArgumentException("Callback must be a valid callable object or NULL");
  155. }
  156. $this->commands[$commandId] = $callback;
  157. }
  158. /**
  159. * Extracts the key from the first argument of a command instance.
  160. *
  161. * @param CommandInterface $command Command instance.
  162. * @return string
  163. */
  164. protected function getKeyFromFirstArgument(CommandInterface $command)
  165. {
  166. return $command->getArgument(0);
  167. }
  168. /**
  169. * Extracts the key from a command that can accept multiple keys ensuring
  170. * that only one key is actually specified to comply with redis-cluster.
  171. *
  172. * @param CommandInterface $command Command instance.
  173. * @return string
  174. */
  175. protected function getKeyFromAllArguments(CommandInterface $command)
  176. {
  177. $arguments = $command->getArguments();
  178. if (count($arguments) === 1) {
  179. return $arguments[0];
  180. }
  181. }
  182. /**
  183. * Extracts the key from a command that can accept multiple keys ensuring
  184. * that only one key is actually specified to comply with redis-cluster.
  185. *
  186. * @param CommandInterface $command Command instance.
  187. * @return string
  188. */
  189. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  190. {
  191. $arguments = $command->getArguments();
  192. if (count($arguments) === 2) {
  193. return $arguments[0];
  194. }
  195. }
  196. /**
  197. * Extracts the key from BLPOP and BRPOP commands ensuring that only one key
  198. * is actually specified to comply with redis-cluster.
  199. *
  200. * @param CommandInterface $command Command instance.
  201. * @return string
  202. */
  203. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  204. {
  205. $arguments = $command->getArguments();
  206. if (count($arguments) === 2) {
  207. return $arguments[0];
  208. }
  209. }
  210. /**
  211. * {@inheritdoc}
  212. */
  213. public function getHash(HashGeneratorInterface $hasher, CommandInterface $command)
  214. {
  215. if (isset($this->commands[$cmdID = $command->getId()])) {
  216. if ($key = call_user_func($this->commands[$cmdID], $command)) {
  217. return $this->getKeyHash($hasher, $key);
  218. }
  219. }
  220. }
  221. /**
  222. * {@inheritdoc}
  223. */
  224. public function getKeyHash(HashGeneratorInterface $hasher, $key)
  225. {
  226. return $hasher->hash($key);
  227. }
  228. }