RedisStrategy.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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\ScriptedCommand;
  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
  147. * of type Predis\Command\CommandInterface.
  148. *
  149. * When the callback argument is omitted or NULL, the previously
  150. * associated handler for the specified command ID is removed.
  151. *
  152. * @param string $commandId The ID of the command to be handled.
  153. * @param mixed $callback A valid callable object or NULL.
  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("Callback must be a valid callable object or NULL");
  164. }
  165. $this->commands[$commandId] = $callback;
  166. }
  167. /**
  168. * Extracts the key from the first argument of a command instance.
  169. *
  170. * @param CommandInterface $command Command instance.
  171. * @return string
  172. */
  173. protected function getKeyFromFirstArgument(CommandInterface $command)
  174. {
  175. return $command->getArgument(0);
  176. }
  177. /**
  178. * Extracts the key from a command that can accept multiple keys ensuring
  179. * that only one key is actually specified to comply with redis-cluster.
  180. *
  181. * @param CommandInterface $command Command instance.
  182. * @return string
  183. */
  184. protected function getKeyFromAllArguments(CommandInterface $command)
  185. {
  186. $arguments = $command->getArguments();
  187. if (count($arguments) === 1) {
  188. return $arguments[0];
  189. }
  190. }
  191. /**
  192. * Extracts the key from a command that can accept multiple keys ensuring
  193. * that only one key is actually specified to comply with redis-cluster.
  194. *
  195. * @param CommandInterface $command Command instance.
  196. * @return string
  197. */
  198. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  199. {
  200. $arguments = $command->getArguments();
  201. if (count($arguments) === 2) {
  202. return $arguments[0];
  203. }
  204. }
  205. /**
  206. * Extracts the key from BLPOP and BRPOP commands ensuring that only one key
  207. * is actually specified to comply with redis-cluster.
  208. *
  209. * @param CommandInterface $command Command instance.
  210. * @return string
  211. */
  212. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  213. {
  214. $arguments = $command->getArguments();
  215. if (count($arguments) === 2) {
  216. return $arguments[0];
  217. }
  218. }
  219. /**
  220. * Extracts the key from EVAL and EVALSHA commands.
  221. *
  222. * @param CommandInterface $command Command instance.
  223. * @return string
  224. */
  225. protected function getKeyFromScriptingCommands(CommandInterface $command)
  226. {
  227. if ($command instanceof ScriptedCommand) {
  228. $keys = $command->getKeys();
  229. } else {
  230. $keys = array_slice($args = $command->getArguments(), 2, $args[1]);
  231. }
  232. if (count($keys) === 1) {
  233. return $keys[0];
  234. }
  235. }
  236. /**
  237. * {@inheritdoc}
  238. */
  239. public function getHash(CommandInterface $command)
  240. {
  241. $hash = $command->getHash();
  242. if (!isset($hash) && isset($this->commands[$cmdID = $command->getId()])) {
  243. $key = call_user_func($this->commands[$cmdID], $command);
  244. if (isset($key)) {
  245. $hash = $this->hashGenerator->hash($key);
  246. $command->setHash($hash);
  247. }
  248. }
  249. return $hash;
  250. }
  251. /**
  252. * {@inheritdoc}
  253. */
  254. public function getKeyHash($key)
  255. {
  256. return $this->hashGenerator->hash($key);
  257. }
  258. }