RedisClusterHashStrategy.php 10 KB

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