KeyPrefixProcessor.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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\Command\Processor;
  11. use Predis\Command\CommandInterface;
  12. use Predis\Command\PrefixableCommandInterface;
  13. /**
  14. * Command processor capable of prefixing keys stored in the arguments of Redis
  15. * commands supported.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class KeyPrefixProcessor implements ProcessorInterface
  20. {
  21. private $prefix;
  22. private $commands;
  23. /**
  24. * @param string $prefix Prefix for the keys.
  25. */
  26. public function __construct($prefix)
  27. {
  28. $this->prefix = $prefix;
  29. $this->commands = array(
  30. /* ---------------- Redis 1.2 ---------------- */
  31. 'EXISTS' => 'static::all',
  32. 'DEL' => 'static::all',
  33. 'TYPE' => 'static::first',
  34. 'KEYS' => 'static::first',
  35. 'RENAME' => 'static::all',
  36. 'RENAMENX' => 'static::all',
  37. 'EXPIRE' => 'static::first',
  38. 'EXPIREAT' => 'static::first',
  39. 'TTL' => 'static::first',
  40. 'MOVE' => 'static::first',
  41. 'SORT' => 'static::sort',
  42. 'DUMP' => 'static::first',
  43. 'RESTORE' => 'static::first',
  44. 'SET' => 'static::first',
  45. 'SETNX' => 'static::first',
  46. 'MSET' => 'static::interleaved',
  47. 'MSETNX' => 'static::interleaved',
  48. 'GET' => 'static::first',
  49. 'MGET' => 'static::all',
  50. 'GETSET' => 'static::first',
  51. 'INCR' => 'static::first',
  52. 'INCRBY' => 'static::first',
  53. 'DECR' => 'static::first',
  54. 'DECRBY' => 'static::first',
  55. 'RPUSH' => 'static::first',
  56. 'LPUSH' => 'static::first',
  57. 'LLEN' => 'static::first',
  58. 'LRANGE' => 'static::first',
  59. 'LTRIM' => 'static::first',
  60. 'LINDEX' => 'static::first',
  61. 'LSET' => 'static::first',
  62. 'LREM' => 'static::first',
  63. 'LPOP' => 'static::first',
  64. 'RPOP' => 'static::first',
  65. 'RPOPLPUSH' => 'static::all',
  66. 'SADD' => 'static::first',
  67. 'SREM' => 'static::first',
  68. 'SPOP' => 'static::first',
  69. 'SMOVE' => 'static::skipLast',
  70. 'SCARD' => 'static::first',
  71. 'SISMEMBER' => 'static::first',
  72. 'SINTER' => 'static::all',
  73. 'SINTERSTORE' => 'static::all',
  74. 'SUNION' => 'static::all',
  75. 'SUNIONSTORE' => 'static::all',
  76. 'SDIFF' => 'static::all',
  77. 'SDIFFSTORE' => 'static::all',
  78. 'SMEMBERS' => 'static::first',
  79. 'SRANDMEMBER' => 'static::first',
  80. 'ZADD' => 'static::first',
  81. 'ZINCRBY' => 'static::first',
  82. 'ZREM' => 'static::first',
  83. 'ZRANGE' => 'static::first',
  84. 'ZREVRANGE' => 'static::first',
  85. 'ZRANGEBYSCORE' => 'static::first',
  86. 'ZCARD' => 'static::first',
  87. 'ZSCORE' => 'static::first',
  88. 'ZREMRANGEBYSCORE' => 'static::first',
  89. /* ---------------- Redis 2.0 ---------------- */
  90. 'SETEX' => 'static::first',
  91. 'APPEND' => 'static::first',
  92. 'SUBSTR' => 'static::first',
  93. 'BLPOP' => 'static::skipLast',
  94. 'BRPOP' => 'static::skipLast',
  95. 'ZUNIONSTORE' => 'static::zsetStore',
  96. 'ZINTERSTORE' => 'static::zsetStore',
  97. 'ZCOUNT' => 'static::first',
  98. 'ZRANK' => 'static::first',
  99. 'ZREVRANK' => 'static::first',
  100. 'ZREMRANGEBYRANK' => 'static::first',
  101. 'HSET' => 'static::first',
  102. 'HSETNX' => 'static::first',
  103. 'HMSET' => 'static::first',
  104. 'HINCRBY' => 'static::first',
  105. 'HGET' => 'static::first',
  106. 'HMGET' => 'static::first',
  107. 'HDEL' => 'static::first',
  108. 'HEXISTS' => 'static::first',
  109. 'HLEN' => 'static::first',
  110. 'HKEYS' => 'static::first',
  111. 'HVALS' => 'static::first',
  112. 'HGETALL' => 'static::first',
  113. 'SUBSCRIBE' => 'static::all',
  114. 'UNSUBSCRIBE' => 'static::all',
  115. 'PSUBSCRIBE' => 'static::all',
  116. 'PUNSUBSCRIBE' => 'static::all',
  117. 'PUBLISH' => 'static::first',
  118. /* ---------------- Redis 2.2 ---------------- */
  119. 'PERSIST' => 'static::first',
  120. 'STRLEN' => 'static::first',
  121. 'SETRANGE' => 'static::first',
  122. 'GETRANGE' => 'static::first',
  123. 'SETBIT' => 'static::first',
  124. 'GETBIT' => 'static::first',
  125. 'RPUSHX' => 'static::first',
  126. 'LPUSHX' => 'static::first',
  127. 'LINSERT' => 'static::first',
  128. 'BRPOPLPUSH' => 'static::skipLast',
  129. 'ZREVRANGEBYSCORE' => 'static::first',
  130. 'WATCH' => 'static::all',
  131. /* ---------------- Redis 2.6 ---------------- */
  132. 'PTTL' => 'static::first',
  133. 'PEXPIRE' => 'static::first',
  134. 'PEXPIREAT' => 'static::first',
  135. 'PSETEX' => 'static::first',
  136. 'INCRBYFLOAT' => 'static::first',
  137. 'BITOP' => 'static::skipFirst',
  138. 'BITCOUNT' => 'static::first',
  139. 'HINCRBYFLOAT' => 'static::first',
  140. 'EVAL' => 'static::evalKeys',
  141. 'EVALSHA' => 'static::evalKeys',
  142. 'MIGRATE' => 'static::migrate',
  143. /* ---------------- Redis 2.8 ---------------- */
  144. 'SSCAN' => 'static::first',
  145. 'ZSCAN' => 'static::first',
  146. 'HSCAN' => 'static::first',
  147. 'PFADD' => 'static::first',
  148. 'PFCOUNT' => 'static::all',
  149. 'PFMERGE' => 'static::all',
  150. 'ZLEXCOUNT' => 'static::first',
  151. 'ZRANGEBYLEX' => 'static::first',
  152. 'ZREMRANGEBYLEX' => 'static::first',
  153. 'ZREVRANGEBYLEX' => 'static::first',
  154. 'BITPOS' => 'static::first',
  155. /* ---------------- Redis 3.2 ---------------- */
  156. 'HSTRLEN' => 'static::first',
  157. 'BITFIELD' => 'static::first',
  158. 'GEOADD' => 'static::first',
  159. 'GEOHASH' => 'static::first',
  160. 'GEOPOS' => 'static::first',
  161. 'GEODIST' => 'static::first',
  162. 'GEORADIUS' => 'static::georadius',
  163. );
  164. }
  165. /**
  166. * Sets a prefix that is applied to all the keys.
  167. *
  168. * @param string $prefix Prefix for the keys.
  169. */
  170. public function setPrefix($prefix)
  171. {
  172. $this->prefix = $prefix;
  173. }
  174. /**
  175. * Gets the current prefix.
  176. *
  177. * @return string
  178. */
  179. public function getPrefix()
  180. {
  181. return $this->prefix;
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function process(CommandInterface $command)
  187. {
  188. if ($command instanceof PrefixableCommandInterface) {
  189. $command->prefixKeys($this->prefix);
  190. } elseif (isset($this->commands[$commandID = strtoupper($command->getId())])) {
  191. call_user_func($this->commands[$commandID], $command, $this->prefix);
  192. }
  193. }
  194. /**
  195. * Sets an handler for the specified command ID.
  196. *
  197. * The callback signature must have 2 parameters of the following types:
  198. *
  199. * - Predis\Command\CommandInterface (command instance)
  200. * - String (prefix)
  201. *
  202. * When the callback argument is omitted or NULL, the previously
  203. * associated handler for the specified command ID is removed.
  204. *
  205. * @param string $commandID The ID of the command to be handled.
  206. * @param mixed $callback A valid callable object or NULL.
  207. *
  208. * @throws \InvalidArgumentException
  209. */
  210. public function setCommandHandler($commandID, $callback = null)
  211. {
  212. $commandID = strtoupper($commandID);
  213. if (!isset($callback)) {
  214. unset($this->commands[$commandID]);
  215. return;
  216. }
  217. if (!is_callable($callback)) {
  218. throw new \InvalidArgumentException(
  219. 'Callback must be a valid callable object or NULL'
  220. );
  221. }
  222. $this->commands[$commandID] = $callback;
  223. }
  224. /**
  225. * {@inheritdoc}
  226. */
  227. public function __toString()
  228. {
  229. return $this->getPrefix();
  230. }
  231. /**
  232. * Applies the specified prefix only the first argument.
  233. *
  234. * @param CommandInterface $command Command instance.
  235. * @param string $prefix Prefix string.
  236. */
  237. public static function first(CommandInterface $command, $prefix)
  238. {
  239. if ($arguments = $command->getArguments()) {
  240. $arguments[0] = "$prefix{$arguments[0]}";
  241. $command->setRawArguments($arguments);
  242. }
  243. }
  244. /**
  245. * Applies the specified prefix to all the arguments.
  246. *
  247. * @param CommandInterface $command Command instance.
  248. * @param string $prefix Prefix string.
  249. */
  250. public static function all(CommandInterface $command, $prefix)
  251. {
  252. if ($arguments = $command->getArguments()) {
  253. foreach ($arguments as &$key) {
  254. $key = "$prefix$key";
  255. }
  256. $command->setRawArguments($arguments);
  257. }
  258. }
  259. /**
  260. * Applies the specified prefix only to even arguments in the list.
  261. *
  262. * @param CommandInterface $command Command instance.
  263. * @param string $prefix Prefix string.
  264. */
  265. public static function interleaved(CommandInterface $command, $prefix)
  266. {
  267. if ($arguments = $command->getArguments()) {
  268. $length = count($arguments);
  269. for ($i = 0; $i < $length; $i += 2) {
  270. $arguments[$i] = "$prefix{$arguments[$i]}";
  271. }
  272. $command->setRawArguments($arguments);
  273. }
  274. }
  275. /**
  276. * Applies the specified prefix to all the arguments but the first one.
  277. *
  278. * @param CommandInterface $command Command instance.
  279. * @param string $prefix Prefix string.
  280. */
  281. public static function skipFirst(CommandInterface $command, $prefix)
  282. {
  283. if ($arguments = $command->getArguments()) {
  284. $length = count($arguments);
  285. for ($i = 1; $i < $length; ++$i) {
  286. $arguments[$i] = "$prefix{$arguments[$i]}";
  287. }
  288. $command->setRawArguments($arguments);
  289. }
  290. }
  291. /**
  292. * Applies the specified prefix to all the arguments but the last one.
  293. *
  294. * @param CommandInterface $command Command instance.
  295. * @param string $prefix Prefix string.
  296. */
  297. public static function skipLast(CommandInterface $command, $prefix)
  298. {
  299. if ($arguments = $command->getArguments()) {
  300. $length = count($arguments);
  301. for ($i = 0; $i < $length - 1; ++$i) {
  302. $arguments[$i] = "$prefix{$arguments[$i]}";
  303. }
  304. $command->setRawArguments($arguments);
  305. }
  306. }
  307. /**
  308. * Applies the specified prefix to the keys of a SORT command.
  309. *
  310. * @param CommandInterface $command Command instance.
  311. * @param string $prefix Prefix string.
  312. */
  313. public static function sort(CommandInterface $command, $prefix)
  314. {
  315. if ($arguments = $command->getArguments()) {
  316. $arguments[0] = "$prefix{$arguments[0]}";
  317. if (($count = count($arguments)) > 1) {
  318. for ($i = 1; $i < $count; ++$i) {
  319. switch ($arguments[$i]) {
  320. case 'BY':
  321. case 'STORE':
  322. $arguments[$i] = "$prefix{$arguments[++$i]}";
  323. break;
  324. case 'GET':
  325. $value = $arguments[++$i];
  326. if ($value !== '#') {
  327. $arguments[$i] = "$prefix$value";
  328. }
  329. break;
  330. case 'LIMIT';
  331. $i += 2;
  332. break;
  333. }
  334. }
  335. }
  336. $command->setRawArguments($arguments);
  337. }
  338. }
  339. /**
  340. * Applies the specified prefix to the keys of an EVAL-based command.
  341. *
  342. * @param CommandInterface $command Command instance.
  343. * @param string $prefix Prefix string.
  344. */
  345. public static function evalKeys(CommandInterface $command, $prefix)
  346. {
  347. if ($arguments = $command->getArguments()) {
  348. for ($i = 2; $i < $arguments[1] + 2; ++$i) {
  349. $arguments[$i] = "$prefix{$arguments[$i]}";
  350. }
  351. $command->setRawArguments($arguments);
  352. }
  353. }
  354. /**
  355. * Applies the specified prefix to the keys of Z[INTERSECTION|UNION]STORE.
  356. *
  357. * @param CommandInterface $command Command instance.
  358. * @param string $prefix Prefix string.
  359. */
  360. public static function zsetStore(CommandInterface $command, $prefix)
  361. {
  362. if ($arguments = $command->getArguments()) {
  363. $arguments[0] = "$prefix{$arguments[0]}";
  364. $length = ((int) $arguments[1]) + 2;
  365. for ($i = 2; $i < $length; ++$i) {
  366. $arguments[$i] = "$prefix{$arguments[$i]}";
  367. }
  368. $command->setRawArguments($arguments);
  369. }
  370. }
  371. /**
  372. * Applies the specified prefix to the key of a MIGRATE command.
  373. *
  374. * @param CommandInterface $command Command instance.
  375. * @param string $prefix Prefix string.
  376. */
  377. public static function migrate(CommandInterface $command, $prefix)
  378. {
  379. if ($arguments = $command->getArguments()) {
  380. $arguments[2] = "$prefix{$arguments[2]}";
  381. $command->setRawArguments($arguments);
  382. }
  383. }
  384. /**
  385. * Applies the specified prefix to the key of a GEORADIUS command.
  386. *
  387. * @param CommandInterface $command Command instance.
  388. * @param string $prefix Prefix string.
  389. */
  390. public static function georadius(CommandInterface $command, $prefix)
  391. {
  392. if ($arguments = $command->getArguments()) {
  393. $arguments[0] = "$prefix{$arguments[0]}";
  394. if (($count = count($arguments)) > 5) {
  395. for ($i = 5; $i < $count; ++$i) {
  396. switch (strtoupper($arguments[$i])) {
  397. case 'STORE':
  398. case 'STOREDIST':
  399. $arguments[$i] = "$prefix{$arguments[++$i]}";
  400. break;
  401. }
  402. }
  403. }
  404. $command->setRawArguments($arguments);
  405. }
  406. }
  407. }