RedisClusterHashStrategy.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. );
  126. }
  127. /**
  128. * Returns the list of IDs for the supported commands.
  129. *
  130. * @return array
  131. */
  132. public function getSupportedCommands()
  133. {
  134. return array_keys($this->commands);
  135. }
  136. /**
  137. * Sets an handler for the specified command ID.
  138. *
  139. * The signature of the callback must have a single parameter
  140. * of type Predis\Command\CommandInterface.
  141. *
  142. * When the callback argument is omitted or NULL, the previously
  143. * associated handler for the specified command ID is removed.
  144. *
  145. * @param string $commandId The ID of the command to be handled.
  146. * @param mixed $callback A valid callable object or NULL.
  147. */
  148. public function setCommandHandler($commandId, $callback = null)
  149. {
  150. $commandId = strtoupper($commandId);
  151. if (!isset($callback)) {
  152. unset($this->commands[$commandId]);
  153. return;
  154. }
  155. if (!is_callable($callback)) {
  156. throw new \InvalidArgumentException("Callback must be a valid callable object or NULL");
  157. }
  158. $this->commands[$commandId] = $callback;
  159. }
  160. /**
  161. * Extracts the key from the first argument of a command instance.
  162. *
  163. * @param CommandInterface $command Command instance.
  164. * @return string
  165. */
  166. protected function getKeyFromFirstArgument(CommandInterface $command)
  167. {
  168. return $command->getArgument(0);
  169. }
  170. /**
  171. * Extracts the key from a command that can accept multiple keys ensuring
  172. * that only one key is actually specified to comply with redis-cluster.
  173. *
  174. * @param CommandInterface $command Command instance.
  175. * @return string
  176. */
  177. protected function getKeyFromAllArguments(CommandInterface $command)
  178. {
  179. $arguments = $command->getArguments();
  180. if (count($arguments) === 1) {
  181. return $arguments[0];
  182. }
  183. }
  184. /**
  185. * Extracts the key from a command that can accept multiple keys ensuring
  186. * that only one key is actually specified to comply with redis-cluster.
  187. *
  188. * @param CommandInterface $command Command instance.
  189. * @return string
  190. */
  191. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  192. {
  193. $arguments = $command->getArguments();
  194. if (count($arguments) === 2) {
  195. return $arguments[0];
  196. }
  197. }
  198. /**
  199. * Extracts the key from BLPOP and BRPOP commands ensuring that only one key
  200. * is actually specified to comply with redis-cluster.
  201. *
  202. * @param CommandInterface $command Command instance.
  203. * @return string
  204. */
  205. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  206. {
  207. $arguments = $command->getArguments();
  208. if (count($arguments) === 2) {
  209. return $arguments[0];
  210. }
  211. }
  212. /**
  213. * {@inheritdoc}
  214. */
  215. public function getHash(CommandInterface $command)
  216. {
  217. $hash = $command->getHash();
  218. if (!isset($hash) && isset($this->commands[$cmdID = $command->getId()])) {
  219. if (null !== $key = call_user_func($this->commands[$cmdID], $command)) {
  220. $hash = $this->hashGenerator->hash($key);
  221. $command->setHash($hash);
  222. }
  223. }
  224. return $hash;
  225. }
  226. /**
  227. * {@inheritdoc}
  228. */
  229. public function getKeyHash($key)
  230. {
  231. return $this->hashGenerator->hash($key);
  232. }
  233. }