Predis.php 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  1. <?php
  2. namespace Predis;
  3. class PredisException extends \Exception { }
  4. class ClientException extends PredisException { }
  5. class ServerException extends PredisException { }
  6. class MalformedServerResponse extends ServerException { }
  7. /* ------------------------------------------------------------------------- */
  8. class Client {
  9. private $_connection, $_serverProfile, $_responseReader;
  10. public function __construct($parameters = null, RedisServerProfile $serverProfile = null) {
  11. $this->_responseReader = new ResponseReader();
  12. $this->setProfile($serverProfile ?: RedisServerProfile::getDefault());
  13. $this->setupConnection($parameters);
  14. }
  15. public function __destruct() {
  16. $this->_connection->disconnect();
  17. }
  18. public static function create(/* arguments */) {
  19. $argv = func_get_args();
  20. $argc = func_num_args();
  21. $serverProfile = null;
  22. $lastArg = $argv[$argc-1];
  23. if ($argc > 0 && !is_string($lastArg) && is_subclass_of($lastArg, '\Predis\RedisServerProfile')) {
  24. $serverProfile = array_pop($argv);
  25. $argc--;
  26. }
  27. if ($argc === 0) {
  28. throw new ClientException('Missing connection parameters');
  29. }
  30. return new Client($argc === 1 ? $argv[0] : $argv, $serverProfile);
  31. }
  32. private function setupConnection($parameters) {
  33. if ($parameters !== null && !(is_array($parameters) || is_string($parameters))) {
  34. throw new ClientException('Invalid parameters type (array or string expected)');
  35. }
  36. if (is_array($parameters) && isset($parameters[0])) {
  37. $cluster = new ConnectionCluster();
  38. foreach ($parameters as $shardParams) {
  39. $cluster->add($this->createConnection($shardParams));
  40. }
  41. $this->setConnection($cluster);
  42. }
  43. else {
  44. $this->setConnection($this->createConnection($parameters));
  45. }
  46. }
  47. private function createConnection($parameters) {
  48. $params = new ConnectionParameters($parameters);
  49. $connection = new Connection($params, $this->_responseReader);
  50. if ($params->password !== null) {
  51. $connection->pushInitCommand($this->createCommand(
  52. 'auth', array($params->password)
  53. ));
  54. }
  55. if ($params->database !== null) {
  56. $connection->pushInitCommand($this->createCommand(
  57. 'select', array($params->database)
  58. ));
  59. }
  60. return $connection;
  61. }
  62. private function setConnection(IConnection $connection) {
  63. $this->_connection = $connection;
  64. }
  65. public function setProfile(RedisServerProfile $serverProfile) {
  66. $this->_serverProfile = $serverProfile;
  67. }
  68. public function getProfile() {
  69. return $this->_serverProfile;
  70. }
  71. public function getResponseReader() {
  72. return $this->_responseReader;
  73. }
  74. public function connect() {
  75. $this->_connection->connect();
  76. }
  77. public function disconnect() {
  78. $this->_connection->disconnect();
  79. }
  80. public function isConnected() {
  81. return $this->_connection->isConnected();
  82. }
  83. public function getConnection($id = null) {
  84. if (!isset($id)) {
  85. return $this->_connection;
  86. }
  87. else {
  88. return $this->_connection instanceof ConnectionCluster
  89. ? $this->_connection->getConnectionById($id)
  90. : $this->_connection;
  91. }
  92. }
  93. public function __call($method, $arguments) {
  94. $command = $this->_serverProfile->createCommand($method, $arguments);
  95. return $this->_connection->executeCommand($command);
  96. }
  97. public function createCommand($method, $arguments = array()) {
  98. return $this->_serverProfile->createCommand($method, $arguments);
  99. }
  100. public function executeCommand(Command $command) {
  101. return $this->_connection->executeCommand($command);
  102. }
  103. public function executeCommandOnShards(Command $command) {
  104. $replies = array();
  105. if ($this->_connection instanceof \Predis\ConnectionCluster) {
  106. foreach($this->_connection as $connection) {
  107. $replies[] = $connection->executeCommand($command);
  108. }
  109. }
  110. else {
  111. $replies[] = $this->_connection->executeCommand($command);
  112. }
  113. return $replies;
  114. }
  115. public function rawCommand($rawCommandData, $closesConnection = false) {
  116. if ($this->_connection instanceof \Predis\ConnectionCluster) {
  117. throw new ClientException('Cannot send raw commands when connected to a cluster of Redis servers');
  118. }
  119. return $this->_connection->rawCommand($rawCommandData, $closesConnection);
  120. }
  121. public function pipeline($pipelineBlock = null) {
  122. $pipeline = new CommandPipeline($this);
  123. return $pipelineBlock !== null ? $pipeline->execute($pipelineBlock) : $pipeline;
  124. }
  125. public function multiExec($multiExecBlock = null) {
  126. $multiExec = new MultiExecBlock($this);
  127. return $multiExecBlock !== null ? $multiExec->execute($multiExecBlock) : $multiExec;
  128. }
  129. }
  130. /* ------------------------------------------------------------------------- */
  131. abstract class Command {
  132. private $_arguments, $_hash;
  133. public abstract function getCommandId();
  134. public abstract function serializeRequest($command, $arguments);
  135. public function canBeHashed() {
  136. return true;
  137. }
  138. public function getHash() {
  139. if (isset($this->_hash)) {
  140. return $this->_hash;
  141. }
  142. else {
  143. if (isset($this->_arguments[0])) {
  144. $key = $this->_arguments[0];
  145. $start = strpos($key, '{');
  146. $end = strpos($key, '}');
  147. if ($start !== false && $end !== false) {
  148. $key = substr($key, ++$start, $end - $start);
  149. }
  150. $this->_hash = crc32($key);
  151. return $this->_hash;
  152. }
  153. }
  154. return null;
  155. }
  156. public function closesConnection() {
  157. return false;
  158. }
  159. protected function filterArguments(Array $arguments) {
  160. return $arguments;
  161. }
  162. public function setArguments(/* arguments */) {
  163. $this->_arguments = $this->filterArguments(func_get_args());
  164. }
  165. public function setArgumentsArray(Array $arguments) {
  166. $this->_arguments = $this->filterArguments($arguments);
  167. }
  168. protected function getArguments() {
  169. // TODO: why getArguments is protected?
  170. return isset($this->_arguments) ? $this->_arguments : array();
  171. }
  172. public function getArgument($index = 0) {
  173. return isset($this->_arguments[$index]) ? $this->_arguments[$index] : null;
  174. }
  175. public function parseResponse($data) {
  176. return $data;
  177. }
  178. public final function __invoke() {
  179. return $this->serializeRequest($this->getCommandId(), $this->getArguments());
  180. }
  181. }
  182. abstract class InlineCommand extends Command {
  183. public function serializeRequest($command, $arguments) {
  184. if (isset($arguments[0]) && is_array($arguments[0])) {
  185. $arguments[0] = implode($arguments[0], ' ');
  186. }
  187. return $command . ' ' . implode($arguments, ' ') . ResponseReader::NEWLINE;
  188. }
  189. }
  190. abstract class BulkCommand extends Command {
  191. public function serializeRequest($command, $arguments) {
  192. $data = array_pop($arguments);
  193. if (is_array($data)) {
  194. $data = implode($data, ' ');
  195. }
  196. return $command . ' ' . implode($arguments, ' ') . ' ' . strlen($data) .
  197. ResponseReader::NEWLINE . $data . ResponseReader::NEWLINE;
  198. }
  199. }
  200. abstract class MultiBulkCommand extends Command {
  201. public function serializeRequest($command, $arguments) {
  202. $buffer = array();
  203. $cmd_args = null;
  204. if (count($arguments) === 1 && is_array($arguments[0])) {
  205. $cmd_args = array();
  206. foreach ($arguments[0] as $k => $v) {
  207. $cmd_args[] = $k;
  208. $cmd_args[] = $v;
  209. }
  210. }
  211. else {
  212. $cmd_args = $arguments;
  213. }
  214. $buffer[] = '*' . ((string) count($cmd_args) + 1) . ResponseReader::NEWLINE;
  215. $buffer[] = '$' . strlen($command) . ResponseReader::NEWLINE . $command . ResponseReader::NEWLINE;
  216. foreach ($cmd_args as $argument) {
  217. $buffer[] = '$' . strlen($argument) . ResponseReader::NEWLINE . $argument . ResponseReader::NEWLINE;
  218. }
  219. return implode('', $buffer);
  220. }
  221. }
  222. /* ------------------------------------------------------------------------- */
  223. interface IResponseHandler {
  224. function handle(Connection $connection, $payload);
  225. }
  226. class ResponseStatusHandler implements IResponseHandler {
  227. public function handle(Connection $connection, $status) {
  228. if ($status === ResponseReader::OK) {
  229. return true;
  230. }
  231. else if ($status === ResponseReader::QUEUED) {
  232. return new ResponseQueued();
  233. }
  234. return $status;
  235. }
  236. }
  237. class ResponseErrorHandler implements IResponseHandler {
  238. public function handle(Connection $connection, $errorMessage) {
  239. throw new ServerException(substr($errorMessage, 4));
  240. }
  241. }
  242. class ResponseErrorSilentHandler implements IResponseHandler {
  243. public function handle(Connection $connection, $errorMessage) {
  244. return new ResponseError(substr($errorMessage, 4));
  245. }
  246. }
  247. class ResponseBulkHandler implements IResponseHandler {
  248. public function handle(Connection $connection, $dataLength) {
  249. $socket = $connection->getSocket();
  250. if (!is_numeric($dataLength)) {
  251. throw new ClientException("Cannot parse '$dataLength' as data length");
  252. }
  253. if ($dataLength > 0) {
  254. $value = stream_get_contents($socket, $dataLength);
  255. if ($value === false) {
  256. throw new ClientException('An error has occurred while reading from the network stream');
  257. }
  258. fread($socket, 2);
  259. return $value;
  260. }
  261. else if ($dataLength == 0) {
  262. fread($socket, 2);
  263. return '';
  264. }
  265. return null;
  266. }
  267. }
  268. class ResponseMultiBulkHandler implements IResponseHandler {
  269. public function handle(Connection $connection, $rawLength) {
  270. if (!is_numeric($rawLength)) {
  271. throw new ClientException("Cannot parse '$rawLength' as data length");
  272. }
  273. $listLength = (int) $rawLength;
  274. if ($listLength === -1) {
  275. return null;
  276. }
  277. $list = array();
  278. if ($listLength > 0) {
  279. for ($i = 0; $i < $listLength; $i++) {
  280. $list[] = $connection->getResponseReader()->read($connection);
  281. }
  282. }
  283. return $list;
  284. }
  285. }
  286. class ResponseMultiBulkStreamHandler implements IResponseHandler {
  287. public function handle(Connection $connection, $rawLength) {
  288. if (!is_numeric($rawLength)) {
  289. throw new ClientException("Cannot parse '$rawLength' as data length");
  290. }
  291. return new Utilities\MultiBulkResponseIterator($connection, (int)$rawLength);
  292. }
  293. }
  294. class ResponseIntegerHandler implements IResponseHandler {
  295. public function handle(Connection $connection, $number) {
  296. if (is_numeric($number)) {
  297. return (int) $number;
  298. }
  299. else {
  300. if ($number !== ResponseReader::NULL) {
  301. throw new ClientException("Cannot parse '$number' as numeric response");
  302. }
  303. return null;
  304. }
  305. }
  306. }
  307. class ResponseReader {
  308. const NEWLINE = "\r\n";
  309. const OK = 'OK';
  310. const ERROR = 'ERR';
  311. const QUEUED = 'QUEUED';
  312. const NULL = 'nil';
  313. const PREFIX_STATUS = '+';
  314. const PREFIX_ERROR = '-';
  315. const PREFIX_INTEGER = ':';
  316. const PREFIX_BULK = '$';
  317. const PREFIX_MULTI_BULK = '*';
  318. private $_prefixHandlers;
  319. public function __construct() {
  320. $this->initializePrefixHandlers();
  321. }
  322. private function initializePrefixHandlers() {
  323. $this->_prefixHandlers = array(
  324. self::PREFIX_STATUS => new ResponseStatusHandler(),
  325. self::PREFIX_ERROR => new ResponseErrorHandler(),
  326. self::PREFIX_INTEGER => new ResponseIntegerHandler(),
  327. self::PREFIX_BULK => new ResponseBulkHandler(),
  328. self::PREFIX_MULTI_BULK => new ResponseMultiBulkHandler(),
  329. );
  330. }
  331. private function setHandler($prefix, IResponseHandler $handler) {
  332. $this->_prefixHandlers[$prefix] = $handler;
  333. }
  334. public function setOption($option, $value) {
  335. switch ($option) {
  336. case 'iterable_multibulk_replies':
  337. case 'iterableMultiBulkReplies':
  338. $this->setHandler(self::PREFIX_MULTI_BULK, $value == true
  339. ? new ResponseMultiBulkStreamHandler()
  340. : new ResponseMultiBulkHandler()
  341. );
  342. break;
  343. case 'errorThrowException':
  344. case 'error_throw_exception':
  345. $this->setHandler(self::PREFIX_ERROR, $value == true
  346. ? new ResponseErrorHandler()
  347. : new ResponseErrorSilentHandler()
  348. );
  349. break;
  350. default:
  351. throw new \InvalidArgumentException("Unknown option: $option");
  352. }
  353. }
  354. public function getOption($option) {
  355. switch ($option) {
  356. case 'iterable_multibulk_replies':
  357. case 'iterableMultiBulkReplies':
  358. return $this->_prefixHandlers[self::PREFIX_MULTI_BULK]
  359. instanceof ResponseMultiBulkStreamHandler;
  360. case 'errorThrowException':
  361. case 'error_throw_exception':
  362. return $this->_prefixHandlers[self::PREFIX_ERROR]
  363. instanceof ResponseErrorHandler;
  364. default:
  365. throw new \InvalidArgumentException("Unknown option: $option");
  366. }
  367. }
  368. public function read(Connection $connection) {
  369. $header = fgets($connection->getSocket());
  370. if ($header === false) {
  371. throw new ClientException('An error has occurred while reading from the network stream');
  372. }
  373. $prefix = $header[0];
  374. $payload = substr($header, 1, -2);
  375. if (!isset($this->_prefixHandlers[$prefix])) {
  376. throw new MalformedServerResponse("Unknown prefix '$prefix'");
  377. }
  378. $handler = $this->_prefixHandlers[$prefix];
  379. return $handler->handle($connection, $payload);
  380. }
  381. }
  382. class ResponseError {
  383. private $_message;
  384. public function __construct($message) {
  385. $this->_message = $message;
  386. }
  387. public function __get($property) {
  388. if ($property == 'error') {
  389. return true;
  390. }
  391. if ($property == 'message') {
  392. return $this->_message;
  393. }
  394. }
  395. public function __isset($property) {
  396. return $property === 'error';
  397. }
  398. public function __toString() {
  399. return $this->_message;
  400. }
  401. }
  402. class ResponseQueued {
  403. public $queued = true;
  404. public function __toString() {
  405. return ResponseReader::QUEUED;
  406. }
  407. }
  408. /* ------------------------------------------------------------------------- */
  409. class CommandPipeline {
  410. private $_redisClient, $_pipelineBuffer, $_returnValues, $_running;
  411. public function __construct(Client $redisClient) {
  412. $this->_redisClient = $redisClient;
  413. $this->_pipelineBuffer = array();
  414. $this->_returnValues = array();
  415. }
  416. public function __call($method, $arguments) {
  417. $command = $this->_redisClient->createCommand($method, $arguments);
  418. $this->recordCommand($command);
  419. return $this;
  420. }
  421. private function recordCommand(Command $command) {
  422. $this->_pipelineBuffer[] = $command;
  423. }
  424. private function getRecordedCommands() {
  425. return $this->_pipelineBuffer;
  426. }
  427. public function flushPipeline() {
  428. $sizeofPipe = count($this->_pipelineBuffer);
  429. if ($sizeofPipe === 0) {
  430. return;
  431. }
  432. $connection = $this->_redisClient->getConnection();
  433. $commands = &$this->_pipelineBuffer;
  434. foreach ($commands as $command) {
  435. $connection->writeCommand($command);
  436. }
  437. for ($i = 0; $i < $sizeofPipe; $i++) {
  438. $response = $connection->readResponse($commands[$i]);
  439. $this->_returnValues[] = ($response instanceof \Iterator
  440. ? iterator_to_array($response)
  441. : $response
  442. );
  443. unset($commands[$i]);
  444. }
  445. $this->_pipelineBuffer = array();
  446. return $this;
  447. }
  448. private function setRunning($bool) {
  449. if ($bool == true && $this->_running == true) {
  450. throw new ClientException("This pipeline is already opened");
  451. }
  452. $this->_running = $bool;
  453. }
  454. public function execute($block = null) {
  455. if ($block && !is_callable($block)) {
  456. throw new \RuntimeException('Argument passed must be a callable object');
  457. }
  458. // TODO: do not reuse previously executed pipelines
  459. $this->setRunning(true);
  460. $pipelineBlockException = null;
  461. try {
  462. if ($block !== null) {
  463. $block($this);
  464. }
  465. $this->flushPipeline();
  466. }
  467. catch (\Exception $exception) {
  468. // TODO: client/server desync on ServerException
  469. $pipelineBlockException = $exception;
  470. }
  471. $this->setRunning(false);
  472. if ($pipelineBlockException !== null) {
  473. throw $pipelineBlockException;
  474. }
  475. return $this->_returnValues;
  476. }
  477. }
  478. class MultiExecBlock {
  479. private $_redisClient, $_commands, $_initialized, $_discarded;
  480. public function __construct(Client $redisClient) {
  481. $this->_initialized = false;
  482. $this->_discarded = false;
  483. $this->_redisClient = $redisClient;
  484. $this->_commands = array();
  485. }
  486. private function initialize() {
  487. if ($this->_initialized === false) {
  488. $this->_redisClient->multi();
  489. $this->_initialized = true;
  490. $this->_discarded = false;
  491. }
  492. }
  493. public function __call($method, $arguments) {
  494. $this->initialize();
  495. $command = $this->_redisClient->createCommand($method, $arguments);
  496. $response = $this->_redisClient->executeCommand($command);
  497. if (isset($response->queued)) {
  498. $this->_commands[] = $command;
  499. return $this;
  500. }
  501. else {
  502. throw new ClientException('The server did not respond with a QUEUED status reply');
  503. }
  504. }
  505. public function discard() {
  506. $this->_redisClient->discard();
  507. $this->_commands = array();
  508. $this->_initialized = false;
  509. $this->_discarded = true;
  510. }
  511. public function execute($block = null) {
  512. if ($block && !is_callable($block)) {
  513. throw new \RuntimeException('Argument passed must be a callable object');
  514. }
  515. $blockException = null;
  516. $returnValues = array();
  517. try {
  518. if ($block !== null) {
  519. $block($this);
  520. }
  521. if ($this->_discarded === true) {
  522. return;
  523. }
  524. $execReply = (($reply = $this->_redisClient->exec()) instanceof \Iterator
  525. ? iterator_to_array($reply)
  526. : $reply
  527. );
  528. $commands = &$this->_commands;
  529. $sizeofReplies = count($execReply);
  530. if ($sizeofReplies !== count($commands)) {
  531. // TODO: think of a better exception message
  532. throw new ClientException("Out-of-sync");
  533. }
  534. for ($i = 0; $i < $sizeofReplies; $i++) {
  535. $returnValues[] = $commands[$i]->parseResponse($execReply[$i] instanceof \Iterator
  536. ? iterator_to_array($execReply[$i])
  537. : $execReply[$i]
  538. );
  539. unset($commands[$i]);
  540. }
  541. }
  542. catch (\Exception $exception) {
  543. $blockException = $exception;
  544. }
  545. if ($blockException !== null) {
  546. throw $blockException;
  547. }
  548. return $returnValues;
  549. }
  550. }
  551. /* ------------------------------------------------------------------------- */
  552. class ConnectionParameters {
  553. const DEFAULT_HOST = '127.0.0.1';
  554. const DEFAULT_PORT = 6379;
  555. const DEFAULT_TIMEOUT = 5;
  556. private $_parameters;
  557. public function __construct($parameters = null) {
  558. $parameters = $parameters ?: array();
  559. $this->_parameters = is_array($parameters)
  560. ? self::filterConnectionParams($parameters)
  561. : self::parseURI($parameters);
  562. }
  563. private static function parseURI($uri) {
  564. $parsed = @parse_url($uri);
  565. if ($parsed == false || $parsed['scheme'] != 'redis' || $parsed['host'] == null) {
  566. throw new ClientException("Invalid URI: $uri");
  567. }
  568. if (array_key_exists('query', $parsed)) {
  569. $details = array();
  570. foreach (explode('&', $parsed['query']) as $kv) {
  571. list($k, $v) = explode('=', $kv);
  572. switch ($k) {
  573. case 'database':
  574. $details['database'] = $v;
  575. break;
  576. case 'password':
  577. $details['password'] = $v;
  578. break;
  579. case 'connection_timeout':
  580. $details['connection_timeout'] = $v;
  581. break;
  582. case 'read_write_timeout':
  583. $details['read_write_timeout'] = $v;
  584. break;
  585. case 'alias':
  586. $details['alias'] = $v;
  587. break;
  588. }
  589. }
  590. $parsed = array_merge($parsed, $details);
  591. }
  592. return self::filterConnectionParams($parsed);
  593. }
  594. private static function getParamOrDefault(Array $parameters, $param, $default = null) {
  595. return array_key_exists($param, $parameters) ? $parameters[$param] : $default;
  596. }
  597. private static function filterConnectionParams($parameters) {
  598. return array(
  599. 'host' => self::getParamOrDefault($parameters, 'host', self::DEFAULT_HOST),
  600. 'port' => (int) self::getParamOrDefault($parameters, 'port', self::DEFAULT_PORT),
  601. 'database' => self::getParamOrDefault($parameters, 'database'),
  602. 'password' => self::getParamOrDefault($parameters, 'password'),
  603. 'connection_timeout' => self::getParamOrDefault($parameters, 'connection_timeout', self::DEFAULT_TIMEOUT),
  604. 'read_write_timeout' => self::getParamOrDefault($parameters, 'read_write_timeout'),
  605. 'alias' => self::getParamOrDefault($parameters, 'alias'),
  606. );
  607. }
  608. public function __get($parameter) {
  609. return $this->_parameters[$parameter];
  610. }
  611. public function __isset($parameter) {
  612. return isset($this->_parameters[$parameter]);
  613. }
  614. }
  615. interface IConnection {
  616. public function connect();
  617. public function disconnect();
  618. public function isConnected();
  619. public function writeCommand(Command $command);
  620. public function readResponse(Command $command);
  621. public function executeCommand(Command $command);
  622. }
  623. class Connection implements IConnection {
  624. private $_params, $_socket, $_initCmds, $_reader;
  625. public function __construct(ConnectionParameters $parameters, ResponseReader $reader = null) {
  626. $this->_params = $parameters;
  627. $this->_initCmds = array();
  628. $this->_reader = $reader ?: new ResponseReader();
  629. }
  630. public function __destruct() {
  631. $this->disconnect();
  632. }
  633. public function isConnected() {
  634. return is_resource($this->_socket);
  635. }
  636. public function connect() {
  637. if ($this->isConnected()) {
  638. throw new ClientException('Connection already estabilished');
  639. }
  640. $uri = sprintf('tcp://%s:%d/', $this->_params->host, $this->_params->port);
  641. $this->_socket = @stream_socket_client($uri, $errno, $errstr, $this->_params->connection_timeout);
  642. if (!$this->_socket) {
  643. throw new ClientException(trim($errstr), $errno);
  644. }
  645. if (isset($this->_params->read_write_timeout)) {
  646. stream_set_timeout($this->_socket, $this->_params->read_write_timeout);
  647. }
  648. if (count($this->_initCmds) > 0){
  649. $this->sendInitializationCommands();
  650. }
  651. }
  652. public function disconnect() {
  653. if ($this->isConnected()) {
  654. fclose($this->_socket);
  655. }
  656. }
  657. public function pushInitCommand(Command $command){
  658. $this->_initCmds[] = $command;
  659. }
  660. private function sendInitializationCommands() {
  661. foreach ($this->_initCmds as $command) {
  662. $this->writeCommand($command);
  663. }
  664. foreach ($this->_initCmds as $command) {
  665. $this->readResponse($command);
  666. }
  667. }
  668. public function writeCommand(Command $command) {
  669. $written = fwrite($this->getSocket(), $command());
  670. if ($written === false){
  671. throw new ClientException(sprintf(
  672. 'An error has occurred while writing command %s on the network stream',
  673. $command->getCommandId()
  674. ));
  675. }
  676. }
  677. public function readResponse(Command $command) {
  678. $response = $this->_reader->read($this);
  679. $skipparse = isset($response->queued) || isset($response->error);
  680. return $skipparse ? $response : $command->parseResponse($response);
  681. }
  682. public function executeCommand(Command $command) {
  683. $this->writeCommand($command);
  684. if ($command->closesConnection()) {
  685. return $this->disconnect();
  686. }
  687. return $this->readResponse($command);
  688. }
  689. public function rawCommand($rawCommandData, $closesConnection = false) {
  690. $socket = $this->getSocket();
  691. $written = fwrite($socket, $rawCommandData);
  692. if ($written === false){
  693. throw new ClientException('An error has occurred while writing a raw command on the network stream');
  694. }
  695. if ($closesConnection) {
  696. return;
  697. }
  698. return $this->_reader->read($this);
  699. }
  700. public function getSocket() {
  701. if (!$this->isConnected()) {
  702. $this->connect();
  703. }
  704. return $this->_socket;
  705. }
  706. public function getResponseReader() {
  707. return $this->_reader;
  708. }
  709. public function getAlias() {
  710. return $this->_params->alias;
  711. }
  712. public function __toString() {
  713. return sprintf('%s:%d', $this->_params->host, $this->_params->port);
  714. }
  715. }
  716. class ConnectionCluster implements IConnection, \IteratorAggregate {
  717. private $_pool, $_ring;
  718. public function __construct() {
  719. $this->_pool = array();
  720. $this->_ring = new Utilities\HashRing();
  721. }
  722. public function __destruct() {
  723. $this->disconnect();
  724. }
  725. public function isConnected() {
  726. foreach ($this->_pool as $connection) {
  727. if ($connection->isConnected()) {
  728. return true;
  729. }
  730. }
  731. return false;
  732. }
  733. public function connect() {
  734. foreach ($this->_pool as $connection) {
  735. $connection->connect();
  736. }
  737. }
  738. public function disconnect() {
  739. foreach ($this->_pool as $connection) {
  740. $connection->disconnect();
  741. }
  742. }
  743. public function add(Connection $connection) {
  744. $connectionAlias = $connection->getAlias();
  745. if (isset($connectionAlias)) {
  746. $this->_pool[$connectionAlias] = $connection;
  747. }
  748. else {
  749. $this->_pool[] = $connection;
  750. }
  751. $this->_ring->add($connection);
  752. }
  753. private function getConnection(Command $command) {
  754. if ($command->canBeHashed() === false) {
  755. throw new ClientException(
  756. sprintf("Cannot send '%s' commands to a cluster of connections.", $command->getCommandId())
  757. );
  758. }
  759. return $this->_ring->get($command->getHash());
  760. }
  761. public function getConnectionById($id = null) {
  762. return $this->_pool[$id ?: 0];
  763. }
  764. public function getIterator() {
  765. return new \ArrayIterator($this->_pool);
  766. }
  767. public function writeCommand(Command $command) {
  768. $this->getConnection($command)->writeCommand($command);
  769. }
  770. public function readResponse(Command $command) {
  771. return $this->getConnection($command)->readResponse($command);
  772. }
  773. public function executeCommand(Command $command) {
  774. $connection = $this->getConnection($command);
  775. $connection->writeCommand($command);
  776. return $connection->readResponse($command);
  777. }
  778. }
  779. /* ------------------------------------------------------------------------- */
  780. abstract class RedisServerProfile {
  781. private static $_serverProfiles;
  782. private $_registeredCommands;
  783. public function __construct() {
  784. $this->_registeredCommands = $this->getSupportedCommands();
  785. }
  786. public abstract function getVersion();
  787. protected abstract function getSupportedCommands();
  788. public static function getDefault() {
  789. return self::get('default');
  790. }
  791. private static function predisServerProfiles() {
  792. return array(
  793. '1.0' => '\Predis\RedisServer_v1_0',
  794. '1.2' => '\Predis\RedisServer_v1_2',
  795. 'default' => '\Predis\RedisServer_v1_2',
  796. 'dev' => '\Predis\RedisServer_vNext',
  797. );
  798. }
  799. public static function get($version) {
  800. if (!isset(self::$_serverProfiles)) {
  801. self::$_serverProfiles = self::predisServerProfiles();
  802. }
  803. if (!isset(self::$_serverProfiles[$version])) {
  804. throw new ClientException("Unknown server profile: $version");
  805. }
  806. $profile = self::$_serverProfiles[$version];
  807. return new $profile();
  808. }
  809. public function compareWith($version, $operator = null) {
  810. // one could expect that PHP's version_compare would behave
  811. // the same way if invoked with 2 arguments or 3 arguments
  812. // with the third being NULL, but it is not like that.
  813. // TODO: since version_compare considers 1 < 1.0 < 1.0.0,
  814. // we might need to revise the behavior of this method.
  815. return ($operator === null
  816. ? version_compare($this, $version)
  817. : version_compare($this, $version, $operator)
  818. );
  819. }
  820. public function supportsCommand($command) {
  821. return isset($this->_registeredCommands[$command]);
  822. }
  823. public function createCommand($method, $arguments = array()) {
  824. if (!isset($this->_registeredCommands[$method])) {
  825. throw new ClientException("'$method' is not a registered Redis command");
  826. }
  827. $commandClass = $this->_registeredCommands[$method];
  828. $command = new $commandClass();
  829. $command->setArgumentsArray($arguments);
  830. return $command;
  831. }
  832. public function registerCommands(Array $commands) {
  833. foreach ($commands as $command => $aliases) {
  834. $this->registerCommand($command, $aliases);
  835. }
  836. }
  837. public function registerCommand($command, $aliases) {
  838. $commandReflection = new \ReflectionClass($command);
  839. if (!$commandReflection->isSubclassOf('\Predis\Command')) {
  840. throw new ClientException("Cannot register '$command' as it is not a valid Redis command");
  841. }
  842. if (is_array($aliases)) {
  843. foreach ($aliases as $alias) {
  844. $this->_registeredCommands[$alias] = $command;
  845. }
  846. }
  847. else {
  848. $this->_registeredCommands[$aliases] = $command;
  849. }
  850. }
  851. public function __toString() {
  852. return $this->getVersion();
  853. }
  854. }
  855. class RedisServer_v1_0 extends RedisServerProfile {
  856. public function getVersion() { return '1.0'; }
  857. public function getSupportedCommands() {
  858. return array(
  859. /* miscellaneous commands */
  860. 'ping' => '\Predis\Commands\Ping',
  861. 'echo' => '\Predis\Commands\DoEcho',
  862. 'auth' => '\Predis\Commands\Auth',
  863. /* connection handling */
  864. 'quit' => '\Predis\Commands\Quit',
  865. /* commands operating on string values */
  866. 'set' => '\Predis\Commands\Set',
  867. 'setnx' => '\Predis\Commands\SetPreserve',
  868. 'setPreserve' => '\Predis\Commands\SetPreserve',
  869. 'get' => '\Predis\Commands\Get',
  870. 'mget' => '\Predis\Commands\GetMultiple',
  871. 'getMultiple' => '\Predis\Commands\GetMultiple',
  872. 'getset' => '\Predis\Commands\GetSet',
  873. 'getSet' => '\Predis\Commands\GetSet',
  874. 'incr' => '\Predis\Commands\Increment',
  875. 'increment' => '\Predis\Commands\Increment',
  876. 'incrby' => '\Predis\Commands\IncrementBy',
  877. 'incrementBy' => '\Predis\Commands\IncrementBy',
  878. 'decr' => '\Predis\Commands\Decrement',
  879. 'decrement' => '\Predis\Commands\Decrement',
  880. 'decrby' => '\Predis\Commands\DecrementBy',
  881. 'decrementBy' => '\Predis\Commands\DecrementBy',
  882. 'exists' => '\Predis\Commands\Exists',
  883. 'del' => '\Predis\Commands\Delete',
  884. 'delete' => '\Predis\Commands\Delete',
  885. 'type' => '\Predis\Commands\Type',
  886. /* commands operating on the key space */
  887. 'keys' => '\Predis\Commands\Keys',
  888. 'randomkey' => '\Predis\Commands\RandomKey',
  889. 'randomKey' => '\Predis\Commands\RandomKey',
  890. 'rename' => '\Predis\Commands\Rename',
  891. 'renamenx' => '\Predis\Commands\RenamePreserve',
  892. 'renamePreserve' => '\Predis\Commands\RenamePreserve',
  893. 'expire' => '\Predis\Commands\Expire',
  894. 'expireat' => '\Predis\Commands\ExpireAt',
  895. 'expireAt' => '\Predis\Commands\ExpireAt',
  896. 'dbsize' => '\Predis\Commands\DatabaseSize',
  897. 'databaseSize' => '\Predis\Commands\DatabaseSize',
  898. 'ttl' => '\Predis\Commands\TimeToLive',
  899. 'timeToLive' => '\Predis\Commands\TimeToLive',
  900. /* commands operating on lists */
  901. 'rpush' => '\Predis\Commands\ListPushTail',
  902. 'pushTail' => '\Predis\Commands\ListPushTail',
  903. 'lpush' => '\Predis\Commands\ListPushHead',
  904. 'pushHead' => '\Predis\Commands\ListPushHead',
  905. 'llen' => '\Predis\Commands\ListLength',
  906. 'listLength' => '\Predis\Commands\ListLength',
  907. 'lrange' => '\Predis\Commands\ListRange',
  908. 'listRange' => '\Predis\Commands\ListRange',
  909. 'ltrim' => '\Predis\Commands\ListTrim',
  910. 'listTrim' => '\Predis\Commands\ListTrim',
  911. 'lindex' => '\Predis\Commands\ListIndex',
  912. 'listIndex' => '\Predis\Commands\ListIndex',
  913. 'lset' => '\Predis\Commands\ListSet',
  914. 'listSet' => '\Predis\Commands\ListSet',
  915. 'lrem' => '\Predis\Commands\ListRemove',
  916. 'listRemove' => '\Predis\Commands\ListRemove',
  917. 'lpop' => '\Predis\Commands\ListPopFirst',
  918. 'popFirst' => '\Predis\Commands\ListPopFirst',
  919. 'rpop' => '\Predis\Commands\ListPopLast',
  920. 'popLast' => '\Predis\Commands\ListPopLast',
  921. /* commands operating on sets */
  922. 'sadd' => '\Predis\Commands\SetAdd',
  923. 'setAdd' => '\Predis\Commands\SetAdd',
  924. 'srem' => '\Predis\Commands\SetRemove',
  925. 'setRemove' => '\Predis\Commands\SetRemove',
  926. 'spop' => '\Predis\Commands\SetPop',
  927. 'setPop' => '\Predis\Commands\SetPop',
  928. 'smove' => '\Predis\Commands\SetMove',
  929. 'setMove' => '\Predis\Commands\SetMove',
  930. 'scard' => '\Predis\Commands\SetCardinality',
  931. 'setCardinality' => '\Predis\Commands\SetCardinality',
  932. 'sismember' => '\Predis\Commands\SetIsMember',
  933. 'setIsMember' => '\Predis\Commands\SetIsMember',
  934. 'sinter' => '\Predis\Commands\SetIntersection',
  935. 'setIntersection' => '\Predis\Commands\SetIntersection',
  936. 'sinterstore' => '\Predis\Commands\SetIntersectionStore',
  937. 'setIntersectionStore' => '\Predis\Commands\SetIntersectionStore',
  938. 'sunion' => '\Predis\Commands\SetUnion',
  939. 'setUnion' => '\Predis\Commands\SetUnion',
  940. 'sunionstore' => '\Predis\Commands\SetUnionStore',
  941. 'setUnionStore' => '\Predis\Commands\SetUnionStore',
  942. 'sdiff' => '\Predis\Commands\SetDifference',
  943. 'setDifference' => '\Predis\Commands\SetDifference',
  944. 'sdiffstore' => '\Predis\Commands\SetDifferenceStore',
  945. 'setDifferenceStore' => '\Predis\Commands\SetDifferenceStore',
  946. 'smembers' => '\Predis\Commands\SetMembers',
  947. 'setMembers' => '\Predis\Commands\SetMembers',
  948. 'srandmember' => '\Predis\Commands\SetRandomMember',
  949. 'setRandomMember' => '\Predis\Commands\SetRandomMember',
  950. /* multiple databases handling commands */
  951. 'select' => '\Predis\Commands\SelectDatabase',
  952. 'selectDatabase' => '\Predis\Commands\SelectDatabase',
  953. 'move' => '\Predis\Commands\MoveKey',
  954. 'moveKey' => '\Predis\Commands\MoveKey',
  955. 'flushdb' => '\Predis\Commands\FlushDatabase',
  956. 'flushDatabase' => '\Predis\Commands\FlushDatabase',
  957. 'flushall' => '\Predis\Commands\FlushAll',
  958. 'flushDatabases' => '\Predis\Commands\FlushAll',
  959. /* sorting */
  960. 'sort' => '\Predis\Commands\Sort',
  961. /* remote server control commands */
  962. 'info' => '\Predis\Commands\Info',
  963. 'slaveof' => '\Predis\Commands\SlaveOf',
  964. 'slaveOf' => '\Predis\Commands\SlaveOf',
  965. /* persistence control commands */
  966. 'save' => '\Predis\Commands\Save',
  967. 'bgsave' => '\Predis\Commands\BackgroundSave',
  968. 'backgroundSave' => '\Predis\Commands\BackgroundSave',
  969. 'lastsave' => '\Predis\Commands\LastSave',
  970. 'lastSave' => '\Predis\Commands\LastSave',
  971. 'shutdown' => '\Predis\Commands\Shutdown',
  972. );
  973. }
  974. }
  975. class RedisServer_v1_2 extends RedisServer_v1_0 {
  976. public function getVersion() { return '1.2'; }
  977. public function getSupportedCommands() {
  978. return array_merge(parent::getSupportedCommands(), array(
  979. /* commands operating on string values */
  980. 'mset' => '\Predis\Commands\SetMultiple',
  981. 'setMultiple' => '\Predis\Commands\SetMultiple',
  982. 'msetnx' => '\Predis\Commands\SetMultiplePreserve',
  983. 'setMultiplePreserve' => '\Predis\Commands\SetMultiplePreserve',
  984. /* commands operating on lists */
  985. 'rpoplpush' => '\Predis\Commands\ListPushTailPopFirst',
  986. 'listPopLastPushHead' => '\Predis\Commands\ListPopLastPushHead',
  987. /* commands operating on sorted sets */
  988. 'zadd' => '\Predis\Commands\ZSetAdd',
  989. 'zsetAdd' => '\Predis\Commands\ZSetAdd',
  990. 'zincrby' => '\Predis\Commands\ZSetIncrementBy',
  991. 'zsetIncrementBy' => '\Predis\Commands\ZSetIncrementBy',
  992. 'zrem' => '\Predis\Commands\ZSetRemove',
  993. 'zsetRemove' => '\Predis\Commands\ZSetRemove',
  994. 'zrange' => '\Predis\Commands\ZSetRange',
  995. 'zsetRange' => '\Predis\Commands\ZSetRange',
  996. 'zrevrange' => '\Predis\Commands\ZSetReverseRange',
  997. 'zsetReverseRange' => '\Predis\Commands\ZSetReverseRange',
  998. 'zrangebyscore' => '\Predis\Commands\ZSetRangeByScore',
  999. 'zsetRangeByScore' => '\Predis\Commands\ZSetRangeByScore',
  1000. 'zcard' => '\Predis\Commands\ZSetCardinality',
  1001. 'zsetCardinality' => '\Predis\Commands\ZSetCardinality',
  1002. 'zscore' => '\Predis\Commands\ZSetScore',
  1003. 'zsetScore' => '\Predis\Commands\ZSetScore',
  1004. 'zremrangebyscore' => '\Predis\Commands\ZSetRemoveRangeByScore',
  1005. 'zsetRemoveRangeByScore' => '\Predis\Commands\ZSetRemoveRangeByScore',
  1006. /* persistence control commands */
  1007. 'bgrewriteaof' => '\Predis\Commands\BackgroundRewriteAppendOnlyFile',
  1008. 'backgroundRewriteAppendOnlyFile' => '\Predis\Commands\BackgroundRewriteAppendOnlyFile',
  1009. ));
  1010. }
  1011. }
  1012. class RedisServer_vNext extends RedisServer_v1_2 {
  1013. public function getVersion() { return '1.3'; }
  1014. public function getSupportedCommands() {
  1015. return array_merge(parent::getSupportedCommands(), array(
  1016. /* miscellaneous commands */
  1017. 'multi' => '\Predis\Commands\Multi',
  1018. 'exec' => '\Predis\Commands\Exec',
  1019. 'discard' => '\Predis\Commands\Discard',
  1020. /* commands operating on string values */
  1021. 'append' => '\Predis\Commands\Append',
  1022. 'substr' => '\Predis\Commands\Substr',
  1023. /* commands operating on lists */
  1024. 'blpop' => '\Predis\Commands\ListPopFirstBlocking',
  1025. 'popFirstBlocking' => '\Predis\Commands\ListPopFirstBlocking',
  1026. 'brpop' => '\Predis\Commands\ListPopLastBlocking',
  1027. 'popLastBlocking' => '\Predis\Commands\ListPopLastBlocking',
  1028. /* commands operating on sorted sets */
  1029. 'zunion' => '\Predis\Commands\ZSetUnion',
  1030. 'zsetUnion' => '\Predis\Commands\ZSetUnion',
  1031. 'zinter' => '\Predis\Commands\ZSetIntersection',
  1032. 'zsetIntersection' => '\Predis\Commands\ZSetIntersection',
  1033. 'zcount' => '\Predis\Commands\ZSetCount',
  1034. 'zsetCount' => '\Predis\Commands\ZSetCount',
  1035. 'zrank' => '\Predis\Commands\ZSetRank',
  1036. 'zsetRank' => '\Predis\Commands\ZSetRank',
  1037. 'zrevrank' => '\Predis\Commands\ZSetReverseRank',
  1038. 'zsetReverseRank' => '\Predis\Commands\ZSetReverseRank',
  1039. 'zremrangebyrank' => '\Predis\Commands\ZSetRemoveRangeByRank',
  1040. 'zsetRemoveRangeByRank' => '\Predis\Commands\ZSetRemoveRangeByRank',
  1041. /* commands operating on hashes */
  1042. 'hset' => '\Predis\Commands\HashSet',
  1043. 'hashSet' => '\Predis\Commands\HashSet',
  1044. 'hincrby' => '\Predis\Commands\HashIncrementBy',
  1045. 'hashIncrementBy' => '\Predis\Commands\HashIncrementBy',
  1046. 'hget' => '\Predis\Commands\HashGet',
  1047. 'hashGet' => '\Predis\Commands\HashGet',
  1048. 'hdel' => '\Predis\Commands\HashDelete',
  1049. 'hashDelete' => '\Predis\Commands\HashDelete',
  1050. 'hexists' => '\Predis\Commands\HashExists',
  1051. 'hashExists' => '\Predis\Commands\HashExists',
  1052. 'hlen' => '\Predis\Commands\HashLength',
  1053. 'hashLength' => '\Predis\Commands\HashLength',
  1054. 'hkeys' => '\Predis\Commands\HashKeys',
  1055. 'hashKeys' => '\Predis\Commands\HashKeys',
  1056. 'hvals' => '\Predis\Commands\HashValues',
  1057. 'hashValues' => '\Predis\Commands\HashValues',
  1058. 'hgetall' => '\Predis\Commands\HashGetAll',
  1059. 'hashKeysValues' => '\Predis\Commands\HashGetAll',
  1060. /* remote server control commands */
  1061. 'config' => '\Predis\Commands\Config',
  1062. 'configuration' => '\Predis\Commands\Config',
  1063. ));
  1064. }
  1065. }
  1066. /* ------------------------------------------------------------------------- */
  1067. namespace Predis\Utilities;
  1068. class HashRing {
  1069. const DEFAULT_REPLICAS = 128;
  1070. private $_nodes, $_ring, $_ringKeys, $_replicas;
  1071. public function __construct($replicas = self::DEFAULT_REPLICAS) {
  1072. $this->_replicas = $replicas;
  1073. $this->_nodes = array();
  1074. $this->_ring = array();
  1075. $this->_ringKeys = array();
  1076. }
  1077. public function add($node) {
  1078. // NOTE: in case of collisions in the hashes of the nodes, the node added
  1079. // last wins, thus the order in which nodes are added is significant.
  1080. $this->_nodes[] = $node;
  1081. $nodeHash = (string) $node;
  1082. $replicas = $this->_replicas;
  1083. for ($i = 0; $i < $replicas; $i++) {
  1084. $key = crc32($nodeHash . ':' . $i);
  1085. $this->_ring[$key] = $node;
  1086. }
  1087. ksort($this->_ring, SORT_NUMERIC);
  1088. $this->_ringKeys = array_keys($this->_ring);
  1089. }
  1090. public function remove($node) {
  1091. // NOTE: a node is removed by recreating the whole ring from scratch, in
  1092. // order to reassign possible hashes with collisions to the right node
  1093. // according to the order in which they were added in the first place.
  1094. $oldNodes = $this->_nodes;
  1095. $this->_nodes = array();
  1096. $this->_ring = array();
  1097. $this->_ringKeys = array();
  1098. foreach ($oldNodes as $oldNode) {
  1099. if ($oldNode !== $node) {
  1100. $this->add($oldNode);
  1101. }
  1102. }
  1103. }
  1104. public function get($key) {
  1105. return $this->_ring[$this->getNodeKey($key)];
  1106. }
  1107. private function getNodeKey($key) {
  1108. // NOTE: binary search for the last item in _ringkeys with a value
  1109. // less or equal to the key. If no such item exists, return the
  1110. // last item.
  1111. $ringKeys = $this->_ringKeys;
  1112. $upper = count($ringKeys) - 1;
  1113. $lower = 0;
  1114. $index = 0;
  1115. while ($lower <= $upper) {
  1116. $index = ($lower + $upper) >> 1;
  1117. $item = $ringKeys[$index];
  1118. if ($item > $key) {
  1119. $upper = $index - 1;
  1120. }
  1121. else if ($item < $key) {
  1122. $lower = $index + 1;
  1123. }
  1124. else {
  1125. return $item;
  1126. }
  1127. }
  1128. return $ringKeys[$upper >= 0 ? $upper : count($ringKeys) - 1];
  1129. }
  1130. }
  1131. abstract class MultiBulkResponseIteratorBase implements \Iterator, \Countable {
  1132. protected $_position, $_current, $_replySize;
  1133. public function rewind() {
  1134. // NOOP
  1135. }
  1136. public function current() {
  1137. return $this->_current;
  1138. }
  1139. public function key() {
  1140. return $this->_position;
  1141. }
  1142. public function next() {
  1143. if (++$this->_position < $this->_replySize) {
  1144. $this->_current = $this->getValue();
  1145. }
  1146. return $this->_position;
  1147. }
  1148. public function valid() {
  1149. return $this->_position < $this->_replySize;
  1150. }
  1151. public function count() {
  1152. // NOTE: use count if you want to get the size of the current multi-bulk
  1153. // response without using iterator_count (which actually consumes
  1154. // our iterator to calculate the size, and we cannot perform a rewind)
  1155. return $this->_replySize;
  1156. }
  1157. protected abstract function getValue();
  1158. }
  1159. class MultiBulkResponseIterator extends MultiBulkResponseIteratorBase {
  1160. private $_connection;
  1161. public function __construct(\Predis\Connection $connection, $size) {
  1162. $this->_connection = $connection;
  1163. $this->_reader = $connection->getResponseReader();
  1164. $this->_position = 0;
  1165. $this->_current = $size > 0 ? $this->getValue() : null;
  1166. $this->_replySize = $size;
  1167. }
  1168. public function __destruct() {
  1169. // when the iterator is garbage-collected (e.g. it goes out of the
  1170. // scope of a foreach) but it has not reached its end, we must sync
  1171. // the client with the queued elements that have not been read from
  1172. // the connection with the server.
  1173. $this->sync();
  1174. }
  1175. public function sync() {
  1176. while ($this->valid()) {
  1177. $this->next();
  1178. }
  1179. }
  1180. protected function getValue() {
  1181. return $this->_reader->read($this->_connection);
  1182. }
  1183. }
  1184. class MultiBulkResponseKVIterator extends MultiBulkResponseIteratorBase {
  1185. private $_iterator;
  1186. public function __construct(MultiBulkResponseIterator $iterator) {
  1187. $virtualSize = count($iterator) / 2;
  1188. $this->_iterator = $iterator;
  1189. $this->_position = 0;
  1190. $this->_current = $virtualSize > 0 ? $this->getValue() : null;
  1191. $this->_replySize = $virtualSize;
  1192. }
  1193. public function __destruct() {
  1194. $this->_iterator->sync();
  1195. }
  1196. protected function getValue() {
  1197. $k = $this->_iterator->current();
  1198. $this->_iterator->next();
  1199. $v = $this->_iterator->current();
  1200. $this->_iterator->next();
  1201. return array($k, $v);
  1202. }
  1203. }
  1204. /* ------------------------------------------------------------------------- */
  1205. namespace Predis\Commands;
  1206. /* miscellaneous commands */
  1207. class Ping extends \Predis\InlineCommand {
  1208. public function canBeHashed() { return false; }
  1209. public function getCommandId() { return 'PING'; }
  1210. public function parseResponse($data) {
  1211. return $data === 'PONG' ? true : false;
  1212. }
  1213. }
  1214. class DoEcho extends \Predis\BulkCommand {
  1215. public function canBeHashed() { return false; }
  1216. public function getCommandId() { return 'ECHO'; }
  1217. }
  1218. class Auth extends \Predis\InlineCommand {
  1219. public function canBeHashed() { return false; }
  1220. public function getCommandId() { return 'AUTH'; }
  1221. }
  1222. /* connection handling */
  1223. class Quit extends \Predis\InlineCommand {
  1224. public function canBeHashed() { return false; }
  1225. public function getCommandId() { return 'QUIT'; }
  1226. public function closesConnection() { return true; }
  1227. }
  1228. /* commands operating on string values */
  1229. class Set extends \Predis\BulkCommand {
  1230. public function getCommandId() { return 'SET'; }
  1231. }
  1232. class SetPreserve extends \Predis\BulkCommand {
  1233. public function getCommandId() { return 'SETNX'; }
  1234. public function parseResponse($data) { return (bool) $data; }
  1235. }
  1236. class SetMultiple extends \Predis\MultiBulkCommand {
  1237. public function canBeHashed() { return false; }
  1238. public function getCommandId() { return 'MSET'; }
  1239. }
  1240. class SetMultiplePreserve extends \Predis\MultiBulkCommand {
  1241. public function canBeHashed() { return false; }
  1242. public function getCommandId() { return 'MSETNX'; }
  1243. public function parseResponse($data) { return (bool) $data; }
  1244. }
  1245. class Get extends \Predis\InlineCommand {
  1246. public function getCommandId() { return 'GET'; }
  1247. }
  1248. class GetMultiple extends \Predis\InlineCommand {
  1249. public function canBeHashed() { return false; }
  1250. public function getCommandId() { return 'MGET'; }
  1251. }
  1252. class GetSet extends \Predis\BulkCommand {
  1253. public function getCommandId() { return 'GETSET'; }
  1254. }
  1255. class Increment extends \Predis\InlineCommand {
  1256. public function getCommandId() { return 'INCR'; }
  1257. }
  1258. class IncrementBy extends \Predis\InlineCommand {
  1259. public function getCommandId() { return 'INCRBY'; }
  1260. }
  1261. class Decrement extends \Predis\InlineCommand {
  1262. public function getCommandId() { return 'DECR'; }
  1263. }
  1264. class DecrementBy extends \Predis\InlineCommand {
  1265. public function getCommandId() { return 'DECRBY'; }
  1266. }
  1267. class Exists extends \Predis\InlineCommand {
  1268. public function getCommandId() { return 'EXISTS'; }
  1269. public function parseResponse($data) { return (bool) $data; }
  1270. }
  1271. class Delete extends \Predis\InlineCommand {
  1272. public function getCommandId() { return 'DEL'; }
  1273. public function parseResponse($data) { return (bool) $data; }
  1274. }
  1275. class Type extends \Predis\InlineCommand {
  1276. public function getCommandId() { return 'TYPE'; }
  1277. }
  1278. class Append extends \Predis\BulkCommand {
  1279. public function getCommandId() { return 'APPEND'; }
  1280. }
  1281. class Substr extends \Predis\InlineCommand {
  1282. public function getCommandId() { return 'SUBSTR'; }
  1283. }
  1284. /* commands operating on the key space */
  1285. class Keys extends \Predis\InlineCommand {
  1286. public function canBeHashed() { return false; }
  1287. public function getCommandId() { return 'KEYS'; }
  1288. public function parseResponse($data) {
  1289. // TODO: is this behaviour correct?
  1290. if (is_array($data) || $data instanceof \Iterator) {
  1291. return $data;
  1292. }
  1293. return strlen($data) > 0 ? explode(' ', $data) : array();
  1294. }
  1295. }
  1296. class RandomKey extends \Predis\InlineCommand {
  1297. public function canBeHashed() { return false; }
  1298. public function getCommandId() { return 'RANDOMKEY'; }
  1299. public function parseResponse($data) { return $data !== '' ? $data : null; }
  1300. }
  1301. class Rename extends \Predis\InlineCommand {
  1302. public function canBeHashed() { return false; }
  1303. public function getCommandId() { return 'RENAME'; }
  1304. }
  1305. class RenamePreserve extends \Predis\InlineCommand {
  1306. public function canBeHashed() { return false; }
  1307. public function getCommandId() { return 'RENAMENX'; }
  1308. public function parseResponse($data) { return (bool) $data; }
  1309. }
  1310. class Expire extends \Predis\InlineCommand {
  1311. public function getCommandId() { return 'EXPIRE'; }
  1312. public function parseResponse($data) { return (bool) $data; }
  1313. }
  1314. class ExpireAt extends \Predis\InlineCommand {
  1315. public function getCommandId() { return 'EXPIREAT'; }
  1316. public function parseResponse($data) { return (bool) $data; }
  1317. }
  1318. class DatabaseSize extends \Predis\InlineCommand {
  1319. public function canBeHashed() { return false; }
  1320. public function getCommandId() { return 'DBSIZE'; }
  1321. }
  1322. class TimeToLive extends \Predis\InlineCommand {
  1323. public function getCommandId() { return 'TTL'; }
  1324. }
  1325. /* commands operating on lists */
  1326. class ListPushTail extends \Predis\BulkCommand {
  1327. public function getCommandId() { return 'RPUSH'; }
  1328. }
  1329. class ListPushHead extends \Predis\BulkCommand {
  1330. public function getCommandId() { return 'LPUSH'; }
  1331. }
  1332. class ListLength extends \Predis\InlineCommand {
  1333. public function getCommandId() { return 'LLEN'; }
  1334. }
  1335. class ListRange extends \Predis\InlineCommand {
  1336. public function getCommandId() { return 'LRANGE'; }
  1337. }
  1338. class ListTrim extends \Predis\InlineCommand {
  1339. public function getCommandId() { return 'LTRIM'; }
  1340. }
  1341. class ListIndex extends \Predis\InlineCommand {
  1342. public function getCommandId() { return 'LINDEX'; }
  1343. }
  1344. class ListSet extends \Predis\BulkCommand {
  1345. public function getCommandId() { return 'LSET'; }
  1346. }
  1347. class ListRemove extends \Predis\BulkCommand {
  1348. public function getCommandId() { return 'LREM'; }
  1349. }
  1350. class ListPopLastPushHead extends \Predis\InlineCommand {
  1351. public function getCommandId() { return 'RPOPLPUSH'; }
  1352. }
  1353. class ListPopLastPushHeadBulk extends \Predis\BulkCommand {
  1354. public function getCommandId() { return 'RPOPLPUSH'; }
  1355. }
  1356. class ListPopFirst extends \Predis\InlineCommand {
  1357. public function getCommandId() { return 'LPOP'; }
  1358. }
  1359. class ListPopLast extends \Predis\InlineCommand {
  1360. public function getCommandId() { return 'RPOP'; }
  1361. }
  1362. class ListPopFirstBlocking extends \Predis\InlineCommand {
  1363. public function getCommandId() { return 'BLPOP'; }
  1364. }
  1365. class ListPopLastBlocking extends \Predis\InlineCommand {
  1366. public function getCommandId() { return 'BRPOP'; }
  1367. }
  1368. /* commands operating on sets */
  1369. class SetAdd extends \Predis\BulkCommand {
  1370. public function getCommandId() { return 'SADD'; }
  1371. public function parseResponse($data) { return (bool) $data; }
  1372. }
  1373. class SetRemove extends \Predis\BulkCommand {
  1374. public function getCommandId() { return 'SREM'; }
  1375. public function parseResponse($data) { return (bool) $data; }
  1376. }
  1377. class SetPop extends \Predis\InlineCommand {
  1378. public function getCommandId() { return 'SPOP'; }
  1379. }
  1380. class SetMove extends \Predis\BulkCommand {
  1381. public function canBeHashed() { return false; }
  1382. public function getCommandId() { return 'SMOVE'; }
  1383. public function parseResponse($data) { return (bool) $data; }
  1384. }
  1385. class SetCardinality extends \Predis\InlineCommand {
  1386. public function getCommandId() { return 'SCARD'; }
  1387. }
  1388. class SetIsMember extends \Predis\BulkCommand {
  1389. public function getCommandId() { return 'SISMEMBER'; }
  1390. public function parseResponse($data) { return (bool) $data; }
  1391. }
  1392. class SetIntersection extends \Predis\InlineCommand {
  1393. public function getCommandId() { return 'SINTER'; }
  1394. }
  1395. class SetIntersectionStore extends \Predis\InlineCommand {
  1396. public function getCommandId() { return 'SINTERSTORE'; }
  1397. }
  1398. class SetUnion extends \Predis\InlineCommand {
  1399. public function getCommandId() { return 'SUNION'; }
  1400. }
  1401. class SetUnionStore extends \Predis\InlineCommand {
  1402. public function getCommandId() { return 'SUNIONSTORE'; }
  1403. }
  1404. class SetDifference extends \Predis\InlineCommand {
  1405. public function getCommandId() { return 'SDIFF'; }
  1406. }
  1407. class SetDifferenceStore extends \Predis\InlineCommand {
  1408. public function getCommandId() { return 'SDIFFSTORE'; }
  1409. }
  1410. class SetMembers extends \Predis\InlineCommand {
  1411. public function getCommandId() { return 'SMEMBERS'; }
  1412. }
  1413. class SetRandomMember extends \Predis\InlineCommand {
  1414. public function getCommandId() { return 'SRANDMEMBER'; }
  1415. }
  1416. /* commands operating on sorted sets */
  1417. class ZSetAdd extends \Predis\BulkCommand {
  1418. public function getCommandId() { return 'ZADD'; }
  1419. public function parseResponse($data) { return (bool) $data; }
  1420. }
  1421. class ZSetIncrementBy extends \Predis\BulkCommand {
  1422. public function getCommandId() { return 'ZINCRBY'; }
  1423. }
  1424. class ZSetRemove extends \Predis\BulkCommand {
  1425. public function getCommandId() { return 'ZREM'; }
  1426. public function parseResponse($data) { return (bool) $data; }
  1427. }
  1428. class ZSetUnion extends \Predis\InlineCommand {
  1429. public function getCommandId() { return 'ZUNION'; }
  1430. }
  1431. class ZSetIntersection extends \Predis\InlineCommand {
  1432. public function getCommandId() { return 'ZINTER'; }
  1433. }
  1434. class ZSetRange extends \Predis\InlineCommand {
  1435. public function getCommandId() { return 'ZRANGE'; }
  1436. public function parseResponse($data) {
  1437. $arguments = $this->getArguments();
  1438. if (count($arguments) === 4) {
  1439. if (strtolower($arguments[3]) === 'withscores') {
  1440. if ($data instanceof \Iterator) {
  1441. return new \Predis\Utilities\MultiBulkResponseKVIterator($data);
  1442. }
  1443. $result = array();
  1444. for ($i = 0; $i < count($data); $i++) {
  1445. $result[] = array($data[$i], $data[++$i]);
  1446. }
  1447. return $result;
  1448. }
  1449. }
  1450. return $data;
  1451. }
  1452. }
  1453. class ZSetReverseRange extends \Predis\Commands\ZSetRange {
  1454. public function getCommandId() { return 'ZREVRANGE'; }
  1455. }
  1456. class ZSetRangeByScore extends \Predis\Commands\ZSetRange {
  1457. public function getCommandId() { return 'ZRANGEBYSCORE'; }
  1458. }
  1459. class ZSetCount extends \Predis\InlineCommand {
  1460. public function getCommandId() { return 'ZCOUNT'; }
  1461. }
  1462. class ZSetCardinality extends \Predis\InlineCommand {
  1463. public function getCommandId() { return 'ZCARD'; }
  1464. }
  1465. class ZSetScore extends \Predis\BulkCommand {
  1466. public function getCommandId() { return 'ZSCORE'; }
  1467. }
  1468. class ZSetRemoveRangeByScore extends \Predis\InlineCommand {
  1469. public function getCommandId() { return 'ZREMRANGEBYSCORE'; }
  1470. }
  1471. class ZSetRank extends \Predis\BulkCommand {
  1472. public function getCommandId() { return 'ZRANK'; }
  1473. }
  1474. class ZSetReverseRank extends \Predis\BulkCommand {
  1475. public function getCommandId() { return 'ZREVRANK'; }
  1476. }
  1477. class ZSetRemoveRangeByRank extends \Predis\InlineCommand {
  1478. public function getCommandId() { return 'ZREMRANGEBYRANK'; }
  1479. }
  1480. /* commands operating on hashes */
  1481. class HashSet extends \Predis\MultiBulkCommand {
  1482. public function getCommandId() { return 'HSET'; }
  1483. }
  1484. class HashIncrementBy extends \Predis\InlineCommand {
  1485. public function getCommandId() { return 'HINCRBY'; }
  1486. }
  1487. class HashGet extends \Predis\BulkCommand {
  1488. public function getCommandId() { return 'HGET'; }
  1489. }
  1490. class HashDelete extends \Predis\BulkCommand {
  1491. public function getCommandId() { return 'HDEL'; }
  1492. }
  1493. class HashExists extends \Predis\BulkCommand {
  1494. public function getCommandId() { return 'HEXISTS'; }
  1495. }
  1496. class HashLength extends \Predis\InlineCommand {
  1497. public function getCommandId() { return 'HLEN'; }
  1498. }
  1499. class HashKeys extends \Predis\InlineCommand {
  1500. public function getCommandId() { return 'HKEYS'; }
  1501. }
  1502. class HashValues extends \Predis\InlineCommand {
  1503. public function getCommandId() { return 'HVALS'; }
  1504. }
  1505. class HashGetAll extends \Predis\InlineCommand {
  1506. public function getCommandId() { return 'HGETALL'; }
  1507. }
  1508. /* multiple databases handling commands */
  1509. class SelectDatabase extends \Predis\InlineCommand {
  1510. public function canBeHashed() { return false; }
  1511. public function getCommandId() { return 'SELECT'; }
  1512. }
  1513. class MoveKey extends \Predis\InlineCommand {
  1514. public function canBeHashed() { return false; }
  1515. public function getCommandId() { return 'MOVE'; }
  1516. public function parseResponse($data) { return (bool) $data; }
  1517. }
  1518. class FlushDatabase extends \Predis\InlineCommand {
  1519. public function canBeHashed() { return false; }
  1520. public function getCommandId() { return 'FLUSHDB'; }
  1521. }
  1522. class FlushAll extends \Predis\InlineCommand {
  1523. public function canBeHashed() { return false; }
  1524. public function getCommandId() { return 'FLUSHALL'; }
  1525. }
  1526. /* sorting */
  1527. class Sort extends \Predis\InlineCommand {
  1528. public function getCommandId() { return 'SORT'; }
  1529. public function filterArguments(Array $arguments) {
  1530. if (count($arguments) === 1) {
  1531. return $arguments;
  1532. }
  1533. // TODO: add more parameters checks
  1534. $query = array($arguments[0]);
  1535. $sortParams = $arguments[1];
  1536. if (isset($sortParams['by'])) {
  1537. $query[] = 'BY ' . $sortParams['by'];
  1538. }
  1539. if (isset($sortParams['get'])) {
  1540. $getargs = $sortParams['get'];
  1541. if (is_array($getargs)) {
  1542. foreach ($getargs as $getarg) {
  1543. $query[] = 'GET ' . $getarg;
  1544. }
  1545. }
  1546. else {
  1547. $query[] = 'GET ' . $getargs;
  1548. }
  1549. }
  1550. if (isset($sortParams['limit']) && is_array($sortParams['limit'])) {
  1551. $query[] = 'LIMIT ' . $sortParams['limit'][0] . ' ' . $sortParams['limit'][1];
  1552. }
  1553. if (isset($sortParams['sort'])) {
  1554. $query[] = strtoupper($sortParams['sort']);
  1555. }
  1556. if (isset($sortParams['alpha']) && $sortParams['alpha'] == true) {
  1557. $query[] = 'ALPHA';
  1558. }
  1559. if (isset($sortParams['store']) && $sortParams['store'] == true) {
  1560. $query[] = 'STORE ' . $sortParams['store'];
  1561. }
  1562. return $query;
  1563. }
  1564. }
  1565. /* persistence control commands */
  1566. class Save extends \Predis\InlineCommand {
  1567. public function canBeHashed() { return false; }
  1568. public function getCommandId() { return 'SAVE'; }
  1569. }
  1570. class BackgroundSave extends \Predis\InlineCommand {
  1571. public function canBeHashed() { return false; }
  1572. public function getCommandId() { return 'BGSAVE'; }
  1573. public function parseResponse($data) {
  1574. if ($data == 'Background saving started') {
  1575. return true;
  1576. }
  1577. return $data;
  1578. }
  1579. }
  1580. class BackgroundRewriteAppendOnlyFile extends \Predis\InlineCommand {
  1581. public function canBeHashed() { return false; }
  1582. public function getCommandId() { return 'BGREWRITEAOF'; }
  1583. public function parseResponse($data) {
  1584. return $data == 'Background append only file rewriting started';
  1585. }
  1586. }
  1587. class LastSave extends \Predis\InlineCommand {
  1588. public function canBeHashed() { return false; }
  1589. public function getCommandId() { return 'LASTSAVE'; }
  1590. }
  1591. class Shutdown extends \Predis\InlineCommand {
  1592. public function canBeHashed() { return false; }
  1593. public function getCommandId() { return 'SHUTDOWN'; }
  1594. public function closesConnection() { return true; }
  1595. }
  1596. /* remote server control commands */
  1597. class Info extends \Predis\InlineCommand {
  1598. public function canBeHashed() { return false; }
  1599. public function getCommandId() { return 'INFO'; }
  1600. public function parseResponse($data) {
  1601. $info = array();
  1602. $infoLines = explode("\r\n", $data, -1);
  1603. foreach ($infoLines as $row) {
  1604. list($k, $v) = explode(':', $row);
  1605. if (!preg_match('/^db\d+$/', $k)) {
  1606. $info[$k] = $v;
  1607. }
  1608. else {
  1609. $db = array();
  1610. foreach (explode(',', $v) as $dbvar) {
  1611. list($dbvk, $dbvv) = explode('=', $dbvar);
  1612. $db[trim($dbvk)] = $dbvv;
  1613. }
  1614. $info[$k] = $db;
  1615. }
  1616. }
  1617. return $info;
  1618. }
  1619. }
  1620. class SlaveOf extends \Predis\InlineCommand {
  1621. public function canBeHashed() { return false; }
  1622. public function getCommandId() { return 'SLAVEOF'; }
  1623. public function filterArguments(Array $arguments) {
  1624. return count($arguments) === 0 ? array('NO ONE') : $arguments;
  1625. }
  1626. }
  1627. class Config extends \Predis\BulkCommand {
  1628. public function canBeHashed() { return false; }
  1629. public function getCommandId() { return 'CONFIG'; }
  1630. }
  1631. class Multi extends \Predis\InlineCommand {
  1632. public function canBeHashed() { return false; }
  1633. public function getCommandId() { return 'MULTI'; }
  1634. }
  1635. class Exec extends \Predis\InlineCommand {
  1636. public function canBeHashed() { return false; }
  1637. public function getCommandId() { return 'EXEC'; }
  1638. }
  1639. class Discard extends \Predis\InlineCommand {
  1640. public function canBeHashed() { return false; }
  1641. public function getCommandId() { return 'DISCARD'; }
  1642. }
  1643. ?>