Predis_Compatibility.php 21 KB

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