RedisStrategy.php 12 KB

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