RedisStrategy.php 12 KB

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