KeyPrefixProcessor.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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::first',
  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. );
  158. }
  159. /**
  160. * Sets a prefix that is applied to all the keys.
  161. *
  162. * @param string $prefix Prefix for the keys.
  163. */
  164. public function setPrefix($prefix)
  165. {
  166. $this->prefix = $prefix;
  167. }
  168. /**
  169. * Gets the current prefix.
  170. *
  171. * @return string
  172. */
  173. public function getPrefix()
  174. {
  175. return $this->prefix;
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function process(CommandInterface $command)
  181. {
  182. if ($command instanceof PrefixableCommandInterface) {
  183. $command->prefixKeys($this->prefix);
  184. } elseif (isset($this->commands[$commandID = strtoupper($command->getId())])) {
  185. call_user_func($this->commands[$commandID], $command, $this->prefix);
  186. }
  187. }
  188. /**
  189. * Sets an handler for the specified command ID.
  190. *
  191. * The callback signature must have 2 parameters of the following types:
  192. *
  193. * - Predis\Command\CommandInterface (command instance)
  194. * - String (prefix)
  195. *
  196. * When the callback argument is omitted or NULL, the previously
  197. * associated handler for the specified command ID is removed.
  198. *
  199. * @param string $commandID The ID of the command to be handled.
  200. * @param mixed $callback A valid callable object or NULL.
  201. *
  202. * @throws \InvalidArgumentException
  203. */
  204. public function setCommandHandler($commandID, $callback = null)
  205. {
  206. $commandID = strtoupper($commandID);
  207. if (!isset($callback)) {
  208. unset($this->commands[$commandID]);
  209. return;
  210. }
  211. if (!is_callable($callback)) {
  212. throw new \InvalidArgumentException(
  213. 'Callback must be a valid callable object or NULL'
  214. );
  215. }
  216. $this->commands[$commandID] = $callback;
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function __toString()
  222. {
  223. return $this->getPrefix();
  224. }
  225. /**
  226. * Applies the specified prefix only the first argument.
  227. *
  228. * @param CommandInterface $command Command instance.
  229. * @param string $prefix Prefix string.
  230. */
  231. public static function first(CommandInterface $command, $prefix)
  232. {
  233. if ($arguments = $command->getArguments()) {
  234. $arguments[0] = "$prefix{$arguments[0]}";
  235. $command->setRawArguments($arguments);
  236. }
  237. }
  238. /**
  239. * Applies the specified prefix to all the arguments.
  240. *
  241. * @param CommandInterface $command Command instance.
  242. * @param string $prefix Prefix string.
  243. */
  244. public static function all(CommandInterface $command, $prefix)
  245. {
  246. if ($arguments = $command->getArguments()) {
  247. foreach ($arguments as &$key) {
  248. $key = "$prefix$key";
  249. }
  250. $command->setRawArguments($arguments);
  251. }
  252. }
  253. /**
  254. * Applies the specified prefix only to even arguments in the list.
  255. *
  256. * @param CommandInterface $command Command instance.
  257. * @param string $prefix Prefix string.
  258. */
  259. public static function interleaved(CommandInterface $command, $prefix)
  260. {
  261. if ($arguments = $command->getArguments()) {
  262. $length = count($arguments);
  263. for ($i = 0; $i < $length; $i += 2) {
  264. $arguments[$i] = "$prefix{$arguments[$i]}";
  265. }
  266. $command->setRawArguments($arguments);
  267. }
  268. }
  269. /**
  270. * Applies the specified prefix to all the arguments but the first one.
  271. *
  272. * @param CommandInterface $command Command instance.
  273. * @param string $prefix Prefix string.
  274. */
  275. public static function skipFirst(CommandInterface $command, $prefix)
  276. {
  277. if ($arguments = $command->getArguments()) {
  278. $length = count($arguments);
  279. for ($i = 1; $i < $length; ++$i) {
  280. $arguments[$i] = "$prefix{$arguments[$i]}";
  281. }
  282. $command->setRawArguments($arguments);
  283. }
  284. }
  285. /**
  286. * Applies the specified prefix to all the arguments but the last one.
  287. *
  288. * @param CommandInterface $command Command instance.
  289. * @param string $prefix Prefix string.
  290. */
  291. public static function skipLast(CommandInterface $command, $prefix)
  292. {
  293. if ($arguments = $command->getArguments()) {
  294. $length = count($arguments);
  295. for ($i = 0; $i < $length - 1; ++$i) {
  296. $arguments[$i] = "$prefix{$arguments[$i]}";
  297. }
  298. $command->setRawArguments($arguments);
  299. }
  300. }
  301. /**
  302. * Applies the specified prefix to the keys of a SORT command.
  303. *
  304. * @param CommandInterface $command Command instance.
  305. * @param string $prefix Prefix string.
  306. */
  307. public static function sort(CommandInterface $command, $prefix)
  308. {
  309. if ($arguments = $command->getArguments()) {
  310. $arguments[0] = "$prefix{$arguments[0]}";
  311. if (($count = count($arguments)) > 1) {
  312. for ($i = 1; $i < $count; ++$i) {
  313. switch ($arguments[$i]) {
  314. case 'BY':
  315. case 'STORE':
  316. $arguments[$i] = "$prefix{$arguments[++$i]}";
  317. break;
  318. case 'GET':
  319. $value = $arguments[++$i];
  320. if ($value !== '#') {
  321. $arguments[$i] = "$prefix$value";
  322. }
  323. break;
  324. case 'LIMIT';
  325. $i += 2;
  326. break;
  327. }
  328. }
  329. }
  330. $command->setRawArguments($arguments);
  331. }
  332. }
  333. /**
  334. * Applies the specified prefix to the keys of an EVAL-based command.
  335. *
  336. * @param CommandInterface $command Command instance.
  337. * @param string $prefix Prefix string.
  338. */
  339. public static function evalKeys(CommandInterface $command, $prefix)
  340. {
  341. if ($arguments = $command->getArguments()) {
  342. for ($i = 2; $i < $arguments[1] + 2; ++$i) {
  343. $arguments[$i] = "$prefix{$arguments[$i]}";
  344. }
  345. $command->setRawArguments($arguments);
  346. }
  347. }
  348. /**
  349. * Applies the specified prefix to the keys of Z[INTERSECTION|UNION]STORE.
  350. *
  351. * @param CommandInterface $command Command instance.
  352. * @param string $prefix Prefix string.
  353. */
  354. public static function zsetStore(CommandInterface $command, $prefix)
  355. {
  356. if ($arguments = $command->getArguments()) {
  357. $arguments[0] = "$prefix{$arguments[0]}";
  358. $length = ((int) $arguments[1]) + 2;
  359. for ($i = 2; $i < $length; ++$i) {
  360. $arguments[$i] = "$prefix{$arguments[$i]}";
  361. }
  362. $command->setRawArguments($arguments);
  363. }
  364. }
  365. /**
  366. * Applies the specified prefix to the key of a MIGRATE command.
  367. *
  368. * @param CommandInterface $command Command instance.
  369. * @param string $prefix Prefix string.
  370. */
  371. public static function migrate(CommandInterface $command, $prefix)
  372. {
  373. if ($arguments = $command->getArguments()) {
  374. $arguments[2] = "$prefix{$arguments[2]}";
  375. $command->setRawArguments($arguments);
  376. }
  377. }
  378. }