Predis.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  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. // TODO: command arguments should be sanitized or checked for bad arguments
  10. // (e.g. CRLF in keys for inline commands)
  11. private $_connection, $_serverProfile;
  12. public function __construct($parameters = null, RedisServerProfile $serverProfile = null) {
  13. $this->setServerProfile(
  14. $serverProfile === null
  15. ? RedisServerProfile::getDefault()
  16. : $serverProfile
  17. );
  18. $this->setupConnection($parameters);
  19. }
  20. public function __destruct() {
  21. $this->_connection->disconnect();
  22. }
  23. public static function create(/* arguments */) {
  24. $argv = func_get_args();
  25. $argc = func_num_args();
  26. $serverProfile = null;
  27. $lastArg = $argv[$argc-1];
  28. if ($argc > 0 && !is_string($lastArg) && is_subclass_of($lastArg, '\Predis\RedisServerProfile')) {
  29. $serverProfile = array_pop($argv);
  30. $argc--;
  31. }
  32. if ($argc === 0) {
  33. throw new ClientException('Missing connection parameters');
  34. }
  35. return new Client($argc === 1 ? $argv[0] : $argv, $serverProfile);
  36. }
  37. private function setupConnection($parameters) {
  38. if ($parameters !== null && !(is_array($parameters) || is_string($parameters))) {
  39. throw new ClientException('Invalid parameters type (array or string expected)');
  40. }
  41. if (is_array($parameters) && isset($parameters[0])) {
  42. $cluster = new ConnectionCluster();
  43. foreach ($parameters as $shardParams) {
  44. $cluster->add($this->createConnection($shardParams));
  45. }
  46. $this->setConnection($cluster);
  47. }
  48. else {
  49. $this->setConnection($this->createConnection($parameters));
  50. }
  51. }
  52. private function createConnection($parameters) {
  53. $params = new ConnectionParameters($parameters);
  54. $connection = new Connection($params);
  55. if ($params->password !== null) {
  56. $connection->pushInitCommand($this->createCommand(
  57. 'auth', array($params->password)
  58. ));
  59. }
  60. if ($params->database !== null) {
  61. $connection->pushInitCommand($this->createCommand(
  62. 'select', array($params->database)
  63. ));
  64. }
  65. return $connection;
  66. }
  67. private function setConnection(IConnection $connection) {
  68. $this->_connection = $connection;
  69. }
  70. public function setServerProfile(RedisServerProfile $serverProfile) {
  71. $this->_serverProfile = $serverProfile;
  72. }
  73. public function connect() {
  74. $this->_connection->connect();
  75. }
  76. public function disconnect() {
  77. $this->_connection->disconnect();
  78. }
  79. public function isConnected() {
  80. return $this->_connection->isConnected();
  81. }
  82. public function getConnection() {
  83. return $this->_connection;
  84. }
  85. public function __call($method, $arguments) {
  86. $command = $this->_serverProfile->createCommand($method, $arguments);
  87. return $this->executeCommand($command);
  88. }
  89. public function createCommand($method, $arguments = array()) {
  90. return $this->_serverProfile->createCommand($method, $arguments);
  91. }
  92. private function executeCommandInternal(IConnection $connection, Command $command) {
  93. $connection->writeCommand($command);
  94. if ($command->closesConnection()) {
  95. return $connection->disconnect();
  96. }
  97. return $connection->readResponse($command);
  98. }
  99. public function executeCommand(Command $command) {
  100. return self::executeCommandInternal($this->_connection, $command);
  101. }
  102. public function executeCommandOnShards(Command $command) {
  103. $replies = array();
  104. if (is_a($this->_connection, '\Predis\ConnectionCluster')) {
  105. foreach($this->_connection as $connection) {
  106. $replies[] = self::executeCommandInternal($connection, $command);
  107. }
  108. }
  109. else {
  110. $replies[] = self::executeCommandInternal($this->_connection, $command);
  111. }
  112. return $replies;
  113. }
  114. public function rawCommand($rawCommandData, $closesConnection = false) {
  115. // TODO: rather than check the type of a connection instance, we should
  116. // check if it does respond to the rawCommand method.
  117. if (is_a($this->_connection, '\Predis\ConnectionCluster')) {
  118. throw new ClientException('Cannot send raw commands when connected to a cluster of Redis servers');
  119. }
  120. return $this->_connection->rawCommand($rawCommandData, $closesConnection);
  121. }
  122. public function pipeline(\Closure $pipelineBlock = null) {
  123. $pipeline = new CommandPipeline($this);
  124. return $pipelineBlock !== null ? $pipeline->execute($pipelineBlock) : $pipeline;
  125. }
  126. public function registerCommands(Array $commands) {
  127. $this->_serverProfile->registerCommands($commands);
  128. }
  129. public function registerCommand($command, $aliases) {
  130. $this->_serverProfile->registerCommand($command, $aliases);
  131. }
  132. }
  133. /* ------------------------------------------------------------------------- */
  134. abstract class Command {
  135. private $_arguments, $_hash;
  136. public abstract function getCommandId();
  137. public abstract function serializeRequest($command, $arguments);
  138. public function canBeHashed() {
  139. return true;
  140. }
  141. public function getHash() {
  142. if (isset($this->_hash)) {
  143. return $this->_hash;
  144. }
  145. else {
  146. if (isset($this->_arguments[0])) {
  147. $key = $this->_arguments[0];
  148. $start = strpos($key, '{');
  149. $end = strpos($key, '}');
  150. if ($start !== false && $end !== false) {
  151. $key = substr($key, ++$start, $end - $start);
  152. }
  153. $this->_hash = crc32($key);
  154. return $this->_hash;
  155. }
  156. }
  157. return null;
  158. }
  159. public function closesConnection() {
  160. return false;
  161. }
  162. protected function filterArguments(Array $arguments) {
  163. return $arguments;
  164. }
  165. public function setArguments(/* arguments */) {
  166. $this->_arguments = $this->filterArguments(func_get_args());
  167. }
  168. public function setArgumentsArray(Array $arguments) {
  169. $this->_arguments = $this->filterArguments($arguments);
  170. }
  171. protected function getArguments() {
  172. return isset($this->_arguments) ? $this->_arguments : array();
  173. }
  174. public function getArgument($index = 0) {
  175. return isset($this->_arguments[$index]) ? $this->_arguments[$index] : null;
  176. }
  177. public function parseResponse($data) {
  178. return $data;
  179. }
  180. public final function __invoke() {
  181. return $this->serializeRequest($this->getCommandId(), $this->getArguments());
  182. }
  183. }
  184. abstract class InlineCommand extends Command {
  185. public function serializeRequest($command, $arguments) {
  186. if (isset($arguments[0]) && is_array($arguments[0])) {
  187. $arguments[0] = implode($arguments[0], ' ');
  188. }
  189. return $command . ' ' . implode($arguments, ' ') . Response::NEWLINE;
  190. }
  191. }
  192. abstract class BulkCommand extends Command {
  193. public function serializeRequest($command, $arguments) {
  194. $data = array_pop($arguments);
  195. if (is_array($data)) {
  196. $data = implode($data, ' ');
  197. }
  198. return $command . ' ' . implode($arguments, ' ') . ' ' . strlen($data) .
  199. Response::NEWLINE . $data . Response::NEWLINE;
  200. }
  201. }
  202. abstract class MultiBulkCommand extends Command {
  203. public function serializeRequest($command, $arguments) {
  204. $buffer = array();
  205. $cmd_args = null;
  206. if (count($arguments) === 1 && is_array($arguments[0])) {
  207. $cmd_args = array();
  208. foreach ($arguments[0] as $k => $v) {
  209. $cmd_args[] = $k;
  210. $cmd_args[] = $v;
  211. }
  212. }
  213. else {
  214. $cmd_args = $arguments;
  215. }
  216. $buffer[] = '*' . ((string) count($cmd_args) + 1) . Response::NEWLINE;
  217. $buffer[] = '$' . strlen($command) . Response::NEWLINE . $command . Response::NEWLINE;
  218. foreach ($cmd_args as $argument) {
  219. $buffer[] = '$' . strlen($argument) . Response::NEWLINE . $argument . Response::NEWLINE;
  220. }
  221. return implode('', $buffer);
  222. }
  223. }
  224. /* ------------------------------------------------------------------------- */
  225. class Response {
  226. const NEWLINE = "\r\n";
  227. const OK = 'OK';
  228. const ERROR = 'ERR';
  229. const NULL = 'nil';
  230. private static $_prefixHandlers;
  231. private static function initializePrefixHandlers() {
  232. return array(
  233. // status
  234. '+' => function($socket) {
  235. $status = rtrim(fgets($socket), Response::NEWLINE);
  236. return $status === Response::OK ? true : $status;
  237. },
  238. // error
  239. '-' => function($socket) {
  240. $errorMessage = rtrim(fgets($socket), Response::NEWLINE);
  241. throw new ServerException(substr($errorMessage, 4));
  242. },
  243. // bulk
  244. '$' => function($socket) {
  245. $dataLength = rtrim(fgets($socket), Response::NEWLINE);
  246. if (!is_numeric($dataLength)) {
  247. throw new ClientException("Cannot parse '$dataLength' as data length");
  248. }
  249. if ($dataLength > 0) {
  250. $value = stream_get_contents($socket, $dataLength);
  251. fread($socket, 2);
  252. return $value;
  253. }
  254. else if ($dataLength == 0) {
  255. // TODO: I just have a doubt here...
  256. fread($socket, 2);
  257. }
  258. return null;
  259. },
  260. // multibulk
  261. '*' => function($socket) {
  262. $rawLength = rtrim(fgets($socket), Response::NEWLINE);
  263. if (!is_numeric($rawLength)) {
  264. throw new ClientException("Cannot parse '$rawLength' as data length");
  265. }
  266. $listLength = (int) $rawLength;
  267. if ($listLength === -1) {
  268. return null;
  269. }
  270. $list = array();
  271. if ($listLength > 0) {
  272. for ($i = 0; $i < $listLength; $i++) {
  273. $handler = Response::getPrefixHandler(fgetc($socket));
  274. $list[] = $handler($socket);
  275. }
  276. }
  277. return $list;
  278. },
  279. // integer
  280. ':' => function($socket) {
  281. $number = rtrim(fgets($socket), Response::NEWLINE);
  282. if (is_numeric($number)) {
  283. return (int) $number;
  284. }
  285. else {
  286. if ($number !== Response::NULL) {
  287. throw new ClientException("Cannot parse '$number' as numeric response");
  288. }
  289. return null;
  290. }
  291. }
  292. );
  293. }
  294. public static function getPrefixHandler($prefix) {
  295. if (self::$_prefixHandlers === null) {
  296. self::$_prefixHandlers = self::initializePrefixHandlers();
  297. }
  298. $handler = self::$_prefixHandlers[$prefix];
  299. if ($handler === null) {
  300. throw new MalformedServerResponse("Unknown prefix '$prefix'");
  301. }
  302. return $handler;
  303. }
  304. }
  305. class CommandPipeline {
  306. private $_redisClient, $_pipelineBuffer, $_returnValues, $_running;
  307. public function __construct(Client $redisClient) {
  308. $this->_redisClient = $redisClient;
  309. $this->_pipelineBuffer = array();
  310. $this->_returnValues = array();
  311. }
  312. public function __call($method, $arguments) {
  313. $command = $this->_redisClient->createCommand($method, $arguments);
  314. $this->recordCommand($command);
  315. }
  316. private function recordCommand(Command $command) {
  317. $this->_pipelineBuffer[] = $command;
  318. }
  319. private function getRecordedCommands() {
  320. return $this->_pipelineBuffer;
  321. }
  322. public function flushPipeline() {
  323. if (count($this->_pipelineBuffer) === 0) {
  324. return;
  325. }
  326. $connection = $this->_redisClient->getConnection();
  327. $commands = $this->getRecordedCommands();
  328. foreach ($commands as $command) {
  329. $connection->writeCommand($command);
  330. }
  331. foreach ($commands as $command) {
  332. $this->_returnValues[] = $connection->readResponse($command);
  333. }
  334. $this->_pipelineBuffer = array();
  335. }
  336. private function setRunning($bool) {
  337. // TODO: I am honest when I say that I don't like this approach.
  338. if ($bool == true && $this->_running == true) {
  339. throw new ClientException("This pipeline is already opened");
  340. }
  341. $this->_running = $bool;
  342. }
  343. public function execute(\Closure $block = null) {
  344. $this->setRunning(true);
  345. $pipelineBlockException = null;
  346. try {
  347. if ($block !== null) {
  348. $block($this);
  349. }
  350. $this->flushPipeline();
  351. }
  352. catch (\Exception $exception) {
  353. $pipelineBlockException = $exception;
  354. }
  355. $this->setRunning(false);
  356. if ($pipelineBlockException !== null) {
  357. throw $pipelineBlockException;
  358. }
  359. return $this->_returnValues;
  360. }
  361. }
  362. /* ------------------------------------------------------------------------- */
  363. class ConnectionParameters {
  364. const DEFAULT_HOST = '127.0.0.1';
  365. const DEFAULT_PORT = 6379;
  366. private $_parameters;
  367. public function __construct($parameters) {
  368. $parameters = $parameters !== null ? $parameters : array();
  369. $this->_parameters = is_array($parameters)
  370. ? self::filterConnectionParams($parameters)
  371. : self::parseURI($parameters);
  372. }
  373. private static function parseURI($uri) {
  374. $parsed = @parse_url($uri);
  375. if ($parsed == false || $parsed['scheme'] != 'redis' || $parsed['host'] == null) {
  376. throw new ClientException("Invalid URI: $uri");
  377. }
  378. if (array_key_exists('query', $parsed)) {
  379. $details = array();
  380. foreach (explode('&', $parsed['query']) as $kv) {
  381. list($k, $v) = explode('=', $kv);
  382. switch ($k) {
  383. case 'database':
  384. $details['database'] = $v;
  385. break;
  386. case 'password':
  387. $details['password'] = $v;
  388. break;
  389. }
  390. }
  391. $parsed = array_merge($parsed, $details);
  392. }
  393. return self::filterConnectionParams($parsed);
  394. }
  395. private static function getParamOrDefault(Array $parameters, $param, $default = null) {
  396. return array_key_exists($param, $parameters) ? $parameters[$param] : $default;
  397. }
  398. private static function filterConnectionParams($parameters) {
  399. return array(
  400. 'host' => self::getParamOrDefault($parameters, 'host', self::DEFAULT_HOST),
  401. 'port' => (int) self::getParamOrDefault($parameters, 'port', self::DEFAULT_PORT),
  402. 'database' => self::getParamOrDefault($parameters, 'database'),
  403. 'password' => self::getParamOrDefault($parameters, 'password')
  404. );
  405. }
  406. public function __get($parameter) {
  407. return $this->_parameters[$parameter];
  408. }
  409. }
  410. interface IConnection {
  411. public function connect();
  412. public function disconnect();
  413. public function isConnected();
  414. public function writeCommand(Command $command);
  415. public function readResponse(Command $command);
  416. }
  417. class Connection implements IConnection {
  418. const CONNECTION_TIMEOUT = 2;
  419. const READ_WRITE_TIMEOUT = 5;
  420. private $_params, $_socket, $_initCmds;
  421. public function __construct(ConnectionParameters $parameters) {
  422. $this->_params = $parameters;
  423. $this->_initCmds = array();
  424. }
  425. public function __destruct() {
  426. $this->disconnect();
  427. }
  428. public function isConnected() {
  429. return is_resource($this->_socket);
  430. }
  431. public function connect() {
  432. if ($this->isConnected()) {
  433. throw new ClientException('Connection already estabilished');
  434. }
  435. $uri = sprintf('tcp://%s:%d/', $this->_params->host, $this->_params->port);
  436. $this->_socket = @stream_socket_client($uri, $errno, $errstr, self::CONNECTION_TIMEOUT);
  437. if (!$this->_socket) {
  438. throw new ClientException(trim($errstr), $errno);
  439. }
  440. stream_set_timeout($this->_socket, self::READ_WRITE_TIMEOUT);
  441. if (count($this->_initCmds) > 0){
  442. $this->sendInitializationCommands();
  443. }
  444. }
  445. public function disconnect() {
  446. if ($this->isConnected()) {
  447. fclose($this->_socket);
  448. }
  449. }
  450. public function pushInitCommand(Command $command){
  451. $this->_initCmds[] = $command;
  452. }
  453. private function sendInitializationCommands() {
  454. foreach ($this->_initCmds as $command) {
  455. $this->writeCommand($command);
  456. }
  457. foreach ($this->_initCmds as $command) {
  458. $this->readResponse($command);
  459. }
  460. }
  461. public function writeCommand(Command $command) {
  462. fwrite($this->getSocket(), $command());
  463. }
  464. public function readResponse(Command $command) {
  465. $socket = $this->getSocket();
  466. $handler = Response::getPrefixHandler(fgetc($socket));
  467. $response = $command->parseResponse($handler($socket));
  468. return $response;
  469. }
  470. public function rawCommand($rawCommandData, $closesConnection = false) {
  471. $socket = $this->getSocket();
  472. fwrite($socket, $rawCommandData);
  473. if ($closesConnection) {
  474. return;
  475. }
  476. $handler = Response::getPrefixHandler(fgetc($socket));
  477. return $handler($socket);
  478. }
  479. public function getSocket() {
  480. if (!$this->isConnected()) {
  481. $this->connect();
  482. }
  483. return $this->_socket;
  484. }
  485. public function __toString() {
  486. return sprintf('%s:%d', $this->_params->host, $this->_params->port);
  487. }
  488. }
  489. class ConnectionCluster implements IConnection, \IteratorAggregate {
  490. // TODO: find a clean way to handle connection failures of single nodes.
  491. private $_pool, $_ring;
  492. public function __construct() {
  493. $this->_pool = array();
  494. $this->_ring = new Utilities\HashRing();
  495. }
  496. public function __destruct() {
  497. $this->disconnect();
  498. }
  499. public function isConnected() {
  500. foreach ($this->_pool as $connection) {
  501. if ($connection->isConnected()) {
  502. return true;
  503. }
  504. }
  505. return false;
  506. }
  507. public function connect() {
  508. foreach ($this->_pool as $connection) {
  509. $connection->connect();
  510. }
  511. }
  512. public function disconnect() {
  513. foreach ($this->_pool as $connection) {
  514. $connection->disconnect();
  515. }
  516. }
  517. public function add(Connection $connection) {
  518. $this->_pool[] = $connection;
  519. $this->_ring->add($connection);
  520. }
  521. private function getConnection(Command $command) {
  522. if ($command->canBeHashed() === false) {
  523. throw new ClientException(
  524. sprintf("Cannot send '%s' commands to a cluster of connections.", $command->getCommandId())
  525. );
  526. }
  527. return $this->_ring->get($command->getHash());
  528. }
  529. public function getConnectionById($id = null) {
  530. return $this->_pool[$id === null ? 0 : $id];
  531. }
  532. public function getIterator() {
  533. return new \ArrayIterator($this->_pool);
  534. }
  535. public function writeCommand(Command $command) {
  536. $this->getConnection($command)->writeCommand($command);
  537. }
  538. public function readResponse(Command $command) {
  539. return $this->getConnection($command)->readResponse($command);
  540. }
  541. }
  542. /* ------------------------------------------------------------------------- */
  543. abstract class RedisServerProfile {
  544. const DEFAULT_SERVER_PROFILE = '\Predis\RedisServer__V1_2';
  545. private $_registeredCommands;
  546. public function __construct() {
  547. $this->_registeredCommands = $this->getSupportedCommands();
  548. }
  549. public abstract function getVersion();
  550. protected abstract function getSupportedCommands();
  551. public static function getDefault() {
  552. $defaultProfile = self::DEFAULT_SERVER_PROFILE;
  553. return new $defaultProfile();
  554. }
  555. public function createCommand($method, $arguments = array()) {
  556. $commandClass = $this->_registeredCommands[$method];
  557. if ($commandClass === null) {
  558. throw new ClientException("'$method' is not a registered Redis command");
  559. }
  560. $command = new $commandClass();
  561. $command->setArgumentsArray($arguments);
  562. return $command;
  563. }
  564. public function registerCommands(Array $commands) {
  565. foreach ($commands as $command => $aliases) {
  566. $this->registerCommand($command, $aliases);
  567. }
  568. }
  569. public function registerCommand($command, $aliases) {
  570. $commandReflection = new \ReflectionClass($command);
  571. if (!$commandReflection->isSubclassOf('\Predis\Command')) {
  572. throw new ClientException("Cannot register '$command' as it is not a valid Redis command");
  573. }
  574. if (is_array($aliases)) {
  575. foreach ($aliases as $alias) {
  576. $this->_registeredCommands[$alias] = $command;
  577. }
  578. }
  579. else {
  580. $this->_registeredCommands[$aliases] = $command;
  581. }
  582. }
  583. }
  584. class RedisServer__V1_0 extends RedisServerProfile {
  585. public function getVersion() { return 1.0; }
  586. public function getSupportedCommands() {
  587. return array(
  588. /* miscellaneous commands */
  589. 'ping' => '\Predis\Commands\Ping',
  590. 'echo' => '\Predis\Commands\DoEcho',
  591. 'auth' => '\Predis\Commands\Auth',
  592. /* connection handling */
  593. 'quit' => '\Predis\Commands\Quit',
  594. /* commands operating on string values */
  595. 'set' => '\Predis\Commands\Set',
  596. 'setnx' => '\Predis\Commands\SetPreserve',
  597. 'setPreserve' => '\Predis\Commands\SetPreserve',
  598. 'get' => '\Predis\Commands\Get',
  599. 'mget' => '\Predis\Commands\GetMultiple',
  600. 'getMultiple' => '\Predis\Commands\GetMultiple',
  601. 'getset' => '\Predis\Commands\GetSet',
  602. 'getSet' => '\Predis\Commands\GetSet',
  603. 'incr' => '\Predis\Commands\Increment',
  604. 'increment' => '\Predis\Commands\Increment',
  605. 'incrby' => '\Predis\Commands\IncrementBy',
  606. 'incrementBy' => '\Predis\Commands\IncrementBy',
  607. 'decr' => '\Predis\Commands\Decrement',
  608. 'decrement' => '\Predis\Commands\Decrement',
  609. 'decrby' => '\Predis\Commands\DecrementBy',
  610. 'decrementBy' => '\Predis\Commands\DecrementBy',
  611. 'exists' => '\Predis\Commands\Exists',
  612. 'del' => '\Predis\Commands\Delete',
  613. 'delete' => '\Predis\Commands\Delete',
  614. 'type' => '\Predis\Commands\Type',
  615. /* commands operating on the key space */
  616. 'keys' => '\Predis\Commands\Keys',
  617. 'randomkey' => '\Predis\Commands\RandomKey',
  618. 'randomKey' => '\Predis\Commands\RandomKey',
  619. 'rename' => '\Predis\Commands\Rename',
  620. 'renamenx' => '\Predis\Commands\RenamePreserve',
  621. 'renamePreserve' => '\Predis\Commands\RenamePreserve',
  622. 'expire' => '\Predis\Commands\Expire',
  623. 'expireat' => '\Predis\Commands\ExpireAt',
  624. 'expireAt' => '\Predis\Commands\ExpireAt',
  625. 'dbsize' => '\Predis\Commands\DatabaseSize',
  626. 'databaseSize' => '\Predis\Commands\DatabaseSize',
  627. 'ttl' => '\Predis\Commands\TimeToLive',
  628. 'timeToLive' => '\Predis\Commands\TimeToLive',
  629. /* commands operating on lists */
  630. 'rpush' => '\Predis\Commands\ListPushTail',
  631. 'pushTail' => '\Predis\Commands\ListPushTail',
  632. 'lpush' => '\Predis\Commands\ListPushHead',
  633. 'pushHead' => '\Predis\Commands\ListPushHead',
  634. 'llen' => '\Predis\Commands\ListLength',
  635. 'listLength' => '\Predis\Commands\ListLength',
  636. 'lrange' => '\Predis\Commands\ListRange',
  637. 'listRange' => '\Predis\Commands\ListRange',
  638. 'ltrim' => '\Predis\Commands\ListTrim',
  639. 'listTrim' => '\Predis\Commands\ListTrim',
  640. 'lindex' => '\Predis\Commands\ListIndex',
  641. 'listIndex' => '\Predis\Commands\ListIndex',
  642. 'lset' => '\Predis\Commands\ListSet',
  643. 'listSet' => '\Predis\Commands\ListSet',
  644. 'lrem' => '\Predis\Commands\ListRemove',
  645. 'listRemove' => '\Predis\Commands\ListRemove',
  646. 'lpop' => '\Predis\Commands\ListPopFirst',
  647. 'popFirst' => '\Predis\Commands\ListPopFirst',
  648. 'rpop' => '\Predis\Commands\ListPopLast',
  649. 'popLast' => '\Predis\Commands\ListPopLast',
  650. /* commands operating on sets */
  651. 'sadd' => '\Predis\Commands\SetAdd',
  652. 'setAdd' => '\Predis\Commands\SetAdd',
  653. 'srem' => '\Predis\Commands\SetRemove',
  654. 'setRemove' => '\Predis\Commands\SetRemove',
  655. 'spop' => '\Predis\Commands\SetPop',
  656. 'setPop' => '\Predis\Commands\SetPop',
  657. 'smove' => '\Predis\Commands\SetMove',
  658. 'setMove' => '\Predis\Commands\SetMove',
  659. 'scard' => '\Predis\Commands\SetCardinality',
  660. 'setCardinality' => '\Predis\Commands\SetCardinality',
  661. 'sismember' => '\Predis\Commands\SetIsMember',
  662. 'setIsMember' => '\Predis\Commands\SetIsMember',
  663. 'sinter' => '\Predis\Commands\SetIntersection',
  664. 'setIntersection' => '\Predis\Commands\SetIntersection',
  665. 'sinterstore' => '\Predis\Commands\SetIntersectionStore',
  666. 'setIntersectionStore' => '\Predis\Commands\SetIntersectionStore',
  667. 'sunion' => '\Predis\Commands\SetUnion',
  668. 'setUnion' => '\Predis\Commands\SetUnion',
  669. 'sunionstore' => '\Predis\Commands\SetUnionStore',
  670. 'setUnionStore' => '\Predis\Commands\SetUnionStore',
  671. 'sdiff' => '\Predis\Commands\SetDifference',
  672. 'setDifference' => '\Predis\Commands\SetDifference',
  673. 'sdiffstore' => '\Predis\Commands\SetDifferenceStore',
  674. 'setDifferenceStore' => '\Predis\Commands\SetDifferenceStore',
  675. 'smembers' => '\Predis\Commands\SetMembers',
  676. 'setMembers' => '\Predis\Commands\SetMembers',
  677. 'srandmember' => '\Predis\Commands\SetRandomMember',
  678. 'setRandomMember' => '\Predis\Commands\SetRandomMember',
  679. /* multiple databases handling commands */
  680. 'select' => '\Predis\Commands\SelectDatabase',
  681. 'selectDatabase' => '\Predis\Commands\SelectDatabase',
  682. 'move' => '\Predis\Commands\MoveKey',
  683. 'moveKey' => '\Predis\Commands\MoveKey',
  684. 'flushdb' => '\Predis\Commands\FlushDatabase',
  685. 'flushDatabase' => '\Predis\Commands\FlushDatabase',
  686. 'flushall' => '\Predis\Commands\FlushAll',
  687. 'flushDatabases' => '\Predis\Commands\FlushAll',
  688. /* sorting */
  689. 'sort' => '\Predis\Commands\Sort',
  690. /* remote server control commands */
  691. 'info' => '\Predis\Commands\Info',
  692. 'slaveof' => '\Predis\Commands\SlaveOf',
  693. 'slaveOf' => '\Predis\Commands\SlaveOf',
  694. /* persistence control commands */
  695. 'save' => '\Predis\Commands\Save',
  696. 'bgsave' => '\Predis\Commands\BackgroundSave',
  697. 'backgroundSave' => '\Predis\Commands\BackgroundSave',
  698. 'lastsave' => '\Predis\Commands\LastSave',
  699. 'lastSave' => '\Predis\Commands\LastSave',
  700. 'shutdown' => '\Predis\Commands\Shutdown'
  701. );
  702. }
  703. }
  704. class RedisServer__V1_2 extends RedisServer__V1_0 {
  705. public function getVersion() { return 1.2; }
  706. public function getSupportedCommands() {
  707. return array_merge(parent::getSupportedCommands(), array(
  708. /* commands operating on string values */
  709. 'mset' => '\Predis\Commands\SetMultiple',
  710. 'setMultiple' => '\Predis\Commands\SetMultiple',
  711. 'msetnx' => '\Predis\Commands\SetMultiplePreserve',
  712. 'setMultiplePreserve' => '\Predis\Commands\SetMultiplePreserve',
  713. /* commands operating on lists */
  714. 'rpoplpush' => '\Predis\Commands\ListPushTailPopFirst',
  715. 'listPopLastPushHead' => '\Predis\Commands\ListPopLastPushHead',
  716. /* commands operating on sorted sets */
  717. 'zadd' => '\Predis\Commands\ZSetAdd',
  718. 'zsetAdd' => '\Predis\Commands\ZSetAdd',
  719. 'zincrby' => '\Predis\Commands\ZSetIncrementBy',
  720. 'zsetIncrementBy' => '\Predis\Commands\ZSetIncrementBy',
  721. 'zrem' => '\Predis\Commands\ZSetRemove',
  722. 'zsetRemove' => '\Predis\Commands\ZSetRemove',
  723. 'zrange' => '\Predis\Commands\ZSetRange',
  724. 'zsetRange' => '\Predis\Commands\ZSetRange',
  725. 'zrevrange' => '\Predis\Commands\ZSetReverseRange',
  726. 'zsetReverseRange' => '\Predis\Commands\ZSetReverseRange',
  727. 'zrangebyscore' => '\Predis\Commands\ZSetRangeByScore',
  728. 'zsetRangeByScore' => '\Predis\Commands\ZSetRangeByScore',
  729. 'zcard' => '\Predis\Commands\ZSetCardinality',
  730. 'zsetCardinality' => '\Predis\Commands\ZSetCardinality',
  731. 'zscore' => '\Predis\Commands\ZSetScore',
  732. 'zsetScore' => '\Predis\Commands\ZSetScore',
  733. 'zremrangebyscore' => '\Predis\Commands\ZSetRemoveRangeByScore',
  734. 'zsetRemoveRangeByScore' => '\Predis\Commands\ZSetRemoveRangeByScore'
  735. ));
  736. }
  737. }
  738. class RedisServer__Futures extends RedisServer__V1_2 {
  739. public function getVersion() { return 0; }
  740. public function getSupportedCommands() {
  741. return array_merge(parent::getSupportedCommands(), array(
  742. 'multi' => '\Predis\Commands\Multi',
  743. 'exec' => '\Predis\Commands\Exec'
  744. ));
  745. }
  746. }
  747. /* ------------------------------------------------------------------------- */
  748. namespace Predis\Utilities;
  749. class HashRing {
  750. const DEFAULT_REPLICAS = 128;
  751. private $_ring, $_ringKeys, $_replicas;
  752. public function __construct($replicas = self::DEFAULT_REPLICAS) {
  753. $this->_replicas = $replicas;
  754. $this->_ring = array();
  755. $this->_ringKeys = array();
  756. }
  757. public function add($node) {
  758. $nodeHash = (string) $node;
  759. $replicas = $this->_replicas;
  760. for ($i = 0; $i < $replicas; $i++) {
  761. $key = crc32($nodeHash . ':' . $i);
  762. $this->_ring[$key] = $node;
  763. }
  764. ksort($this->_ring, SORT_NUMERIC);
  765. $this->_ringKeys = array_keys($this->_ring);
  766. }
  767. public function remove($node) {
  768. $nodeHash = (string) $node;
  769. $replicas = $this->_replicas;
  770. for ($i = 0; $i < $replicas; $i++) {
  771. $key = crc32($nodeHash . ':' . $i);
  772. unset($this->_ring[$key]);
  773. $this->_ringKeys = array_filter($this->_ringKeys, function($rk) use($key) {
  774. return $rk !== $key;
  775. });
  776. }
  777. }
  778. public function get($key) {
  779. return $this->_ring[$this->getNodeKey($key)];
  780. }
  781. private function getNodeKey($key) {
  782. $ringKeys = $this->_ringKeys;
  783. $upper = count($ringKeys) - 1;
  784. $lower = 0;
  785. $index = 0;
  786. while ($lower <= $upper) {
  787. $index = ($lower + $upper) / 2;
  788. $item = $ringKeys[$index];
  789. if ($item > $key) {
  790. $upper = $index - 1;
  791. }
  792. else if ($item < $key) {
  793. $lower = $index + 1;
  794. }
  795. else {
  796. return $index;
  797. }
  798. }
  799. return $ringKeys[$upper];
  800. }
  801. }
  802. /* ------------------------------------------------------------------------- */
  803. namespace Predis\Commands;
  804. /* miscellaneous commands */
  805. class Ping extends \Predis\InlineCommand {
  806. public function canBeHashed() { return false; }
  807. public function getCommandId() { return 'PING'; }
  808. public function parseResponse($data) {
  809. return $data === 'PONG' ? true : false;
  810. }
  811. }
  812. class DoEcho extends \Predis\BulkCommand {
  813. public function canBeHashed() { return false; }
  814. public function getCommandId() { return 'ECHO'; }
  815. }
  816. class Auth extends \Predis\InlineCommand {
  817. public function canBeHashed() { return false; }
  818. public function getCommandId() { return 'AUTH'; }
  819. }
  820. /* connection handling */
  821. class Quit extends \Predis\InlineCommand {
  822. public function canBeHashed() { return false; }
  823. public function getCommandId() { return 'QUIT'; }
  824. public function closesConnection() { return true; }
  825. }
  826. /* commands operating on string values */
  827. class Set extends \Predis\BulkCommand {
  828. public function getCommandId() { return 'SET'; }
  829. }
  830. class SetPreserve extends \Predis\BulkCommand {
  831. public function getCommandId() { return 'SETNX'; }
  832. public function parseResponse($data) { return (bool) $data; }
  833. }
  834. class SetMultiple extends \Predis\MultiBulkCommand {
  835. public function canBeHashed() { return false; }
  836. public function getCommandId() { return 'MSET'; }
  837. }
  838. class SetMultiplePreserve extends \Predis\MultiBulkCommand {
  839. public function canBeHashed() { return false; }
  840. public function getCommandId() { return 'MSETNX'; }
  841. public function parseResponse($data) { return (bool) $data; }
  842. }
  843. class Get extends \Predis\InlineCommand {
  844. public function getCommandId() { return 'GET'; }
  845. }
  846. class GetMultiple extends \Predis\InlineCommand {
  847. public function canBeHashed() { return false; }
  848. public function getCommandId() { return 'MGET'; }
  849. }
  850. class GetSet extends \Predis\BulkCommand {
  851. public function getCommandId() { return 'GETSET'; }
  852. }
  853. class Increment extends \Predis\InlineCommand {
  854. public function getCommandId() { return 'INCR'; }
  855. }
  856. class IncrementBy extends \Predis\InlineCommand {
  857. public function getCommandId() { return 'INCRBY'; }
  858. }
  859. class Decrement extends \Predis\InlineCommand {
  860. public function getCommandId() { return 'DECR'; }
  861. }
  862. class DecrementBy extends \Predis\InlineCommand {
  863. public function getCommandId() { return 'DECRBY'; }
  864. }
  865. class Exists extends \Predis\InlineCommand {
  866. public function getCommandId() { return 'EXISTS'; }
  867. public function parseResponse($data) { return (bool) $data; }
  868. }
  869. class Delete extends \Predis\InlineCommand {
  870. public function getCommandId() { return 'DEL'; }
  871. public function parseResponse($data) { return (bool) $data; }
  872. }
  873. class Type extends \Predis\InlineCommand {
  874. public function getCommandId() { return 'TYPE'; }
  875. }
  876. /* commands operating on the key space */
  877. class Keys extends \Predis\InlineCommand {
  878. public function canBeHashed() { return false; }
  879. public function getCommandId() { return 'KEYS'; }
  880. public function parseResponse($data) {
  881. // TODO: is this behaviour correct?
  882. return strlen($data) > 0 ? explode(' ', $data) : array();
  883. }
  884. }
  885. class RandomKey extends \Predis\InlineCommand {
  886. public function canBeHashed() { return false; }
  887. public function getCommandId() { return 'RANDOMKEY'; }
  888. public function parseResponse($data) { return $data !== '' ? $data : null; }
  889. }
  890. class Rename extends \Predis\InlineCommand {
  891. // TODO: doesn't RENAME break the hash-based client-side sharding?
  892. public function canBeHashed() { return false; }
  893. public function getCommandId() { return 'RENAME'; }
  894. }
  895. class RenamePreserve extends \Predis\InlineCommand {
  896. public function canBeHashed() { return false; }
  897. public function getCommandId() { return 'RENAMENX'; }
  898. public function parseResponse($data) { return (bool) $data; }
  899. }
  900. class Expire extends \Predis\InlineCommand {
  901. public function getCommandId() { return 'EXPIRE'; }
  902. public function parseResponse($data) { return (bool) $data; }
  903. }
  904. class ExpireAt extends \Predis\InlineCommand {
  905. public function getCommandId() { return 'EXPIREAT'; }
  906. public function parseResponse($data) { return (bool) $data; }
  907. }
  908. class DatabaseSize extends \Predis\InlineCommand {
  909. public function canBeHashed() { return false; }
  910. public function getCommandId() { return 'DBSIZE'; }
  911. }
  912. class TimeToLive extends \Predis\InlineCommand {
  913. public function getCommandId() { return 'TTL'; }
  914. }
  915. /* commands operating on lists */
  916. class ListPushTail extends \Predis\BulkCommand {
  917. public function getCommandId() { return 'RPUSH'; }
  918. }
  919. class ListPushHead extends \Predis\BulkCommand {
  920. public function getCommandId() { return 'LPUSH'; }
  921. }
  922. class ListLength extends \Predis\InlineCommand {
  923. public function getCommandId() { return 'LLEN'; }
  924. }
  925. class ListRange extends \Predis\InlineCommand {
  926. public function getCommandId() { return 'LRANGE'; }
  927. }
  928. class ListTrim extends \Predis\InlineCommand {
  929. public function getCommandId() { return 'LTRIM'; }
  930. }
  931. class ListIndex extends \Predis\InlineCommand {
  932. public function getCommandId() { return 'LINDEX'; }
  933. }
  934. class ListSet extends \Predis\BulkCommand {
  935. public function getCommandId() { return 'LSET'; }
  936. }
  937. class ListRemove extends \Predis\BulkCommand {
  938. public function getCommandId() { return 'LREM'; }
  939. }
  940. class ListPopLastPushHead extends \Predis\BulkCommand {
  941. public function getCommandId() { return 'RPOPLPUSH'; }
  942. }
  943. class ListPopFirst extends \Predis\InlineCommand {
  944. public function getCommandId() { return 'LPOP'; }
  945. }
  946. class ListPopLast extends \Predis\InlineCommand {
  947. public function getCommandId() { return 'RPOP'; }
  948. }
  949. /* commands operating on sets */
  950. class SetAdd extends \Predis\BulkCommand {
  951. public function getCommandId() { return 'SADD'; }
  952. public function parseResponse($data) { return (bool) $data; }
  953. }
  954. class SetRemove extends \Predis\BulkCommand {
  955. public function getCommandId() { return 'SREM'; }
  956. public function parseResponse($data) { return (bool) $data; }
  957. }
  958. class SetPop extends \Predis\InlineCommand {
  959. public function getCommandId() { return 'SPOP'; }
  960. }
  961. class SetMove extends \Predis\BulkCommand {
  962. public function canBeHashed() { return false; }
  963. public function getCommandId() { return 'SMOVE'; }
  964. public function parseResponse($data) { return (bool) $data; }
  965. }
  966. class SetCardinality extends \Predis\InlineCommand {
  967. public function getCommandId() { return 'SCARD'; }
  968. }
  969. class SetIsMember extends \Predis\BulkCommand {
  970. public function getCommandId() { return 'SISMEMBER'; }
  971. public function parseResponse($data) { return (bool) $data; }
  972. }
  973. class SetIntersection extends \Predis\InlineCommand {
  974. public function getCommandId() { return 'SINTER'; }
  975. }
  976. class SetIntersectionStore extends \Predis\InlineCommand {
  977. public function getCommandId() { return 'SINTERSTORE'; }
  978. }
  979. class SetUnion extends \Predis\InlineCommand {
  980. public function getCommandId() { return 'SUNION'; }
  981. }
  982. class SetUnionStore extends \Predis\InlineCommand {
  983. public function getCommandId() { return 'SUNIONSTORE'; }
  984. }
  985. class SetDifference extends \Predis\InlineCommand {
  986. public function getCommandId() { return 'SDIFF'; }
  987. }
  988. class SetDifferenceStore extends \Predis\InlineCommand {
  989. public function getCommandId() { return 'SDIFFSTORE'; }
  990. }
  991. class SetMembers extends \Predis\InlineCommand {
  992. public function getCommandId() { return 'SMEMBERS'; }
  993. }
  994. class SetRandomMember extends \Predis\InlineCommand {
  995. public function getCommandId() { return 'SRANDMEMBER'; }
  996. }
  997. /* commands operating on sorted sets */
  998. class ZSetAdd extends \Predis\BulkCommand {
  999. public function getCommandId() { return 'ZADD'; }
  1000. public function parseResponse($data) { return (bool) $data; }
  1001. }
  1002. class ZSetIncrementBy extends \Predis\BulkCommand {
  1003. public function getCommandId() { return 'ZINCRBY'; }
  1004. }
  1005. class ZSetRemove extends \Predis\BulkCommand {
  1006. public function getCommandId() { return 'ZREM'; }
  1007. public function parseResponse($data) { return (bool) $data; }
  1008. }
  1009. class ZSetRange extends \Predis\InlineCommand {
  1010. public function getCommandId() { return 'ZRANGE'; }
  1011. public function parseResponse($data) {
  1012. $arguments = $this->getArguments();
  1013. if (count($arguments) === 4) {
  1014. if (strtolower($arguments[3]) === 'withscores') {
  1015. $result = array();
  1016. for ($i = 0; $i < count($data); $i++) {
  1017. $result[] = array($data[$i], $data[++$i]);
  1018. }
  1019. return $result;
  1020. }
  1021. }
  1022. return $data;
  1023. }
  1024. }
  1025. class ZSetReverseRange extends \Predis\Commands\ZSetRange {
  1026. public function getCommandId() { return 'ZREVRANGE'; }
  1027. }
  1028. class ZSetRangeByScore extends \Predis\InlineCommand {
  1029. public function getCommandId() { return 'ZRANGEBYSCORE'; }
  1030. }
  1031. class ZSetCardinality extends \Predis\InlineCommand {
  1032. public function getCommandId() { return 'ZCARD'; }
  1033. }
  1034. class ZSetScore extends \Predis\BulkCommand {
  1035. public function getCommandId() { return 'ZSCORE'; }
  1036. }
  1037. class ZSetRemoveRangeByScore extends \Predis\InlineCommand {
  1038. public function getCommandId() { return 'ZREMRANGEBYSCORE'; }
  1039. }
  1040. /* multiple databases handling commands */
  1041. class SelectDatabase extends \Predis\InlineCommand {
  1042. public function canBeHashed() { return false; }
  1043. public function getCommandId() { return 'SELECT'; }
  1044. }
  1045. class MoveKey extends \Predis\InlineCommand {
  1046. public function canBeHashed() { return false; }
  1047. public function getCommandId() { return 'MOVE'; }
  1048. public function parseResponse($data) { return (bool) $data; }
  1049. }
  1050. class FlushDatabase extends \Predis\InlineCommand {
  1051. public function canBeHashed() { return false; }
  1052. public function getCommandId() { return 'FLUSHDB'; }
  1053. }
  1054. class FlushAll extends \Predis\InlineCommand {
  1055. public function canBeHashed() { return false; }
  1056. public function getCommandId() { return 'FLUSHALL'; }
  1057. }
  1058. /* sorting */
  1059. class Sort extends \Predis\InlineCommand {
  1060. public function getCommandId() { return 'SORT'; }
  1061. public function filterArguments(Array $arguments) {
  1062. if (count($arguments) === 1) {
  1063. return $arguments;
  1064. }
  1065. // TODO: add more parameters checks
  1066. $query = array($arguments[0]);
  1067. $sortParams = $arguments[1];
  1068. if (isset($sortParams['by'])) {
  1069. $query[] = 'BY ' . $sortParams['by'];
  1070. }
  1071. if (isset($sortParams['get'])) {
  1072. $query[] = 'GET ' . $sortParams['get'];
  1073. }
  1074. if (isset($sortParams['limit']) && is_array($sortParams['limit'])) {
  1075. $query[] = 'LIMIT ' . $sortParams['limit'][0] . ' ' . $sortParams['limit'][1];
  1076. }
  1077. if (isset($sortParams['sort'])) {
  1078. $query[] = strtoupper($sortParams['sort']);
  1079. }
  1080. if (isset($sortParams['alpha']) && $sortParams['alpha'] == true) {
  1081. $query[] = 'ALPHA';
  1082. }
  1083. if (isset($sortParams['store']) && $sortParams['store'] == true) {
  1084. $query[] = 'STORE ' . $sortParams['store'];
  1085. }
  1086. return $query;
  1087. }
  1088. }
  1089. /* persistence control commands */
  1090. class Save extends \Predis\InlineCommand {
  1091. public function canBeHashed() { return false; }
  1092. public function getCommandId() { return 'SAVE'; }
  1093. }
  1094. class BackgroundSave extends \Predis\InlineCommand {
  1095. public function canBeHashed() { return false; }
  1096. public function getCommandId() { return 'BGSAVE'; }
  1097. }
  1098. class LastSave extends \Predis\InlineCommand {
  1099. public function canBeHashed() { return false; }
  1100. public function getCommandId() { return 'LASTSAVE'; }
  1101. }
  1102. class Shutdown extends \Predis\InlineCommand {
  1103. public function canBeHashed() { return false; }
  1104. public function getCommandId() { return 'SHUTDOWN'; }
  1105. public function closesConnection() { return true; }
  1106. }
  1107. /* remote server control commands */
  1108. class Info extends \Predis\InlineCommand {
  1109. public function canBeHashed() { return false; }
  1110. public function getCommandId() { return 'INFO'; }
  1111. public function parseResponse($data) {
  1112. $info = array();
  1113. $infoLines = explode("\r\n", $data, -1);
  1114. foreach ($infoLines as $row) {
  1115. list($k, $v) = explode(':', $row);
  1116. if (!preg_match('/^db\d+$/', $k)) {
  1117. $info[$k] = $v;
  1118. }
  1119. else {
  1120. $db = array();
  1121. foreach (explode(',', $v) as $dbvar) {
  1122. list($dbvk, $dbvv) = explode('=', $dbvar);
  1123. $db[trim($dbvk)] = $dbvv;
  1124. }
  1125. $info[$k] = $db;
  1126. }
  1127. }
  1128. return $info;
  1129. }
  1130. }
  1131. class SlaveOf extends \Predis\InlineCommand {
  1132. public function canBeHashed() { return false; }
  1133. public function getCommandId() { return 'SLAVEOF'; }
  1134. public function filterArguments(Array $arguments) {
  1135. return count($arguments) === 0 ? array('NO ONE') : $arguments;
  1136. }
  1137. }
  1138. class Multi extends \Predis\InlineCommand {
  1139. public function canBeHashed() { return false; }
  1140. public function getCommandId() { return 'MULTI'; }
  1141. }
  1142. class Exec extends \Predis\InlineCommand {
  1143. public function canBeHashed() { return false; }
  1144. public function getCommandId() { return 'EXEC'; }
  1145. }
  1146. ?>