RedisStrategy.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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\Command\CommandInterface;
  12. use Predis\Command\ScriptCommand;
  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 RedisStrategy implements StrategyInterface
  20. {
  21. private $commands;
  22. private $hashGenerator;
  23. /**
  24. *
  25. */
  26. public function __construct()
  27. {
  28. $this->commands = $this->getDefaultCommands();
  29. $this->hashGenerator = new Hash\CRC16();
  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. 'SSCAN' => $keyIsFirstArgument,
  95. 'SPOP' => $keyIsFirstArgument,
  96. 'SRANDMEMBER' => $keyIsFirstArgument,
  97. 'SREM' => $keyIsFirstArgument,
  98. /* commands operating on sorted sets */
  99. 'ZADD' => $keyIsFirstArgument,
  100. 'ZCARD' => $keyIsFirstArgument,
  101. 'ZCOUNT' => $keyIsFirstArgument,
  102. 'ZINCRBY' => $keyIsFirstArgument,
  103. 'ZRANGE' => $keyIsFirstArgument,
  104. 'ZRANGEBYSCORE' => $keyIsFirstArgument,
  105. 'ZRANK' => $keyIsFirstArgument,
  106. 'ZREM' => $keyIsFirstArgument,
  107. 'ZREMRANGEBYRANK' => $keyIsFirstArgument,
  108. 'ZREMRANGEBYSCORE' => $keyIsFirstArgument,
  109. 'ZREVRANGE' => $keyIsFirstArgument,
  110. 'ZREVRANGEBYSCORE' => $keyIsFirstArgument,
  111. 'ZREVRANK' => $keyIsFirstArgument,
  112. 'ZSCORE' => $keyIsFirstArgument,
  113. 'ZSCAN' => $keyIsFirstArgument,
  114. /* commands operating on hashes */
  115. 'HDEL' => $keyIsFirstArgument,
  116. 'HEXISTS' => $keyIsFirstArgument,
  117. 'HGET' => $keyIsFirstArgument,
  118. 'HGETALL' => $keyIsFirstArgument,
  119. 'HMGET' => $keyIsFirstArgument,
  120. 'HMSET' => $keyIsFirstArgument,
  121. 'HINCRBY' => $keyIsFirstArgument,
  122. 'HINCRBYFLOAT' => $keyIsFirstArgument,
  123. 'HKEYS' => $keyIsFirstArgument,
  124. 'HLEN' => $keyIsFirstArgument,
  125. 'HSET' => $keyIsFirstArgument,
  126. 'HSETNX' => $keyIsFirstArgument,
  127. 'HVALS' => $keyIsFirstArgument,
  128. 'HSCAN' => $keyIsFirstArgument,
  129. /* scripting */
  130. 'EVAL' => array($this, 'getKeyFromScriptingCommands'),
  131. 'EVALSHA' => array($this, 'getKeyFromScriptingCommands'),
  132. );
  133. }
  134. /**
  135. * Returns the list of IDs for the supported commands.
  136. *
  137. * @return array
  138. */
  139. public function getSupportedCommands()
  140. {
  141. return array_keys($this->commands);
  142. }
  143. /**
  144. * Sets an handler for the specified command ID.
  145. *
  146. * The signature of the callback must have a single parameter of type
  147. * Predis\Command\CommandInterface.
  148. *
  149. * When the callback argument is omitted or NULL, the previously associated
  150. * handler for the specified command ID is removed.
  151. *
  152. * @param string $commandID Command ID.
  153. * @param mixed $callback A valid callable object, or NULL to unset the handler.
  154. */
  155. public function setCommandHandler($commandID, $callback = null)
  156. {
  157. $commandID = strtoupper($commandID);
  158. if (!isset($callback)) {
  159. unset($this->commands[$commandID]);
  160. return;
  161. }
  162. if (!is_callable($callback)) {
  163. throw new \InvalidArgumentException(
  164. "Callback must be a valid callable object or NULL"
  165. );
  166. }
  167. $this->commands[$commandID] = $callback;
  168. }
  169. /**
  170. * Extracts the key from the first argument of a command instance.
  171. *
  172. * @param CommandInterface $command Command instance.
  173. * @return string
  174. */
  175. protected function getKeyFromFirstArgument(CommandInterface $command)
  176. {
  177. return $command->getArgument(0);
  178. }
  179. /**
  180. * Extracts the key from a command that can accept multiple keys ensuring
  181. * that only one key is actually specified to comply with redis-cluster.
  182. *
  183. * @param CommandInterface $command Command instance.
  184. * @return string
  185. */
  186. protected function getKeyFromAllArguments(CommandInterface $command)
  187. {
  188. $arguments = $command->getArguments();
  189. if (count($arguments) === 1) {
  190. return $arguments[0];
  191. }
  192. }
  193. /**
  194. * Extracts the key from a command that can accept multiple keys ensuring
  195. * that only one key is actually specified to comply with redis-cluster.
  196. *
  197. * @param CommandInterface $command Command instance.
  198. * @return string
  199. */
  200. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  201. {
  202. $arguments = $command->getArguments();
  203. if (count($arguments) === 2) {
  204. return $arguments[0];
  205. }
  206. }
  207. /**
  208. * Extracts the key from BLPOP and BRPOP commands ensuring that only one key
  209. * is actually specified to comply with redis-cluster.
  210. *
  211. * @param CommandInterface $command Command instance.
  212. * @return string
  213. */
  214. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  215. {
  216. $arguments = $command->getArguments();
  217. if (count($arguments) === 2) {
  218. return $arguments[0];
  219. }
  220. }
  221. /**
  222. * Extracts the key from EVAL and EVALSHA commands.
  223. *
  224. * @param CommandInterface $command Command instance.
  225. * @return string
  226. */
  227. protected function getKeyFromScriptingCommands(CommandInterface $command)
  228. {
  229. if ($command instanceof ScriptCommand) {
  230. $keys = $command->getKeys();
  231. } else {
  232. $keys = array_slice($args = $command->getArguments(), 2, $args[1]);
  233. }
  234. if (count($keys) === 1) {
  235. return $keys[0];
  236. }
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. public function getHash(CommandInterface $command)
  242. {
  243. $hash = $command->getHash();
  244. if (!isset($hash) && isset($this->commands[$cmdID = $command->getId()])) {
  245. $key = call_user_func($this->commands[$cmdID], $command);
  246. if (isset($key)) {
  247. $hash = $this->hashGenerator->hash($key);
  248. $command->setHash($hash);
  249. }
  250. }
  251. return $hash;
  252. }
  253. /**
  254. * {@inheritdoc}
  255. */
  256. public function getKeyHash($key)
  257. {
  258. return $this->hashGenerator->hash($key);
  259. }
  260. }