RedisStrategy.php 10 KB

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