UnixSocketRedisTest.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Tioncico
  5. * Date: 2019/9/24 0024
  6. * Time: 16:16
  7. */
  8. namespace Test;
  9. use EasySwoole\Redis\Client;
  10. use EasySwoole\Redis\Config\RedisConfig;
  11. use EasySwoole\Redis\Exception\RedisException;
  12. use EasySwoole\Redis\Redis;
  13. use PHPUnit\Framework\TestCase;
  14. use Swoole\Coroutine;
  15. class UnixSocketRedisTest extends TestCase
  16. {
  17. /**
  18. * @var $redis Redis
  19. */
  20. protected $redis;
  21. protected function setUp():void
  22. {
  23. parent::setUp(); // TODO: Change the autogenerated stub
  24. $this->redis = new Redis(new RedisConfig([
  25. 'unixSocket' => REDIS_UNIX_SOCKET,
  26. ]));
  27. $this->redis->connect();
  28. //测试原始rawCommand
  29. $redis = $this->redis;
  30. $data = $redis->rawCommand(['set','a','1']);
  31. $this->assertEquals('OK',$data->getData());
  32. $data = $redis->rawCommand(['get','a']);
  33. $this->assertEquals('1',$data->getData());
  34. $redis->del('a');
  35. }
  36. function testConnect()
  37. {
  38. $this->assertTrue($this->redis->connect());
  39. }
  40. public function testAuth()
  41. {
  42. if (!empty(REDIS_AUTH)) {
  43. $this->assertTrue($this->redis->auth(REDIS_AUTH));
  44. }
  45. $this->assertTrue(true);
  46. }
  47. /**
  48. * key值操作测试
  49. * testKey
  50. * @author Tioncico
  51. * Time: 10:02
  52. */
  53. function testKey()
  54. {
  55. $redis = $this->redis;
  56. $key = 'test123213Key';
  57. $redis->select(0);
  58. $redis->flushAll();
  59. $redis->set($key, 123);
  60. $data = $redis->dump($key);
  61. $this->assertTrue(!!$data);
  62. $data = $redis->dump($key . 'x');
  63. $this->assertNull($data);
  64. $data = $this->redis->exists($key);
  65. $this->assertEquals(1, $data);
  66. $data = $this->redis->expire($key, 1);
  67. $this->assertEquals(1, $data);
  68. Coroutine::sleep(2);
  69. $this->assertEquals(0, $this->redis->exists($key));
  70. $redis->set($key, 123);
  71. $data = $this->redis->pExpire($key, 1000);
  72. $this->assertEquals(1, $data);
  73. $this->assertEquals(123, $this->redis->get($key));
  74. Coroutine::sleep(2);
  75. $this->assertEquals(0, $this->redis->pExpire($key));
  76. $redis->expireAt($key, 1 * 100);
  77. Coroutine::sleep(0.1);
  78. $this->assertEquals(0, $this->redis->exists($key));
  79. $redis->set($key, 123);
  80. $data = $redis->keys("{$key}");
  81. $this->assertEquals($key, $data[0]);
  82. $redis->select(1);
  83. $redis->del($key);
  84. $redis->select(0);
  85. $data = $redis->move($key, 1);
  86. $this->assertEquals(1, $data);
  87. $data = $redis->exists($key);
  88. $this->assertEquals(0, $data);
  89. $redis->select(0);
  90. $redis->set($key, 123);
  91. $data = $redis->expire($key, 1);
  92. $this->assertEquals(1, $data);
  93. $data = $redis->persist($key);
  94. $this->assertEquals(1, $data);
  95. $redis->expire($key, 1);
  96. $data = $redis->pTTL($key);
  97. $this->assertLessThanOrEqual(1000, $data);
  98. $data = $redis->ttl($key);
  99. $this->assertLessThanOrEqual(1, $data);
  100. $data = $redis->randomKey();
  101. $this->assertTrue(!!$data);
  102. $data = $redis->rename($key, $key . 'new');
  103. $this->assertTrue($data);
  104. $redis->set($key.'old',1);
  105. $data = $redis->renameNx($key.'old', $key . 'new');
  106. $this->assertEquals(0, $data);
  107. $redis->renameNx($key . 'new', $key);
  108. $data = $redis->renameNx($key, $key . 'new');
  109. $this->assertEquals(1, $data);
  110. $data = $redis->type($key);
  111. $this->assertEquals('none', $data);
  112. $data = $redis->type($key . 'new');
  113. $this->assertEquals('string', $data);
  114. //del单元测试新增
  115. $keyArr = ['a1','a2','a3','a4'];
  116. $valueArr = ['a1','a2','a3','a4'];
  117. $redis->mSet([
  118. $keyArr[0]=>$valueArr[0],
  119. $keyArr[1]=>$valueArr[1],
  120. $keyArr[2]=>$valueArr[2],
  121. $keyArr[3]=>$valueArr[3],
  122. ]);
  123. $data = $redis->del($keyArr[0]);
  124. $this->assertEquals(1,$data);
  125. $data = $redis->del($keyArr[1],$keyArr[2],$keyArr[3]);
  126. $this->assertEquals(3,$data);
  127. $redis->mSet([
  128. $keyArr[0]=>$valueArr[0],
  129. $keyArr[1]=>$valueArr[1],
  130. $keyArr[2]=>$valueArr[2],
  131. $keyArr[3]=>$valueArr[3],
  132. ]);
  133. $data = $redis->del([$keyArr[1],$keyArr[2],$keyArr[3]]);
  134. $this->assertEquals(3,$data);
  135. //unlink命令新增
  136. $keyArr = ['a1','a2','a3','a4'];
  137. $valueArr = ['a1','a2','a3','a4'];
  138. $redis->mSet([
  139. $keyArr[0]=>$valueArr[0],
  140. $keyArr[1]=>$valueArr[1],
  141. $keyArr[2]=>$valueArr[2],
  142. $keyArr[3]=>$valueArr[3],
  143. ]);
  144. $data = $redis->unlink($keyArr[0]);
  145. $this->assertEquals(1,$data);
  146. $data = $redis->unlink($keyArr[0].'test');
  147. $this->assertEquals(0,$data);
  148. $data = $redis->unlink($keyArr[1],$keyArr[2],$keyArr[3]);
  149. $this->assertEquals(3,$data);
  150. $redis->mSet([
  151. $keyArr[0]=>$valueArr[0],
  152. $keyArr[1]=>$valueArr[1],
  153. $keyArr[2]=>$valueArr[2],
  154. $keyArr[3]=>$valueArr[3],
  155. ]);
  156. $data = $redis->unlink([$keyArr[1],$keyArr[2],$keyArr[3]]);
  157. $this->assertEquals(3,$data);
  158. }
  159. /**
  160. * 字符串单元测试
  161. * testString
  162. * @author tioncico
  163. * Time: 下午9:41
  164. */
  165. function testString()
  166. {
  167. $redis = $this->redis;
  168. $key = 'test';
  169. $value = 1;
  170. $data = $redis->del($key);
  171. $this->assertNotFalse($data);
  172. $data = $redis->set($key, $value);
  173. $this->assertTrue($data);
  174. $redis->set($key, $value);
  175. $data = $redis->get($key);
  176. $this->assertEquals($data, $value);
  177. //set的其他作用测试(超时作用)
  178. $keyTTL=$key.'ttl';
  179. $redis->set($keyTTL,$value,20);
  180. $this->assertGreaterThan(18,$redis->ttl($keyTTL));
  181. $redis->set($keyTTL,$value,['EX'=>20]);
  182. $this->assertGreaterThan(18,$redis->ttl($keyTTL));
  183. $redis->set($keyTTL,$value,['PX'=>20000]);
  184. $this->assertGreaterThan(18000,$redis->pTTL($keyTTL));
  185. $this->assertGreaterThan(18,$redis->ttl($keyTTL));
  186. //set的其他作用测试(存在以及不存在时候设置)
  187. $keyExist = $key.'exist';
  188. $redis->del($keyExist);
  189. $data = $redis->set($keyExist,'1','XX');
  190. $this->assertNull($data);
  191. $data = $redis->set($keyExist,'1','NX');
  192. $this->assertTrue($data);
  193. $this->assertEquals('1',$redis->get($keyExist));
  194. $data = $redis->set($keyExist,'1','NX');
  195. $this->assertNull($data);
  196. $data = $redis->set($keyExist,'2','XX');
  197. $this->assertTrue($data);
  198. $this->assertEquals('2',$redis->get($keyExist));
  199. //set组合测试
  200. $data = $redis->set($keyExist,'3',['XX','EX'=>10]);
  201. $this->assertTrue($data);
  202. $this->assertEquals('3',$redis->get($keyExist));
  203. $this->assertGreaterThan(8,$redis->ttl($keyTTL));
  204. $data = $redis->set($keyExist,'3',['NX','EX'=>10]);
  205. $this->assertNull($data);
  206. $redis->del($keyExist);;
  207. $redis->set($keyExist,'4',['NX','EX'=>20]);
  208. $this->assertEquals('4',$redis->get($keyExist));
  209. $this->assertGreaterThan(18,$redis->ttl($keyTTL));
  210. $data = $redis->exists($key);
  211. $this->assertEquals(1, $data);
  212. $data = $redis->set($key, $value);
  213. $this->assertTrue($data);
  214. $value += 1;
  215. $data = $redis->incr($key);
  216. $this->assertEquals($value, $data);
  217. $value += 10;
  218. $data = $redis->incrBy($key, 10);
  219. $this->assertEquals($value, $data);
  220. $value -= 1;
  221. $data = $redis->decr($key);
  222. $this->assertEquals($value, $data);
  223. $value -= 10;
  224. $data = $redis->decrBy($key, 10);
  225. $this->assertEquals($value, $data);
  226. $key = 'stringTest';
  227. $value = 'tioncico';
  228. $redis->set($key, $value);
  229. $data = $redis->getRange($key, 1, 2);
  230. $this->assertEquals('io', $data);
  231. $data = $redis->getSet($key, $value . 'a');
  232. $this->assertEquals($data, $value);
  233. $redis->set($key, $value);
  234. $bitKey = 'testBit';
  235. $bitValue = 10000;
  236. $redis->set($bitKey, $bitValue);
  237. $data = $redis->setBit($bitKey, 1, 0);
  238. $this->assertEquals(0, $data);
  239. $data = $redis->getBit($key, 1);
  240. $this->assertEquals(1, $data);
  241. $field = [
  242. 'stringField1',
  243. 'stringField2',
  244. 'stringField3',
  245. 'stringField4',
  246. 'stringField5',
  247. ];
  248. $value = [
  249. 1,
  250. 2,
  251. 3,
  252. 4,
  253. 5,
  254. ];
  255. $data = $redis->mSet([
  256. "{$field[0]}" => $value[0],
  257. "{$field[1]}" => $value[1],
  258. "{$field[2]}" => $value[2],
  259. "{$field[3]}" => $value[3],
  260. "{$field[4]}" => $value[4],
  261. ]);
  262. $this->assertTrue($data);
  263. $data = $redis->mGet([$field[3], $field[2], $field[1]]);
  264. $this->assertEquals([$value[3], $value[2], $value[1]], $data);
  265. $data = $redis->setEx($key, 1, $value[0] . $value[0]);
  266. $this->assertTrue($data);
  267. $this->assertEquals($value[0] . $value[0], $redis->get($key));
  268. $data = $redis->pSetEx($key, 1, $value[0]);
  269. $this->assertTrue($data);
  270. $this->assertEquals($value[0], $redis->get($key));
  271. $redis->del($key);
  272. $data = $redis->setNx($key, 1);
  273. $this->assertEquals(1, $data);
  274. $redis->del($field[0]);
  275. $data = $redis->mSetNx([
  276. "{$field[0]}" => $value[0],
  277. "{$field[1]}" => $value[1],
  278. ]);
  279. $this->assertEquals(0, $data);
  280. $this->assertEquals($value[1], $redis->get($field[1]));
  281. $redis->del($field[1]);
  282. $data = $redis->mSetNx([
  283. "{$field[0]}" => $value[0] + 1,
  284. "{$field[1]}" => $value[1] + 1,
  285. ]);
  286. $this->assertEquals(1, $data);
  287. $this->assertEquals($value[0] + 1, $redis->get($field[0]));
  288. $data = $redis->setRange($field[0], 1, 1);
  289. $this->assertEquals(2, $data);
  290. $this->assertEquals('2' . $value[0], $redis->get($field[0]));
  291. $data = $redis->strLen($field[0]);
  292. $this->assertEquals(2, $data);
  293. $redis->set($key, 1);
  294. $data = $redis->incrByFloat($key, 0.1);
  295. $this->assertEquals(1.1, $data);
  296. $data = $redis->appEnd($field[0], '1');
  297. $this->assertEquals($redis->strLen($field[0]), $data);
  298. $this->assertEquals('2' . $value[0] . '1', $redis->get($field[0]));
  299. //迭代测试
  300. $cursor = 0;
  301. $redis->flushAll();
  302. $redis->set('xxxa', '仙士可');
  303. $redis->set('xxxb', '仙士可');
  304. $redis->set('xxxc', '仙士可');
  305. $redis->set('xxxd', '仙士可');
  306. $data = [];
  307. do {
  308. $keys = $redis->scan($cursor, 'xxx*', 1);
  309. $data = array_merge($data, $keys);
  310. } while ($cursor);
  311. $this->assertEquals(4, count($data));
  312. }
  313. /**
  314. * testHash
  315. * @author Tioncico
  316. * Time: 11:54
  317. */
  318. function testHash()
  319. {
  320. $key = 'hKey';
  321. $field = [
  322. 'hField1',
  323. 'hField2',
  324. 'hField3',
  325. 'hField4',
  326. 'hField5',
  327. ];
  328. $value = [
  329. 1,
  330. 2,
  331. 3,
  332. 4,
  333. 5,
  334. ];
  335. $redis = $this->redis;
  336. $redis->del($key);
  337. $data = $redis->hSet($key, $field[0], $value[0]);
  338. $this->assertNotFalse($data);
  339. $data = $redis->hGet($key, $field[0]);
  340. $this->assertEquals($data, $value[0]);
  341. $data = $redis->hExists($key, $field[0]);
  342. $this->assertEquals(1, $data);
  343. $data = $redis->hDel($key, $field[0]);
  344. $this->assertEquals(1, $data, $redis->getErrorMsg());
  345. $data = $redis->hExists($key, $field[0]);
  346. $this->assertEquals(0, $data);
  347. $data = $redis->hMSet($key, [
  348. "{$field[0]}" => $value[0],
  349. "{$field[1]}" => $value[1],
  350. "{$field[2]}" => $value[2],
  351. "{$field[3]}" => $value[3],
  352. "{$field[4]}" => $value[4],
  353. ]);
  354. $this->assertTrue($data);
  355. $data = $redis->hValS($key);
  356. sort($data);
  357. $this->assertEquals($value, $data);
  358. $data = $redis->hGetAll($key);
  359. $keyTmp = array_keys($data);
  360. sort($keyTmp);
  361. $this->assertEquals($field, $keyTmp);
  362. $valueTmp = array_values($data);
  363. sort($valueTmp);
  364. $this->assertEquals($value, $valueTmp);
  365. $this->assertEquals($value, [
  366. $data[$field[0]],
  367. $data[$field[1]],
  368. $data[$field[2]],
  369. $data[$field[3]],
  370. $data[$field[4]],
  371. ]);
  372. $data = $redis->hKeys($key);
  373. sort($data);
  374. $this->assertEquals($field, $data);
  375. $data = $redis->hLen($key);
  376. $this->assertEquals(count($field), $data);
  377. $data = $redis->hMGet($key, [$field[0], $field[1], $field[2]]);
  378. $this->assertEquals([1, 2, 3], $data);
  379. $data = $redis->hIncrBy($key, $field[4], 1);
  380. $this->assertEquals($value[4] + 1, $data);
  381. $data = $redis->hIncrByFloat($key, $field[1], 1.1);
  382. $this->assertEquals($value[1] + 1.1, $data);
  383. $data = $redis->hSetNx($key, $field[0], 1);
  384. $this->assertEquals(0, $data);
  385. $data = $redis->hSetNx($key, $field[0] . 'a', 1);
  386. $this->assertEquals(1, $data);
  387. $this->assertEquals(1, $redis->hGet($key, $field[0] . 'a'));
  388. $cursor = 0;
  389. $redis->del('a');
  390. $redis->hMSet('a', [
  391. 'a' => 'tioncico',
  392. 'b' => 'tioncico',
  393. 'c' => 'tioncico',
  394. 'd' => 'tioncico',
  395. 'e' => 'tioncico',
  396. 'f' => 'tioncico',
  397. 'g' => 'tioncico',
  398. 'h' => 'tioncico',
  399. ]);
  400. $data = [];
  401. do {
  402. $keys = $redis->hScan('a', $cursor);
  403. $data = array_merge($data, $keys);
  404. } while ($cursor);
  405. $this->assertEquals(8, count($data));
  406. }
  407. /**
  408. * testList
  409. * @author tioncico
  410. * Time: 下午8:17
  411. */
  412. function testList()
  413. {
  414. $redis = $this->redis;
  415. $key = [
  416. 'listKey1',
  417. 'listKey2',
  418. 'listKey3',
  419. ];
  420. $value = [
  421. 'a', 'b', 'c', 'd'
  422. ];
  423. $redis->flushAll();
  424. //测试null的时候
  425. $data = $redis->bLPop([$key[0], $key[1]], 1);
  426. $this->assertNull($data);
  427. $data = $redis->lPush($key[0], $value[0], $value[1]);
  428. $this->assertEquals(2, $data);
  429. //测试null的时候
  430. $data = $redis->bLPop([$key[1]], 1);
  431. $this->assertNull($data);
  432. $data = $redis->bRPop([$key[1]], 1);
  433. $this->assertNull($data);
  434. $data = $redis->bLPop([$key[0], $key[1]], 1);
  435. $this->assertTrue(!!$data);
  436. $data = $redis->bRPop([$key[0], $key[1]], 1);
  437. $this->assertTrue(!!$data);
  438. $redis->del($key[0]);
  439. $redis->lPush($key[0], $value[0], $value[1]);
  440. $data = $redis->bRPopLPush($key[0], $key[1], 1);
  441. $this->assertEquals($value[0], $data);
  442. $redis->del($key[0]);
  443. $redis->lPush($key[0], $value[0], $value[1]);
  444. $data = $redis->rPopLPush($key[0], $key[1]);
  445. $this->assertEquals($value[0], $data);
  446. $redis->del($key[0]);
  447. $redis->lPush($key[0], $value[0], $value[1]);
  448. $data = $redis->lIndex($key[0], 1);
  449. $this->assertEquals($value[0], $data);
  450. $data = $redis->lLen($key[0]);
  451. $this->assertEquals(2, $data);
  452. $data = $redis->lInsert($key[0], true, 'b', 'c');
  453. $this->assertEquals($redis->lLen($key[0]), $data);
  454. $data = $redis->lInsert($key[0], true, 'd', 'c');
  455. $this->assertEquals(-1, $data);
  456. $redis->del($key[1]);
  457. $data = $redis->rPush($key[1], $value[0], $value[2], $value[1]);
  458. $this->assertEquals(3, $data);
  459. $data = $redis->lRange($key[1], 0, 3);
  460. $this->assertEquals(count($data), 3);
  461. $data = $redis->lPop($key[1]);
  462. $this->assertEquals($value[0], $data);
  463. $data = $redis->rPop($key[1]);
  464. $this->assertEquals($value[1], $data);
  465. $data = $redis->lPuShx($key[1], 'x');
  466. $this->assertEquals($redis->lLen($key[1]), $data);
  467. $this->assertEquals('x', $redis->lPop($key[1]));
  468. $data = $redis->rPuShx($key[1], 'z');
  469. $this->assertEquals($redis->lLen($key[1]), $data);
  470. $this->assertEquals('z', $redis->rPop($key[1]));
  471. $redis->del($key[1]);
  472. $redis->rPush($key[1], $value[0], $value[0], $value[0]);
  473. $data = $redis->lRem($key[1], 1, $value[0]);
  474. $this->assertEquals(1, $data);
  475. $data = $redis->lSet($key[1], 0, 'xx');
  476. $this->assertTrue($data);
  477. $this->assertEquals('xx', $redis->lPop($key[1]));
  478. $data = $redis->lTrim($key[1], 0, 2);
  479. $this->assertTrue($data);
  480. $this->assertEquals(1, $redis->lLen($key[1]));
  481. }
  482. /**
  483. * 集合测试
  484. * testMuster
  485. * @author Tioncico
  486. * Time: 9:10
  487. */
  488. function testMuster()
  489. {
  490. $redis = $this->redis;
  491. $key = [
  492. 'muster1',
  493. 'muster2',
  494. 'muster3',
  495. 'muster4',
  496. 'muster5',
  497. ];
  498. $value = [
  499. '1',
  500. '2',
  501. '3',
  502. '4',
  503. ];
  504. $redis->del($key[0]);
  505. $redis->del($key[1]);
  506. $data = $redis->sAdd($key[0], $value[0], $value[1]);
  507. $this->assertEquals(2, $data);
  508. $data = $redis->sCard($key[0]);
  509. $this->assertEquals(2, $data);
  510. $redis->sAdd($key[1], $value[0], $value[2]);
  511. $data = $redis->sDiff($key[0], $key[1]);
  512. $this->assertEquals([$value[1]], $data);
  513. $data = $redis->sDiff($key[1], $key[0]);
  514. $this->assertEquals([$value[2]], $data);
  515. $data = $redis->sMembers($key[0]);
  516. $this->assertEquals([$value[0], $value[1]], $data);
  517. $data = $redis->sMembers($key[1]);
  518. $this->assertEquals([$value[0], $value[2]], $data);
  519. $data = $redis->sDiffStore($key[2], $key[0], $key[1]);
  520. $this->assertEquals(1, $data);
  521. $data = $redis->sMembers($key[2]);
  522. $this->assertEquals([$value[1]], $data);
  523. $data = $redis->sInter($key[0], $key[1]);
  524. $this->assertEquals([$value[0]], $data);
  525. $data = $redis->sInterStore($key[3], $key[0], $key[1]);
  526. $this->assertEquals(1, $data);
  527. $this->assertEquals([$value[0]], $redis->sMembers($key[3]));
  528. $data = $redis->sIsMember($key[0], $value[0]);
  529. $this->assertEquals(1, $data);
  530. $data = $redis->sIsMember($key[0], $value[3]);
  531. $this->assertEquals(0, $data);
  532. $data = $redis->sMove($key[0], $key[1], $value[1]);
  533. $this->assertEquals(1, $data);
  534. $data = $redis->sPop($key[0]);
  535. $this->assertEquals(1, $data);
  536. $redis->del($key[3]);
  537. $redis->sAdd($key[3], $value[0], $value[1], $value[2], $value[3]);
  538. $data = $redis->sRandMember($key[3], 4);
  539. $this->assertEquals(4, count($data));
  540. $data = $redis->sRandMember($key[3].rand(1,1111));
  541. $this->assertEquals(null, $data);
  542. $data = $redis->sRandMember($key[3]);
  543. $this->assertTrue(in_array($data,$value));
  544. $data = $redis->sRem($key[3], $value[0], $value[1], $value[2], $value[3]);
  545. $this->assertEquals(4, $data);
  546. $this->assertEquals([], $redis->sMembers($key[3]));
  547. $data = $redis->sUnion($key[0], $key[1]);
  548. $this->assertEquals([$value[0], $value[1], $value[2]], $data);
  549. $redis->del($key[1]);
  550. $redis->del($key[2]);
  551. $redis->del($key[3]);
  552. $redis->del($key[4]);
  553. $redis->sAdd($key[1], 1, 2, 3, 4);
  554. $redis->sAdd($key[2], 5);
  555. $redis->sAdd($key[3], 6, 7);
  556. $data = $redis->sUnIonStore($key[4], $key[1], $key[2], $key[3]);
  557. $this->assertEquals(7, $data);
  558. $cursor = 0;
  559. $redis->del('a');
  560. $redis->sAdd('a', 'a1', 'a2', 'a3', 'a4', 'a5');
  561. $data = [];
  562. do {
  563. $keys = $redis->sScan('a', $cursor, '*', 1);
  564. $data = array_merge($data, $keys);
  565. } while ($cursor);
  566. $this->assertEquals(5, count($data));
  567. }
  568. /**
  569. * 有序集合测试
  570. * testSortMuster
  571. * @author Tioncico
  572. * Time: 14:17
  573. */
  574. function testSortMuster()
  575. {
  576. $redis = $this->redis;
  577. $key = [
  578. 'sortMuster1',
  579. 'sortMuster2',
  580. 'sortMuster3',
  581. 'sortMuster4',
  582. 'sortMuster5',
  583. ];
  584. $member = [
  585. 'member1',
  586. 'member2',
  587. 'member3',
  588. 'member4',
  589. 'member5',
  590. ];
  591. $score = [
  592. 1,
  593. 2,
  594. 3,
  595. 4,
  596. ];
  597. $redis->del($key[0]);
  598. $data = $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  599. $this->assertEquals(2, $data);
  600. $data = $redis->zCard($key[0]);
  601. $this->assertEquals(2, $data);
  602. $data = $redis->zCount($key[0], 0, 3);
  603. $this->assertEquals(2, $data);
  604. $data = $redis->zInCrBy($key[0], 1, $member[1]);
  605. $this->assertEquals($score[1] + 1, $data);
  606. $redis->del($key[0]);
  607. $redis->del($key[1]);
  608. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  609. $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
  610. $data = $redis->zInTerStore($key[2], [$key[0], $key[1]], [1, 2]);
  611. $this->assertEquals(1, $data);
  612. $data = $redis->zLexCount($key[0], '-', '+');
  613. $this->assertEquals(2, $data);
  614. $redis->del($key[0]);
  615. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  616. $data = $redis->zRange($key[0], 0, -1, true);
  617. $this->assertEquals([
  618. $member[0] => $score[0],
  619. $member[1] => $score[1],
  620. $member[2] => $score[2],
  621. ], $data);
  622. $data = $redis->zRange($key[0], 0, -1, false);
  623. $this->assertEquals([
  624. $member[0],
  625. $member[1],
  626. $member[2],
  627. ], $data);
  628. $data = $redis->zRangeByLex($key[0], '-', '+');
  629. $this->assertEquals(3, count($data));
  630. $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => true, 'limit' => array(0, 2)]);
  631. $this->assertEquals([
  632. $member[1] => $score[1],
  633. $member[2] => $score[2],
  634. ], $data);
  635. $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => false, 'limit' => array(0, 2)]);
  636. $this->assertEquals([
  637. $member[1],
  638. $member[2],
  639. ], $data);
  640. $data = $redis->zRank($key[0], $member[1]);
  641. $this->assertEquals(1, $data);
  642. $data = $redis->zRem($key[0], $member[1], $member[2]);
  643. $this->assertEquals(2, $data);
  644. $redis->del($key[0]);
  645. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  646. $data = $redis->zRemRangeByLex($key[0], '-', '+');
  647. $this->assertEquals(3, $data);
  648. $redis->del($key[0]);
  649. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  650. $data = $redis->zRemRangeByRank($key[0], 0, 2);
  651. $this->assertEquals(3, $data);
  652. $redis->del($key[0]);
  653. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  654. $data = $redis->zRemRangeByScore($key[0], 0, 3);
  655. $this->assertEquals(3, $data);
  656. $redis->del($key[0]);
  657. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  658. $data = $redis->zRevRange($key[0], 0, 3);
  659. $this->assertEquals([
  660. $member[2],
  661. $member[1],
  662. $member[0],
  663. ], $data);
  664. $redis->del($key[0]);
  665. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  666. $data = $redis->zRevRange($key[0], 0, 3, true);
  667. $this->assertEquals([
  668. $member[2] => $score[2],
  669. $member[1] => $score[1],
  670. $member[0] => $score[0],
  671. ], $data);
  672. $redis->del($key[0]);
  673. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  674. $data = $redis->zRevRangeByScore($key[0], 3, 0, ['withScores' => true, 'limit' => array(0, 3)]);
  675. $this->assertEquals([
  676. $member[2] => $score[2],
  677. $member[1] => $score[1],
  678. $member[0] => $score[0],
  679. ], $data);
  680. $redis->del($key[0]);
  681. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  682. $data = $redis->zReVRangeByScore($key[0], 3, 0, ['withScores' => false, 'limit' => array(0, 3)]);
  683. $this->assertEquals([
  684. $member[2],
  685. $member[1],
  686. $member[0],
  687. ], $data);
  688. $data = $redis->zRevRank($key[0], $member[0]);
  689. $this->assertEquals(2, $data);
  690. $data = $redis->zScore($key[0], $member[0]);
  691. $this->assertEquals($score[0], $data);
  692. $redis->del($key[0]);
  693. $redis->del($key[1]);
  694. $redis->del($key[2]);
  695. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  696. $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
  697. $data = $redis->zUnionStore($key[2], [$key[1], $key[0]]);
  698. $this->assertEquals(3, $data);
  699. $cursor = 0;
  700. $redis->del('a');
  701. $redis->zAdd('a', 1, 'a1', 2, 'a2', 3, 'a3', 4, 'a4', 5, 'a5');
  702. $data = [];
  703. do {
  704. $keys = $redis->zScan('a', $cursor, '*', 1);
  705. $data = array_merge($data, $keys);
  706. } while ($cursor);
  707. $this->assertEquals(5, count($data));
  708. }
  709. /**
  710. * Stream 测试
  711. * @author gaobinzhan <gaobinzhan@gmail.com>
  712. */
  713. function testStream(){
  714. $redis = $this->redis;
  715. $id = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  716. $this->assertIsString($id);
  717. $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  718. $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  719. $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  720. $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy'], 1);
  721. $length = $redis->xLen('test');
  722. $this->assertEquals(1, $length);
  723. $ids[] = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  724. $ids[] = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  725. $delNum = $redis->xDel('test',$ids);
  726. $this->assertEquals(2, $delNum);
  727. $result = $redis->xRange('test','-','+',0);
  728. $this->assertEmpty($result);
  729. $result = $redis->xRange('test','-','+');
  730. $this->assertIsArray($result);
  731. $result = $redis->xRevRange('test','+','-',0);
  732. $this->assertEmpty($result);
  733. $result = $redis->xRevRange('test','+','-');
  734. $this->assertIsArray($result);
  735. $redis->del('test1');
  736. $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
  737. $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
  738. $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
  739. $len = $redis->xTrim('test1',2);
  740. $this->assertEquals(1, $len);
  741. $result = $redis->xRead(['test1' => '0-0'],1,0);
  742. $this->assertIsArray($result);
  743. $this->assertCount(1,$result['test1']);
  744. $result = $redis->xRead(['test1' => '0-0'],2,0);
  745. $this->assertCount(2,$result['test1']);
  746. $result = $redis->xRead(['test' => '$'],1,1000);
  747. $this->assertFalse($result);
  748. go(function (){
  749. $result = (new Redis(new RedisConfig([
  750. 'host' => REDIS_HOST,
  751. 'port' => REDIS_PORT,
  752. 'auth' => REDIS_AUTH
  753. ])))->xRead(['test' => '$'],1,1000);
  754. $this->assertIsArray($result);
  755. });
  756. go(function (){
  757. (new Redis(new RedisConfig([
  758. 'host' => REDIS_HOST,
  759. 'port' => REDIS_PORT,
  760. 'auth' => REDIS_AUTH
  761. ])))->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  762. });
  763. $array = $redis->xGroup('HELP');
  764. $this->assertIsArray($array);
  765. $groupName = time();
  766. $bool = $redis->xGroup('CREATE','mystream',$groupName,'$',true);
  767. $this->assertTrue($bool);
  768. $bool = $redis->xGroup('SETID','mystream',$groupName,0);
  769. $this->assertTrue($bool);
  770. $int = $redis->xGroup('DELCONSUMER','mystream',$groupName,1);
  771. $this->assertEquals(0,$int);
  772. $num = $redis->xGroup('DESTROY','mystream',$groupName);
  773. $this->assertEquals(1,$num);
  774. $array = $redis->xInfo('STREAM','mystream');
  775. $this->assertIsArray($array);
  776. $groupName =time();
  777. $redis->xGroup('CREATE','mystream',$groupName,'$',true);
  778. $array = $redis->xInfo('GROUPS','mystream');
  779. $this->assertIsArray($array);
  780. $array = $redis->xPending('mystream',$groupName);
  781. $this->assertIsArray($array);
  782. $array = $redis->xPending('mystream',$groupName,'-','+',10);
  783. $this->assertIsArray($array);
  784. $array = $redis->xPending('mystream',$groupName,'-','+',10,'consumer-123');
  785. $this->assertIsArray($array);
  786. $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],1,1000);
  787. $this->assertFalse($result);
  788. go(function () use ($groupName){
  789. $result = (new Redis(new RedisConfig([
  790. 'host' => REDIS_HOST,
  791. 'port' => REDIS_PORT,
  792. 'auth' => REDIS_AUTH
  793. ])))->xReadGroup($groupName,'consumer-111',['mystream' => '>'],1,1000);
  794. $this->assertIsArray($result);
  795. });
  796. go(function (){
  797. (new Redis(new RedisConfig([
  798. 'host' => REDIS_HOST,
  799. 'port' => REDIS_PORT,
  800. 'auth' => REDIS_AUTH
  801. ])))->xAdd('mystream','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  802. });
  803. $redis->xAdd('mystream','*',['name' => 1]);
  804. $num = $redis->xAck('mystream',$groupName,[0]);
  805. $this->assertEquals(0,$num);
  806. $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],1,1000);
  807. $num = $redis->xAck('mystream',$groupName,array_keys($result['mystream']));
  808. $this->assertEquals(1,$num);
  809. $redis->xAdd('mystream','*',['name' => 1]);
  810. $redis->xAdd('mystream','*',['name' => 1]);
  811. $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],2,0);
  812. $num = $redis->xAck('mystream',$groupName,array_keys($result['mystream']));
  813. $this->assertEquals(2,$num);
  814. $redis->xAdd('mystream','*',['name' => 1]);
  815. $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],2,0);
  816. $result = $redis->xPending('mystream',$groupName,'-','+',2);
  817. $ids[] = $result[0][0];
  818. $ids[] = $result[1][0];
  819. $array = $redis->xClaim('mystream',$groupName,'abc',2,$ids);
  820. $this->assertIsArray($array);
  821. }
  822. /**
  823. * 基数统计 测试
  824. * testHyperLog
  825. * @author Tioncico
  826. * Time: 14:59
  827. */
  828. function testHyperLog()
  829. {
  830. $redis = $this->redis;
  831. $key = [
  832. 'hp1',
  833. 'hp2',
  834. 'hp3',
  835. 'hp4',
  836. 'hp5',
  837. ];
  838. $redis->del($key[0]);
  839. $redis->del($key[1]);
  840. $data = $redis->pfAdd($key[0], [1, 2, 2, 3, 3]);
  841. $this->assertEquals(1, $data);
  842. $redis->pfAdd($key[1], [1, 2, 2, 3, 3]);
  843. $data = $redis->pfCount([$key[0], $key[1]]);
  844. $this->assertEquals(3, $data);
  845. $data = $redis->pfMerge($key[2], [$key[0], $key[1]]);
  846. $this->assertEquals(1, $data);
  847. }
  848. /**
  849. * 发布订阅测试
  850. * testSubscribe
  851. * @author Tioncico
  852. * Time: 14:59
  853. */
  854. function testSubscribe()
  855. {
  856. // $this->assertEquals(1,1);
  857. go(function () {
  858. $redis = new Redis(new RedisConfig([
  859. 'host' => REDIS_HOST,
  860. 'port' => REDIS_PORT,
  861. 'auth' => REDIS_AUTH
  862. ]));
  863. $redis->pSubscribe(function (Redis $redis, $pattern, $str) {
  864. $redis2 = new Redis(new RedisConfig([
  865. 'host' => REDIS_HOST,
  866. 'port' => REDIS_PORT,
  867. 'auth' => REDIS_AUTH
  868. ]));
  869. var_dump($redis2);
  870. var_dump($redis2->set('a',1));
  871. var_dump($redis2->get('a'));
  872. $this->assertEquals('test', $str);
  873. $data = $redis->unsubscribe('test');
  874. $this->assertTrue(!!$data);
  875. $redis->setSubscribeStop(true);
  876. }, 'test', 'test1', 'test2');
  877. });
  878. go(function () {
  879. $redis = new Redis(new RedisConfig([
  880. 'host' => REDIS_HOST,
  881. 'port' => REDIS_PORT,
  882. 'auth' => REDIS_AUTH
  883. ]));
  884. $redis->subscribe(function (Redis $redis, $pattern, $str) {
  885. $this->assertEquals('test', $str);
  886. $data = $redis->unsubscribe('test');
  887. $this->assertTrue(!!$data);
  888. $redis->setSubscribeStop(true);
  889. }, 'test', 'test1', 'test2');
  890. });
  891. $redis = $this->redis;
  892. $data = $redis->pubSub('CHANNELS');
  893. $this->assertIsArray($data);
  894. Coroutine::sleep(1);
  895. $data = $redis->publish('test2', 'test');
  896. $this->assertGreaterThan(0, $data);
  897. //
  898. $data = $redis->pUnSubscribe('test');
  899. $this->assertTrue(!!$data);
  900. }
  901. /**
  902. * 事务测试
  903. * testTransaction
  904. * @author tioncico
  905. * Time: 下午5:40
  906. */
  907. function testTransaction()
  908. {
  909. $redis = $this->redis;
  910. $data = $redis->multi();
  911. $this->assertTrue($data);
  912. $this->assertEquals(true, $redis->getTransaction()->isTransaction());
  913. $redis->del('ha');
  914. $data = $redis->hset('ha', 'a', 1);
  915. $this->assertEquals('QUEUED', $data);
  916. $data = $redis->hset('ha', 'b', '2');
  917. $this->assertEquals('QUEUED', $data);
  918. $data = $redis->hset('ha', 'c', '3');
  919. $this->assertEquals('QUEUED', $data);
  920. $data = $redis->hGetAll('ha');
  921. $this->assertEquals('QUEUED', $data);
  922. $data = $redis->exec();
  923. $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3], $data[4]);
  924. $this->assertEquals(false, $redis->getTransaction()->isTransaction());
  925. $redis->multi();
  926. $this->assertEquals(true, $redis->getTransaction()->isTransaction());
  927. $data = $redis->discard();
  928. $this->assertEquals(true, $data);
  929. $this->assertEquals(false, $redis->getTransaction()->isTransaction());
  930. $data = $redis->watch('a', 'b', 'c');
  931. $data = $redis->unwatch();
  932. $this->assertEquals(1, $data);
  933. }
  934. /**
  935. * 管道测试
  936. * testTransaction
  937. * @author tioncico
  938. * Time: 下午5:40
  939. */
  940. function testPipe()
  941. {
  942. $this->assertEquals(1, 1);
  943. $redis = $this->redis;
  944. $redis->get('a');
  945. $data = $redis->startPipe();
  946. $this->assertTrue($data);
  947. $this->assertEquals(true, $redis->getPipe()->isStartPipe());
  948. $redis->del('ha');
  949. $data = $redis->hset('ha', "a", "a\r\nb\r\nc");
  950. $this->assertEquals('PIPE', $data);
  951. $data = $redis->hset('ha', 'b', '2');
  952. $this->assertEquals('PIPE', $data);
  953. $data = $redis->hset('ha', 'c', '3');
  954. $this->assertEquals('PIPE', $data);
  955. $data = $redis->hGetAll('ha');
  956. $this->assertEquals('PIPE', $data);
  957. $data = $redis->execPipe();
  958. $this->assertEquals(['a' => "a\r\nb\r\nc", 'b' => 2, 'c' => 3], $data[4]);
  959. $this->assertEquals(false, $redis->getPipe()->isStartPipe());
  960. $redis->startPipe();
  961. $this->assertEquals(true, $redis->getPipe()->isStartPipe());
  962. $data = $redis->set("a", '1');
  963. $this->assertTrue($data);
  964. $this->assertEquals('Set', ($redis->getPipe()->getCommandLog()[0][0]));
  965. $data = $redis->discardPipe();
  966. $this->assertEquals(true, $data);
  967. $this->assertEquals(false, $redis->getPipe()->isStartPipe());
  968. }
  969. /**
  970. * 脚本执行测试
  971. * testScript
  972. * @author tioncico
  973. * Time: 下午5:41
  974. */
  975. function testScript()
  976. {
  977. $this->assertEquals(1, 1);
  978. $redis = $this->redis;
  979. // $data = $redis->eval('s','s','a','1','2','a');
  980. // $this->assertEquals(1,$data);
  981. //
  982. // $data = $redis->evalsha('a','g','g','1','a','a');
  983. // $this->assertEquals(1,$data);
  984. //
  985. // $data = $redis->scriptExists('a','f');
  986. // $this->assertEquals(1,$data);
  987. //
  988. // $data = $redis->scriptFlush();
  989. // $this->assertEquals(1,$data);
  990. //
  991. // $data = $redis->scriptKill();
  992. // $this->assertEquals(1,$data);
  993. //
  994. // $data = $redis->scriptLoad('a');
  995. // $this->assertEquals(1,$data);
  996. }
  997. /**
  998. * 服务器命令测试
  999. * testServer
  1000. * @author tioncico
  1001. * Time: 下午5:41
  1002. */
  1003. function testServer()
  1004. {
  1005. $redis = $this->redis;
  1006. $data = $redis->bgRewriteAof();
  1007. $this->assertEquals('Background append only file rewriting started', $data);
  1008. Coroutine::sleep(1);
  1009. $data = $redis->bgSave();
  1010. $this->assertEquals('Background saving started', $data);
  1011. $data = $redis->clientList();
  1012. $this->assertIsArray($data);
  1013. $data = $redis->clientSetName('test');
  1014. $this->assertTrue($data);
  1015. $data = $redis->clientGetName();
  1016. $this->assertEquals('test', $data);
  1017. $data = $redis->clientPause(1);
  1018. $this->assertEquals(1, $data);
  1019. $data = $redis->command();
  1020. $this->assertIsArray($data);
  1021. $data = $redis->commandCount();
  1022. $this->assertGreaterThan(0, $data);
  1023. $data = $redis->commandGetKeys('MSET', 'a', 'b', 'c', 'd');
  1024. $this->assertEquals(['a', 'c'], $data);
  1025. $data = $redis->time();
  1026. $this->assertIsArray($data);
  1027. $data = $redis->commandInfo('get', 'set');
  1028. $this->assertIsArray($data);
  1029. $data = $redis->configGet('*max-*-entries*');
  1030. $this->assertIsArray($data);
  1031. $data = $redis->configSet('appendonly', 'yes');
  1032. $this->assertTrue($data);
  1033. $data = $redis->configRewrite();
  1034. $this->assertTrue($data);
  1035. $data = $redis->configResetStat();
  1036. $this->assertTrue($data);
  1037. $data = $redis->dBSize();
  1038. $this->assertGreaterThanOrEqual(0, $data);
  1039. $redis->set('a', 1);
  1040. $data = $redis->debugObject('a');
  1041. $this->assertIsString($data);
  1042. $data = $redis->flushAll();
  1043. $this->assertTrue($data);
  1044. $data = $redis->flushDb();
  1045. $this->assertTrue($data);
  1046. $data = $redis->info();
  1047. $this->assertIsArray($data);
  1048. $data = $redis->lastSave();
  1049. $this->assertNotFalse($data);
  1050. go(function () {
  1051. $redis = new Redis(new RedisConfig([
  1052. 'host' => REDIS_HOST,
  1053. 'port' => REDIS_PORT,
  1054. 'auth' => REDIS_AUTH
  1055. ]));
  1056. $redis->monitor(function (Redis $redis, $data) {
  1057. $this->assertIsString($data);
  1058. $redis->set('a', 1);
  1059. $redis->setMonitorStop(true);
  1060. });
  1061. });
  1062. go(function () {
  1063. $redis = new Redis(new RedisConfig([
  1064. 'host' => REDIS_HOST,
  1065. 'port' => REDIS_PORT,
  1066. 'auth' => REDIS_AUTH
  1067. ]));
  1068. Coroutine::sleep(1);
  1069. $redis->set('a', 1);
  1070. });
  1071. $data = $redis->save();
  1072. $this->assertTrue($data);
  1073. $data = $redis->clientList();
  1074. $data = $redis->clientKill($data[0]['addr']);
  1075. $this->assertTrue($data);
  1076. try {
  1077. $redis->slowLog('get', 'a');
  1078. }catch (RedisException $exception){
  1079. $this->assertEquals('ERR value is not an integer or out of range',$exception->getMessage());
  1080. }
  1081. }
  1082. /**
  1083. * geohash测试
  1084. * testGeohash
  1085. * @author tioncico
  1086. * Time: 下午6:11
  1087. */
  1088. function testGeohash()
  1089. {
  1090. $redis = $this->redis;
  1091. $key = 'testGeohash';
  1092. $redis->del($key);
  1093. $data = $redis->geoAdd($key, [
  1094. ['118.6197800000', '24.88849', 'user1',],
  1095. ['118.6197800000', '24.88859', 'user2',],
  1096. ['114.8197800000', '25.88849', 'user3'],
  1097. ['118.8197800000', '22.88849', 'user4'],
  1098. ]);
  1099. $this->assertEquals(4, $data);
  1100. $data = $redis->geoAdd($key, [
  1101. ['118.6197800000', '24.88869', 'user5',],
  1102. ]);
  1103. $data = $redis->geoDist($key, 'user1', 'user2');
  1104. $this->assertGreaterThan(10, $data);
  1105. $data = $redis->geoHash($key, 'user1', 'user2');
  1106. $this->assertIsArray($data);
  1107. $data = $redis->geoPos($key, 'user1', 'user2');
  1108. $this->assertIsArray($data);
  1109. $data = $redis->geoRadius($key, '118.6197800000', '24.88849', 100, 'm', false, false, false, null,'desc');
  1110. $this->assertEquals(['user5','user2', 'user1'], $data);
  1111. $data = $redis->geoRadiusByMember($key, 'user1', 100, 'm', false, false, false, 2,'DESC');
  1112. //限制为2,并且desc
  1113. $this->assertEquals(['user5', 'user2'], $data);
  1114. $data = $redis->geoRadiusByMember($key, 'user1', 100, 'm', false, false, false, null,'asc');
  1115. //不限制并且asc
  1116. $this->assertEquals(['user1', 'user2', 'user5'], $data);
  1117. }
  1118. }