RedisVersion1_0.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. namespace Predis;
  3. RedisServerProfile::registerProfile('\Predis\RedisServer_v1_0', '1.0');
  4. abstract class InlineCommand extends Command {
  5. public function serializeRequest($command, $arguments) {
  6. if (isset($arguments[0]) && is_array($arguments[0])) {
  7. $arguments[0] = implode($arguments[0], ' ');
  8. }
  9. return $command . (count($arguments) > 0
  10. ? ' ' . implode($arguments, ' ') . Protocol::NEWLINE
  11. : Protocol::NEWLINE
  12. );
  13. }
  14. }
  15. abstract class BulkCommand extends Command {
  16. public function serializeRequest($command, $arguments) {
  17. $data = array_pop($arguments);
  18. if (is_array($data)) {
  19. $data = implode($data, ' ');
  20. }
  21. return $command . ' ' . implode($arguments, ' ') . ' ' . strlen($data) .
  22. Protocol::NEWLINE . $data . Protocol::NEWLINE;
  23. }
  24. }
  25. class RedisServer_v1_0 extends \Predis\RedisServerProfile {
  26. public function getVersion() { return '1.0'; }
  27. public function getSupportedCommands() {
  28. return array(
  29. /* miscellaneous commands */
  30. 'ping' => '\Predis\Compatibility\v1_0\Commands\Ping',
  31. 'echo' => '\Predis\Compatibility\v1_0\Commands\DoEcho',
  32. 'auth' => '\Predis\Compatibility\v1_0\Commands\Auth',
  33. /* connection handling */
  34. 'quit' => '\Predis\Compatibility\v1_0\Commands\Quit',
  35. /* commands operating on string values */
  36. 'set' => '\Predis\Compatibility\v1_0\Commands\Set',
  37. 'setnx' => '\Predis\Compatibility\v1_0\Commands\SetPreserve',
  38. 'get' => '\Predis\Compatibility\v1_0\Commands\Get',
  39. 'mget' => '\Predis\Compatibility\v1_0\Commands\GetMultiple',
  40. 'getset' => '\Predis\Compatibility\v1_0\Commands\GetSet',
  41. 'incr' => '\Predis\Compatibility\v1_0\Commands\Increment',
  42. 'incrby' => '\Predis\Compatibility\v1_0\Commands\IncrementBy',
  43. 'decr' => '\Predis\Compatibility\v1_0\Commands\Decrement',
  44. 'decrby' => '\Predis\Compatibility\v1_0\Commands\DecrementBy',
  45. 'exists' => '\Predis\Compatibility\v1_0\Commands\Exists',
  46. 'del' => '\Predis\Compatibility\v1_0\Commands\Delete',
  47. 'type' => '\Predis\Compatibility\v1_0\Commands\Type',
  48. /* commands operating on the key space */
  49. 'keys' => '\Predis\Compatibility\v1_0\Commands\Keys',
  50. 'randomkey' => '\Predis\Compatibility\v1_0\Commands\RandomKey',
  51. 'rename' => '\Predis\Compatibility\v1_0\Commands\Rename',
  52. 'renamenx' => '\Predis\Compatibility\v1_0\Commands\RenamePreserve',
  53. 'expire' => '\Predis\Compatibility\v1_0\Commands\Expire',
  54. 'expireat' => '\Predis\Compatibility\v1_0\Commands\ExpireAt',
  55. 'dbsize' => '\Predis\Compatibility\v1_0\Commands\DatabaseSize',
  56. 'ttl' => '\Predis\Compatibility\v1_0\Commands\TimeToLive',
  57. /* commands operating on lists */
  58. 'rpush' => '\Predis\Compatibility\v1_0\Commands\ListPushTail',
  59. 'lpush' => '\Predis\Compatibility\v1_0\Commands\ListPushHead',
  60. 'llen' => '\Predis\Compatibility\v1_0\Commands\ListLength',
  61. 'lrange' => '\Predis\Compatibility\v1_0\Commands\ListRange',
  62. 'ltrim' => '\Predis\Compatibility\v1_0\Commands\ListTrim',
  63. 'lindex' => '\Predis\Compatibility\v1_0\Commands\ListIndex',
  64. 'lset' => '\Predis\Compatibility\v1_0\Commands\ListSet',
  65. 'lrem' => '\Predis\Compatibility\v1_0\Commands\ListRemove',
  66. 'lpop' => '\Predis\Compatibility\v1_0\Commands\ListPopFirst',
  67. 'rpop' => '\Predis\Compatibility\v1_0\Commands\ListPopLast',
  68. /* commands operating on sets */
  69. 'sadd' => '\Predis\Compatibility\v1_0\Commands\SetAdd',
  70. 'srem' => '\Predis\Compatibility\v1_0\Commands\SetRemove',
  71. 'spop' => '\Predis\Compatibility\v1_0\Commands\SetPop',
  72. 'smove' => '\Predis\Compatibility\v1_0\Commands\SetMove',
  73. 'scard' => '\Predis\Compatibility\v1_0\Commands\SetCardinality',
  74. 'sismember' => '\Predis\Compatibility\v1_0\Commands\SetIsMember',
  75. 'sinter' => '\Predis\Compatibility\v1_0\Commands\SetIntersection',
  76. 'sinterstore' => '\Predis\Compatibility\v1_0\Commands\SetIntersectionStore',
  77. 'sunion' => '\Predis\Compatibility\v1_0\Commands\SetUnion',
  78. 'sunionstore' => '\Predis\Compatibility\v1_0\Commands\SetUnionStore',
  79. 'sdiff' => '\Predis\Compatibility\v1_0\Commands\SetDifference',
  80. 'sdiffstore' => '\Predis\Compatibility\v1_0\Commands\SetDifferenceStore',
  81. 'smembers' => '\Predis\Compatibility\v1_0\Commands\SetMembers',
  82. 'srandmember' => '\Predis\Compatibility\v1_0\Commands\SetRandomMember',
  83. /* multiple databases handling commands */
  84. 'select' => '\Predis\Compatibility\v1_0\Commands\SelectDatabase',
  85. 'move' => '\Predis\Compatibility\v1_0\Commands\MoveKey',
  86. 'flushdb' => '\Predis\Compatibility\v1_0\Commands\FlushDatabase',
  87. 'flushall' => '\Predis\Compatibility\v1_0\Commands\FlushAll',
  88. /* sorting */
  89. 'sort' => '\Predis\Compatibility\v1_0\Commands\Sort',
  90. /* remote server control commands */
  91. 'info' => '\Predis\Compatibility\v1_0\Commands\Info',
  92. 'slaveof' => '\Predis\Compatibility\v1_0\Commands\SlaveOf',
  93. /* persistence control commands */
  94. 'save' => '\Predis\Compatibility\v1_0\Commands\Save',
  95. 'bgsave' => '\Predis\Compatibility\v1_0\Commands\BackgroundSave',
  96. 'lastsave' => '\Predis\Compatibility\v1_0\Commands\LastSave',
  97. 'shutdown' => '\Predis\Compatibility\v1_0\Commands\Shutdown',
  98. );
  99. }
  100. }
  101. namespace Predis\Compatibility\v1_0\Commands;
  102. /* miscellaneous commands */
  103. class Ping extends \Predis\InlineCommand {
  104. public function canBeHashed() { return false; }
  105. public function getCommandId() { return 'PING'; }
  106. public function parseResponse($data) {
  107. return $data === 'PONG' ? true : false;
  108. }
  109. }
  110. class DoEcho extends \Predis\BulkCommand {
  111. public function canBeHashed() { return false; }
  112. public function getCommandId() { return 'ECHO'; }
  113. }
  114. class Auth extends \Predis\InlineCommand {
  115. public function canBeHashed() { return false; }
  116. public function getCommandId() { return 'AUTH'; }
  117. }
  118. /* connection handling */
  119. class Quit extends \Predis\InlineCommand {
  120. public function canBeHashed() { return false; }
  121. public function getCommandId() { return 'QUIT'; }
  122. public function closesConnection() { return true; }
  123. }
  124. /* commands operating on string values */
  125. class Set extends \Predis\BulkCommand {
  126. public function getCommandId() { return 'SET'; }
  127. }
  128. class SetPreserve extends \Predis\BulkCommand {
  129. public function getCommandId() { return 'SETNX'; }
  130. public function parseResponse($data) { return (bool) $data; }
  131. }
  132. class Get extends \Predis\InlineCommand {
  133. public function getCommandId() { return 'GET'; }
  134. }
  135. class GetMultiple extends \Predis\InlineCommand {
  136. public function canBeHashed() { return false; }
  137. public function getCommandId() { return 'MGET'; }
  138. }
  139. class GetSet extends \Predis\BulkCommand {
  140. public function getCommandId() { return 'GETSET'; }
  141. }
  142. class Increment extends \Predis\InlineCommand {
  143. public function getCommandId() { return 'INCR'; }
  144. }
  145. class IncrementBy extends \Predis\InlineCommand {
  146. public function getCommandId() { return 'INCRBY'; }
  147. }
  148. class Decrement extends \Predis\InlineCommand {
  149. public function getCommandId() { return 'DECR'; }
  150. }
  151. class DecrementBy extends \Predis\InlineCommand {
  152. public function getCommandId() { return 'DECRBY'; }
  153. }
  154. class Exists extends \Predis\InlineCommand {
  155. public function getCommandId() { return 'EXISTS'; }
  156. public function parseResponse($data) { return (bool) $data; }
  157. }
  158. class Delete extends \Predis\InlineCommand {
  159. public function getCommandId() { return 'DEL'; }
  160. }
  161. class Type extends \Predis\InlineCommand {
  162. public function getCommandId() { return 'TYPE'; }
  163. }
  164. /* commands operating on the key space */
  165. class Keys extends \Predis\InlineCommand {
  166. public function canBeHashed() { return false; }
  167. public function getCommandId() { return 'KEYS'; }
  168. public function parseResponse($data) {
  169. return strlen($data) > 0 ? explode(' ', $data) : array();
  170. }
  171. }
  172. class RandomKey extends \Predis\InlineCommand {
  173. public function canBeHashed() { return false; }
  174. public function getCommandId() { return 'RANDOMKEY'; }
  175. public function parseResponse($data) { return $data !== '' ? $data : null; }
  176. }
  177. class Rename extends \Predis\InlineCommand {
  178. public function canBeHashed() { return false; }
  179. public function getCommandId() { return 'RENAME'; }
  180. }
  181. class RenamePreserve extends \Predis\InlineCommand {
  182. public function canBeHashed() { return false; }
  183. public function getCommandId() { return 'RENAMENX'; }
  184. public function parseResponse($data) { return (bool) $data; }
  185. }
  186. class Expire extends \Predis\InlineCommand {
  187. public function getCommandId() { return 'EXPIRE'; }
  188. public function parseResponse($data) { return (bool) $data; }
  189. }
  190. class ExpireAt extends \Predis\InlineCommand {
  191. public function getCommandId() { return 'EXPIREAT'; }
  192. public function parseResponse($data) { return (bool) $data; }
  193. }
  194. class DatabaseSize extends \Predis\InlineCommand {
  195. public function canBeHashed() { return false; }
  196. public function getCommandId() { return 'DBSIZE'; }
  197. }
  198. class TimeToLive extends \Predis\InlineCommand {
  199. public function getCommandId() { return 'TTL'; }
  200. }
  201. /* commands operating on lists */
  202. class ListPushTail extends \Predis\BulkCommand {
  203. public function getCommandId() { return 'RPUSH'; }
  204. }
  205. class ListPushHead extends \Predis\BulkCommand {
  206. public function getCommandId() { return 'LPUSH'; }
  207. }
  208. class ListLength extends \Predis\InlineCommand {
  209. public function getCommandId() { return 'LLEN'; }
  210. }
  211. class ListRange extends \Predis\InlineCommand {
  212. public function getCommandId() { return 'LRANGE'; }
  213. }
  214. class ListTrim extends \Predis\InlineCommand {
  215. public function getCommandId() { return 'LTRIM'; }
  216. }
  217. class ListIndex extends \Predis\InlineCommand {
  218. public function getCommandId() { return 'LINDEX'; }
  219. }
  220. class ListSet extends \Predis\BulkCommand {
  221. public function getCommandId() { return 'LSET'; }
  222. }
  223. class ListRemove extends \Predis\BulkCommand {
  224. public function getCommandId() { return 'LREM'; }
  225. }
  226. class ListPopFirst extends \Predis\InlineCommand {
  227. public function getCommandId() { return 'LPOP'; }
  228. }
  229. class ListPopLast extends \Predis\InlineCommand {
  230. public function getCommandId() { return 'RPOP'; }
  231. }
  232. /* commands operating on sets */
  233. class SetAdd extends \Predis\BulkCommand {
  234. public function getCommandId() { return 'SADD'; }
  235. public function parseResponse($data) { return (bool) $data; }
  236. }
  237. class SetRemove extends \Predis\BulkCommand {
  238. public function getCommandId() { return 'SREM'; }
  239. public function parseResponse($data) { return (bool) $data; }
  240. }
  241. class SetPop extends \Predis\InlineCommand {
  242. public function getCommandId() { return 'SPOP'; }
  243. }
  244. class SetMove extends \Predis\BulkCommand {
  245. public function canBeHashed() { return false; }
  246. public function getCommandId() { return 'SMOVE'; }
  247. public function parseResponse($data) { return (bool) $data; }
  248. }
  249. class SetCardinality extends \Predis\InlineCommand {
  250. public function getCommandId() { return 'SCARD'; }
  251. }
  252. class SetIsMember extends \Predis\BulkCommand {
  253. public function getCommandId() { return 'SISMEMBER'; }
  254. public function parseResponse($data) { return (bool) $data; }
  255. }
  256. class SetIntersection extends \Predis\InlineCommand {
  257. public function getCommandId() { return 'SINTER'; }
  258. }
  259. class SetIntersectionStore extends \Predis\InlineCommand {
  260. public function getCommandId() { return 'SINTERSTORE'; }
  261. }
  262. class SetUnion extends \Predis\InlineCommand {
  263. public function getCommandId() { return 'SUNION'; }
  264. }
  265. class SetUnionStore extends \Predis\InlineCommand {
  266. public function getCommandId() { return 'SUNIONSTORE'; }
  267. }
  268. class SetDifference extends \Predis\InlineCommand {
  269. public function getCommandId() { return 'SDIFF'; }
  270. }
  271. class SetDifferenceStore extends \Predis\InlineCommand {
  272. public function getCommandId() { return 'SDIFFSTORE'; }
  273. }
  274. class SetMembers extends \Predis\InlineCommand {
  275. public function getCommandId() { return 'SMEMBERS'; }
  276. }
  277. class SetRandomMember extends \Predis\InlineCommand {
  278. public function getCommandId() { return 'SRANDMEMBER'; }
  279. }
  280. /* multiple databases handling commands */
  281. class SelectDatabase extends \Predis\InlineCommand {
  282. public function canBeHashed() { return false; }
  283. public function getCommandId() { return 'SELECT'; }
  284. }
  285. class MoveKey extends \Predis\InlineCommand {
  286. public function canBeHashed() { return false; }
  287. public function getCommandId() { return 'MOVE'; }
  288. public function parseResponse($data) { return (bool) $data; }
  289. }
  290. class FlushDatabase extends \Predis\InlineCommand {
  291. public function canBeHashed() { return false; }
  292. public function getCommandId() { return 'FLUSHDB'; }
  293. }
  294. class FlushAll extends \Predis\InlineCommand {
  295. public function canBeHashed() { return false; }
  296. public function getCommandId() { return 'FLUSHALL'; }
  297. }
  298. /* sorting */
  299. class Sort extends \Predis\InlineCommand {
  300. public function getCommandId() { return 'SORT'; }
  301. public function filterArguments(Array $arguments) {
  302. if (count($arguments) === 1) {
  303. return $arguments;
  304. }
  305. // TODO: add more parameters checks
  306. $query = array($arguments[0]);
  307. $sortParams = $arguments[1];
  308. if (isset($sortParams['by'])) {
  309. $query[] = 'BY';
  310. $query[] = $sortParams['by'];
  311. }
  312. if (isset($sortParams['get'])) {
  313. $getargs = $sortParams['get'];
  314. if (is_array($getargs)) {
  315. foreach ($getargs as $getarg) {
  316. $query[] = 'GET';
  317. $query[] = $getarg;
  318. }
  319. }
  320. else {
  321. $query[] = 'GET';
  322. $query[] = $getargs;
  323. }
  324. }
  325. if (isset($sortParams['limit']) && is_array($sortParams['limit'])) {
  326. $query[] = 'LIMIT';
  327. $query[] = $sortParams['limit'][0];
  328. $query[] = $sortParams['limit'][1];
  329. }
  330. if (isset($sortParams['sort'])) {
  331. $query[] = strtoupper($sortParams['sort']);
  332. }
  333. if (isset($sortParams['alpha']) && $sortParams['alpha'] == true) {
  334. $query[] = 'ALPHA';
  335. }
  336. if (isset($sortParams['store']) && $sortParams['store'] == true) {
  337. $query[] = 'STORE';
  338. $query[] = $sortParams['store'];
  339. }
  340. return $query;
  341. }
  342. }
  343. /* persistence control commands */
  344. class Save extends \Predis\InlineCommand {
  345. public function canBeHashed() { return false; }
  346. public function getCommandId() { return 'SAVE'; }
  347. }
  348. class BackgroundSave extends \Predis\InlineCommand {
  349. public function canBeHashed() { return false; }
  350. public function getCommandId() { return 'BGSAVE'; }
  351. public function parseResponse($data) {
  352. if ($data == 'Background saving started') {
  353. return true;
  354. }
  355. return $data;
  356. }
  357. }
  358. class LastSave extends \Predis\InlineCommand {
  359. public function canBeHashed() { return false; }
  360. public function getCommandId() { return 'LASTSAVE'; }
  361. }
  362. class Shutdown extends \Predis\InlineCommand {
  363. public function canBeHashed() { return false; }
  364. public function getCommandId() { return 'SHUTDOWN'; }
  365. public function closesConnection() { return true; }
  366. }
  367. /* remote server control commands */
  368. class Info extends \Predis\InlineCommand {
  369. public function canBeHashed() { return false; }
  370. public function getCommandId() { return 'INFO'; }
  371. public function parseResponse($data) {
  372. $info = array();
  373. $infoLines = explode("\r\n", $data, -1);
  374. foreach ($infoLines as $row) {
  375. list($k, $v) = explode(':', $row);
  376. if (!preg_match('/^db\d+$/', $k)) {
  377. $info[$k] = $v;
  378. }
  379. else {
  380. $db = array();
  381. foreach (explode(',', $v) as $dbvar) {
  382. list($dbvk, $dbvv) = explode('=', $dbvar);
  383. $db[trim($dbvk)] = $dbvv;
  384. }
  385. $info[$k] = $db;
  386. }
  387. }
  388. return $info;
  389. }
  390. }
  391. class SlaveOf extends \Predis\InlineCommand {
  392. public function canBeHashed() { return false; }
  393. public function getCommandId() { return 'SLAVEOF'; }
  394. public function filterArguments(Array $arguments) {
  395. if (count($arguments) === 0 || $arguments[0] === 'NO ONE') {
  396. return array('NO', 'ONE');
  397. }
  398. return $arguments;
  399. }
  400. }
  401. ?>