Predis_Compatibility.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. }
  185. class Type extends \Predis\InlineCommand {
  186. public function getCommandId() { return 'TYPE'; }
  187. }
  188. /* commands operating on the key space */
  189. class Keys extends \Predis\InlineCommand {
  190. public function canBeHashed() { return false; }
  191. public function getCommandId() { return 'KEYS'; }
  192. public function parseResponse($data) {
  193. return strlen($data) > 0 ? explode(' ', $data) : array();
  194. }
  195. }
  196. class RandomKey extends \Predis\InlineCommand {
  197. public function canBeHashed() { return false; }
  198. public function getCommandId() { return 'RANDOMKEY'; }
  199. public function parseResponse($data) { return $data !== '' ? $data : null; }
  200. }
  201. class Rename extends \Predis\InlineCommand {
  202. public function canBeHashed() { return false; }
  203. public function getCommandId() { return 'RENAME'; }
  204. }
  205. class RenamePreserve extends \Predis\InlineCommand {
  206. public function canBeHashed() { return false; }
  207. public function getCommandId() { return 'RENAMENX'; }
  208. public function parseResponse($data) { return (bool) $data; }
  209. }
  210. class Expire extends \Predis\InlineCommand {
  211. public function getCommandId() { return 'EXPIRE'; }
  212. public function parseResponse($data) { return (bool) $data; }
  213. }
  214. class ExpireAt extends \Predis\InlineCommand {
  215. public function getCommandId() { return 'EXPIREAT'; }
  216. public function parseResponse($data) { return (bool) $data; }
  217. }
  218. class DatabaseSize extends \Predis\InlineCommand {
  219. public function canBeHashed() { return false; }
  220. public function getCommandId() { return 'DBSIZE'; }
  221. }
  222. class TimeToLive extends \Predis\InlineCommand {
  223. public function getCommandId() { return 'TTL'; }
  224. }
  225. /* commands operating on lists */
  226. class ListPushTail extends \Predis\BulkCommand {
  227. public function getCommandId() { return 'RPUSH'; }
  228. }
  229. class ListPushHead extends \Predis\BulkCommand {
  230. public function getCommandId() { return 'LPUSH'; }
  231. }
  232. class ListLength extends \Predis\InlineCommand {
  233. public function getCommandId() { return 'LLEN'; }
  234. }
  235. class ListRange extends \Predis\InlineCommand {
  236. public function getCommandId() { return 'LRANGE'; }
  237. }
  238. class ListTrim extends \Predis\InlineCommand {
  239. public function getCommandId() { return 'LTRIM'; }
  240. }
  241. class ListIndex extends \Predis\InlineCommand {
  242. public function getCommandId() { return 'LINDEX'; }
  243. }
  244. class ListSet extends \Predis\BulkCommand {
  245. public function getCommandId() { return 'LSET'; }
  246. }
  247. class ListRemove extends \Predis\BulkCommand {
  248. public function getCommandId() { return 'LREM'; }
  249. }
  250. class ListPopFirst extends \Predis\InlineCommand {
  251. public function getCommandId() { return 'LPOP'; }
  252. }
  253. class ListPopLast extends \Predis\InlineCommand {
  254. public function getCommandId() { return 'RPOP'; }
  255. }
  256. /* commands operating on sets */
  257. class SetAdd extends \Predis\BulkCommand {
  258. public function getCommandId() { return 'SADD'; }
  259. public function parseResponse($data) { return (bool) $data; }
  260. }
  261. class SetRemove extends \Predis\BulkCommand {
  262. public function getCommandId() { return 'SREM'; }
  263. public function parseResponse($data) { return (bool) $data; }
  264. }
  265. class SetPop extends \Predis\InlineCommand {
  266. public function getCommandId() { return 'SPOP'; }
  267. }
  268. class SetMove extends \Predis\BulkCommand {
  269. public function canBeHashed() { return false; }
  270. public function getCommandId() { return 'SMOVE'; }
  271. public function parseResponse($data) { return (bool) $data; }
  272. }
  273. class SetCardinality extends \Predis\InlineCommand {
  274. public function getCommandId() { return 'SCARD'; }
  275. }
  276. class SetIsMember extends \Predis\BulkCommand {
  277. public function getCommandId() { return 'SISMEMBER'; }
  278. public function parseResponse($data) { return (bool) $data; }
  279. }
  280. class SetIntersection extends \Predis\InlineCommand {
  281. public function getCommandId() { return 'SINTER'; }
  282. }
  283. class SetIntersectionStore extends \Predis\InlineCommand {
  284. public function getCommandId() { return 'SINTERSTORE'; }
  285. }
  286. class SetUnion extends \Predis\InlineCommand {
  287. public function getCommandId() { return 'SUNION'; }
  288. }
  289. class SetUnionStore extends \Predis\InlineCommand {
  290. public function getCommandId() { return 'SUNIONSTORE'; }
  291. }
  292. class SetDifference extends \Predis\InlineCommand {
  293. public function getCommandId() { return 'SDIFF'; }
  294. }
  295. class SetDifferenceStore extends \Predis\InlineCommand {
  296. public function getCommandId() { return 'SDIFFSTORE'; }
  297. }
  298. class SetMembers extends \Predis\InlineCommand {
  299. public function getCommandId() { return 'SMEMBERS'; }
  300. }
  301. class SetRandomMember extends \Predis\InlineCommand {
  302. public function getCommandId() { return 'SRANDMEMBER'; }
  303. }
  304. /* multiple databases handling commands */
  305. class SelectDatabase extends \Predis\InlineCommand {
  306. public function canBeHashed() { return false; }
  307. public function getCommandId() { return 'SELECT'; }
  308. }
  309. class MoveKey extends \Predis\InlineCommand {
  310. public function canBeHashed() { return false; }
  311. public function getCommandId() { return 'MOVE'; }
  312. public function parseResponse($data) { return (bool) $data; }
  313. }
  314. class FlushDatabase extends \Predis\InlineCommand {
  315. public function canBeHashed() { return false; }
  316. public function getCommandId() { return 'FLUSHDB'; }
  317. }
  318. class FlushAll extends \Predis\InlineCommand {
  319. public function canBeHashed() { return false; }
  320. public function getCommandId() { return 'FLUSHALL'; }
  321. }
  322. /* sorting */
  323. class Sort extends \Predis\InlineCommand {
  324. public function getCommandId() { return 'SORT'; }
  325. public function filterArguments(Array $arguments) {
  326. if (count($arguments) === 1) {
  327. return $arguments;
  328. }
  329. // TODO: add more parameters checks
  330. $query = array($arguments[0]);
  331. $sortParams = $arguments[1];
  332. if (isset($sortParams['by'])) {
  333. $query[] = 'BY';
  334. $query[] = $sortParams['by'];
  335. }
  336. if (isset($sortParams['get'])) {
  337. $getargs = $sortParams['get'];
  338. if (is_array($getargs)) {
  339. foreach ($getargs as $getarg) {
  340. $query[] = 'GET';
  341. $query[] = $getarg;
  342. }
  343. }
  344. else {
  345. $query[] = 'GET';
  346. $query[] = $getargs;
  347. }
  348. }
  349. if (isset($sortParams['limit']) && is_array($sortParams['limit'])) {
  350. $query[] = 'LIMIT';
  351. $query[] = $sortParams['limit'][0];
  352. $query[] = $sortParams['limit'][1];
  353. }
  354. if (isset($sortParams['sort'])) {
  355. $query[] = strtoupper($sortParams['sort']);
  356. }
  357. if (isset($sortParams['alpha']) && $sortParams['alpha'] == true) {
  358. $query[] = 'ALPHA';
  359. }
  360. if (isset($sortParams['store']) && $sortParams['store'] == true) {
  361. $query[] = 'STORE';
  362. $query[] = $sortParams['store'];
  363. }
  364. return $query;
  365. }
  366. }
  367. /* persistence control commands */
  368. class Save extends \Predis\InlineCommand {
  369. public function canBeHashed() { return false; }
  370. public function getCommandId() { return 'SAVE'; }
  371. }
  372. class BackgroundSave extends \Predis\InlineCommand {
  373. public function canBeHashed() { return false; }
  374. public function getCommandId() { return 'BGSAVE'; }
  375. public function parseResponse($data) {
  376. if ($data == 'Background saving started') {
  377. return true;
  378. }
  379. return $data;
  380. }
  381. }
  382. class LastSave extends \Predis\InlineCommand {
  383. public function canBeHashed() { return false; }
  384. public function getCommandId() { return 'LASTSAVE'; }
  385. }
  386. class Shutdown extends \Predis\InlineCommand {
  387. public function canBeHashed() { return false; }
  388. public function getCommandId() { return 'SHUTDOWN'; }
  389. public function closesConnection() { return true; }
  390. }
  391. /* remote server control commands */
  392. class Info extends \Predis\InlineCommand {
  393. public function canBeHashed() { return false; }
  394. public function getCommandId() { return 'INFO'; }
  395. public function parseResponse($data) {
  396. $info = array();
  397. $infoLines = explode("\r\n", $data, -1);
  398. foreach ($infoLines as $row) {
  399. list($k, $v) = explode(':', $row);
  400. if (!preg_match('/^db\d+$/', $k)) {
  401. $info[$k] = $v;
  402. }
  403. else {
  404. $db = array();
  405. foreach (explode(',', $v) as $dbvar) {
  406. list($dbvk, $dbvv) = explode('=', $dbvar);
  407. $db[trim($dbvk)] = $dbvv;
  408. }
  409. $info[$k] = $db;
  410. }
  411. }
  412. return $info;
  413. }
  414. }
  415. class SlaveOf extends \Predis\InlineCommand {
  416. public function canBeHashed() { return false; }
  417. public function getCommandId() { return 'SLAVEOF'; }
  418. public function filterArguments(Array $arguments) {
  419. if (count($arguments) === 0 || $arguments[0] === 'NO ONE') {
  420. return array('NO', 'ONE');
  421. }
  422. return $arguments;
  423. }
  424. }
  425. ?>