RedisTest.php 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  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\Config\RedisConfig;
  10. use EasySwoole\Redis\Exception\RedisException;
  11. use EasySwoole\Redis\Redis;
  12. use PHPUnit\Framework\TestCase;
  13. use Swoole\Coroutine;
  14. class RedisTest extends TestCase
  15. {
  16. /**
  17. * @var $redis Redis
  18. */
  19. protected $redis;
  20. /**
  21. * @var $redisPHPSerialize Redis
  22. */
  23. protected $redisPHPSerialize;
  24. protected function setUp(): void
  25. {
  26. parent::setUp(); // TODO: Change the autogenerated stub
  27. $this->redis = new Redis(new RedisConfig([
  28. 'host' => REDIS_HOST,
  29. 'port' => REDIS_PORT,
  30. 'auth' => REDIS_AUTH
  31. ]));
  32. $this->redisPHPSerialize = new Redis(new RedisConfig([
  33. 'host' => REDIS_HOST,
  34. 'port' => REDIS_PORT,
  35. 'auth' => REDIS_AUTH,
  36. 'serialize' => RedisConfig::SERIALIZE_PHP
  37. ]));
  38. $this->redis->connect();
  39. $this->redisPHPSerialize->connect();
  40. //测试原始rawCommand
  41. $redis = $this->redis;
  42. $redis->flushAll();
  43. $data = $redis->rawCommand(['set','a','1']);
  44. $this->assertEquals('OK',$data);
  45. $data = $redis->rawCommand(['get','a']);
  46. $this->assertEquals('1',$data);
  47. $redis->del('a');
  48. }
  49. function testConnect()
  50. {
  51. $this->assertTrue($this->redis->connect());
  52. }
  53. public function testAuth()
  54. {
  55. if (!empty(REDIS_AUTH)) {
  56. $this->assertTrue($this->redis->auth(REDIS_AUTH));
  57. }
  58. $this->assertTrue(true);
  59. }
  60. /**
  61. * key值操作测试
  62. * testKey
  63. * @author Tioncico
  64. * Time: 10:02
  65. */
  66. function testKey()
  67. {
  68. $redis = $this->redis;
  69. $key = 'test123213Key';
  70. $redis->select(0);
  71. $redis->flushAll();
  72. $redis->set($key, 123);
  73. $data = $redis->dump($key);
  74. $this->assertTrue(!!$data);
  75. $data = $redis->dump($key . 'x');
  76. $this->assertNull($data);
  77. $data = $this->redis->exists($key);
  78. $this->assertEquals(1, $data);
  79. $data = $this->redis->expire($key, 1);
  80. $this->assertEquals(1, $data);
  81. Coroutine::sleep(2);
  82. $this->assertEquals(0, $this->redis->exists($key));
  83. $redis->set($key, 123);
  84. $data = $this->redis->pExpire($key, 1000);
  85. $this->assertEquals(1, $data);
  86. $this->assertEquals(123, $this->redis->get($key));
  87. Coroutine::sleep(2);
  88. $this->assertEquals(0, $this->redis->pExpire($key));
  89. $redis->expireAt($key, 1 * 100);
  90. Coroutine::sleep(0.1);
  91. $this->assertEquals(0, $this->redis->exists($key));
  92. $redis->set($key, 123);
  93. $data = $redis->keys("{$key}");
  94. $this->assertEquals($key, $data[0]);
  95. $redis->select(1);
  96. $redis->del($key);
  97. $redis->select(0);
  98. $data = $redis->move($key, 1);
  99. $this->assertEquals(1, $data);
  100. $data = $redis->exists($key);
  101. $this->assertEquals(0, $data);
  102. $redis->select(0);
  103. $redis->set($key, 123);
  104. $data = $redis->expire($key, 1);
  105. $this->assertEquals(1, $data);
  106. $data = $redis->persist($key);
  107. $this->assertEquals(1, $data);
  108. $redis->expire($key, 1);
  109. $data = $redis->pTTL($key);
  110. $this->assertLessThanOrEqual(1000, $data);
  111. $data = $redis->ttl($key);
  112. $this->assertLessThanOrEqual(1, $data);
  113. $data = $redis->randomKey();
  114. $this->assertTrue(!!$data);
  115. $data = $redis->rename($key, $key . 'new');
  116. $this->assertTrue($data);
  117. $redis->set($key.'old',1);
  118. $data = $redis->renameNx($key.'old', $key . 'new');
  119. $this->assertEquals(0, $data);
  120. $redis->renameNx($key . 'new', $key);
  121. $data = $redis->renameNx($key, $key . 'new');
  122. $this->assertEquals(1, $data);
  123. $data = $redis->type($key);
  124. $this->assertEquals('none', $data);
  125. $data = $redis->type($key . 'new');
  126. $this->assertEquals('string', $data);
  127. //del单元测试新增
  128. $keyArr = ['a1','a2','a3','a4'];
  129. $valueArr = ['a1','a2','a3','a4'];
  130. $redis->mSet([
  131. $keyArr[0]=>$valueArr[0],
  132. $keyArr[1]=>$valueArr[1],
  133. $keyArr[2]=>$valueArr[2],
  134. $keyArr[3]=>$valueArr[3],
  135. ]);
  136. $data = $redis->del($keyArr[0]);
  137. $this->assertEquals(1,$data);
  138. $data = $redis->del($keyArr[1],$keyArr[2],$keyArr[3]);
  139. $this->assertEquals(3,$data);
  140. $redis->mSet([
  141. $keyArr[0]=>$valueArr[0],
  142. $keyArr[1]=>$valueArr[1],
  143. $keyArr[2]=>$valueArr[2],
  144. $keyArr[3]=>$valueArr[3],
  145. ]);
  146. $data = $redis->del([$keyArr[1],$keyArr[2],$keyArr[3]]);
  147. $this->assertEquals(3,$data);
  148. //unlink命令新增
  149. $keyArr = ['a1','a2','a3','a4'];
  150. $valueArr = ['a1','a2','a3','a4'];
  151. $redis->mSet([
  152. $keyArr[0]=>$valueArr[0],
  153. $keyArr[1]=>$valueArr[1],
  154. $keyArr[2]=>$valueArr[2],
  155. $keyArr[3]=>$valueArr[3],
  156. ]);
  157. $data = $redis->unlink($keyArr[0]);
  158. $this->assertEquals(1,$data);
  159. $data = $redis->unlink($keyArr[0].'test');
  160. $this->assertEquals(0,$data);
  161. $data = $redis->unlink($keyArr[1],$keyArr[2],$keyArr[3]);
  162. $this->assertEquals(3,$data);
  163. $redis->mSet([
  164. $keyArr[0]=>$valueArr[0],
  165. $keyArr[1]=>$valueArr[1],
  166. $keyArr[2]=>$valueArr[2],
  167. $keyArr[3]=>$valueArr[3],
  168. ]);
  169. $data = $redis->unlink([$keyArr[1],$keyArr[2],$keyArr[3]]);
  170. $this->assertEquals(3,$data);
  171. }
  172. /**
  173. * 字符串单元测试
  174. * testString
  175. * @author tioncico
  176. * Time: 下午9:41
  177. */
  178. function testString()
  179. {
  180. $redis = $this->redis;
  181. $key = 'test';
  182. $value = 1;
  183. $data = $redis->del($key);
  184. $this->assertNotFalse($data);
  185. $data = $redis->set($key, $value);
  186. $this->assertTrue($data);
  187. $redis->set($key, $value);
  188. $data = $redis->get($key);
  189. $this->assertEquals($data, $value);
  190. //set的其他作用测试(超时作用)
  191. $keyTTL=$key.'ttl';
  192. $redis->set($keyTTL,$value,20);
  193. $this->assertGreaterThan(18,$redis->ttl($keyTTL));
  194. $redis->set($keyTTL,$value,['EX'=>20]);
  195. $this->assertGreaterThan(18,$redis->ttl($keyTTL));
  196. $redis->set($keyTTL,$value,['PX'=>20000]);
  197. $this->assertGreaterThan(18000,$redis->pTTL($keyTTL));
  198. $this->assertGreaterThan(18,$redis->ttl($keyTTL));
  199. //set的其他作用测试(存在以及不存在时候设置)
  200. $keyExist = $key.'exist';
  201. $redis->del($keyExist);
  202. $data = $redis->set($keyExist,'1','XX');
  203. $this->assertNull($data);
  204. $data = $redis->set($keyExist,'1','NX');
  205. $this->assertTrue($data);
  206. $this->assertEquals('1',$redis->get($keyExist));
  207. $data = $redis->set($keyExist,'1','NX');
  208. $this->assertNull($data);
  209. $data = $redis->set($keyExist,'2','XX');
  210. $this->assertTrue($data);
  211. $this->assertEquals('2',$redis->get($keyExist));
  212. //set组合测试
  213. $data = $redis->set($keyExist,'3',['XX','EX'=>10]);
  214. $this->assertTrue($data);
  215. $this->assertEquals('3',$redis->get($keyExist));
  216. $this->assertGreaterThan(8,$redis->ttl($keyTTL));
  217. $data = $redis->set($keyExist,'3',['NX','EX'=>10]);
  218. $this->assertNull($data);
  219. $redis->del($keyExist);;
  220. $redis->set($keyExist,'4',['NX','EX'=>20]);
  221. $this->assertEquals('4',$redis->get($keyExist));
  222. $this->assertGreaterThan(18,$redis->ttl($keyTTL));
  223. $data = $redis->exists($key);
  224. $this->assertEquals(1, $data);
  225. $data = $redis->set($key, $value);
  226. $this->assertTrue($data);
  227. $value += 1;
  228. $data = $redis->incr($key);
  229. $this->assertEquals($value, $data);
  230. $value += 10;
  231. $data = $redis->incrBy($key, 10);
  232. $this->assertEquals($value, $data);
  233. $value -= 1;
  234. $data = $redis->decr($key);
  235. $this->assertEquals($value, $data);
  236. $value -= 10;
  237. $data = $redis->decrBy($key, 10);
  238. $this->assertEquals($value, $data);
  239. $key = 'stringTest';
  240. $value = 'tioncico';
  241. $redis->set($key, $value);
  242. $data = $redis->getRange($key, 1, 2);
  243. $this->assertEquals('io', $data);
  244. $data = $redis->getSet($key, $value . 'a');
  245. $this->assertEquals($data, $value);
  246. $redis->set($key, $value);
  247. $bitKey = 'testBit';
  248. $bitValue = 10000;
  249. $redis->set($bitKey, $bitValue);
  250. $data = $redis->setBit($bitKey, 1, 0);
  251. $this->assertEquals(0, $data);
  252. $data = $redis->getBit($key, 1);
  253. $this->assertEquals(1, $data);
  254. $field = [
  255. 'stringField1',
  256. 'stringField2',
  257. 'stringField3',
  258. 'stringField4',
  259. 'stringField5',
  260. ];
  261. $value = [
  262. 1,
  263. 2,
  264. 3,
  265. 4,
  266. 5,
  267. ];
  268. $data = $redis->mSet([
  269. "{$field[0]}" => $value[0],
  270. "{$field[1]}" => $value[1],
  271. "{$field[2]}" => $value[2],
  272. "{$field[3]}" => $value[3],
  273. "{$field[4]}" => $value[4],
  274. ]);
  275. $this->assertTrue($data);
  276. $data = $redis->mGet([$field[3], $field[2], $field[1]]);
  277. $this->assertEquals([$value[3], $value[2], $value[1]], $data);
  278. $data = $redis->setEx($key, 2, $value[0] . $value[0]);
  279. $this->assertTrue($data);
  280. $this->assertEquals($value[0] . $value[0], $redis->get($key));
  281. $data = $redis->pSetEx($key, 2, $value[0]);
  282. $this->assertTrue($data);
  283. $this->assertEquals($value[0], $redis->get($key));
  284. $redis->del($key);
  285. $data = $redis->setNx($key, 1);
  286. $this->assertEquals(1, $data);
  287. $redis->del($field[0]);
  288. $data = $redis->mSetNx([
  289. "{$field[0]}" => $value[0],
  290. "{$field[1]}" => $value[1],
  291. ]);
  292. $this->assertEquals(0, $data);
  293. $this->assertEquals($value[1], $redis->get($field[1]));
  294. $redis->del($field[1]);
  295. $data = $redis->mSetNx([
  296. "{$field[0]}" => $value[0] + 1,
  297. "{$field[1]}" => $value[1] + 1,
  298. ]);
  299. $this->assertEquals(1, $data);
  300. $this->assertEquals($value[0] + 1, $redis->get($field[0]));
  301. $data = $redis->setRange($field[0], 1, 1);
  302. $this->assertEquals(2, $data);
  303. $this->assertEquals('2' . $value[0], $redis->get($field[0]));
  304. $data = $redis->strLen($field[0]);
  305. $this->assertEquals(2, $data);
  306. $redis->set($key, 1);
  307. $data = $redis->incrByFloat($key, 0.1);
  308. $this->assertEquals(1.1, $data);
  309. $data = $redis->appEnd($field[0], '1');
  310. $this->assertEquals($redis->strLen($field[0]), $data);
  311. $this->assertEquals('2' . $value[0] . '1', $redis->get($field[0]));
  312. //迭代测试
  313. $cursor = 0;
  314. $redis->flushAll();
  315. $redis->set('xxxa', '仙士可');
  316. $redis->set('xxxb', '仙士可');
  317. $redis->set('xxxc', '仙士可');
  318. $redis->set('xxxd', '仙士可');
  319. $data = [];
  320. do {
  321. $keys = $redis->scan($cursor, 'xxx*', 1);
  322. $data = array_merge($data, $keys);
  323. } while ($cursor);
  324. $this->assertEquals(4, count($data));
  325. }
  326. /**
  327. * 序列化字符串单元测试
  328. * testString
  329. * @author tioncico
  330. * Time: 下午9:41
  331. */
  332. function testStringSerialize()
  333. {
  334. $redis = $this->redisPHPSerialize;
  335. $key = 'test';
  336. $value = 1;
  337. $data = $redis->del($key);
  338. $this->assertNotFalse($data);
  339. $data = $redis->set($key, $value);
  340. $this->assertTrue($data);
  341. $redis->set($key, $value);
  342. $data = $redis->get($key);
  343. $this->assertEquals($data, $value);
  344. //set的其他作用测试(超时作用)
  345. $keyTTL=$key.'ttl';
  346. $redis->set($keyTTL,$value,20);
  347. $this->assertGreaterThan(18,$redis->ttl($keyTTL));
  348. $redis->set($keyTTL,$value,['EX'=>20]);
  349. $this->assertGreaterThan(18,$redis->ttl($keyTTL));
  350. $redis->set($keyTTL,$value,['PX'=>20000]);
  351. $this->assertGreaterThan(18000,$redis->pTTL($keyTTL));
  352. $this->assertGreaterThan(18,$redis->ttl($keyTTL));
  353. //set的其他作用测试(存在以及不存在时候设置)
  354. $keyExist = $key.'exist';
  355. $redis->del($keyExist);
  356. $data = $redis->set($keyExist,'1','XX');
  357. $this->assertNull($data);
  358. $data = $redis->set($keyExist,'1','NX');
  359. $this->assertTrue($data);
  360. $this->assertEquals('1',$redis->get($keyExist));
  361. $data = $redis->set($keyExist,'1','NX');
  362. $this->assertNull($data);
  363. $data = $redis->set($keyExist,'2','XX');
  364. $this->assertTrue($data);
  365. $this->assertEquals('2',$redis->get($keyExist));
  366. //set组合测试
  367. $data = $redis->set($keyExist,'3',['XX','EX'=>10]);
  368. $this->assertTrue($data);
  369. $this->assertEquals('3',$redis->get($keyExist));
  370. $this->assertGreaterThan(8,$redis->ttl($keyTTL));
  371. $data = $redis->set($keyExist,'3',['NX','EX'=>10]);
  372. $this->assertNull($data);
  373. $redis->del($keyExist);;
  374. $redis->set($keyExist,'4',['NX','EX'=>20]);
  375. $this->assertEquals('4',$redis->get($keyExist));
  376. $this->assertGreaterThan(18,$redis->ttl($keyTTL));
  377. $data = $redis->get($key);
  378. $this->assertEquals($data, $value);
  379. $data = $redis->exists($key);
  380. $this->assertEquals(1, $data);
  381. $key = 'stringTest';
  382. $value = 'tioncico';
  383. $redis->set($key, $value);
  384. $data = $redis->getSet($key, $value . 'a');
  385. $this->assertEquals($data, $value);
  386. $redis->set($key, $value);
  387. $field = [
  388. 'stringField1',
  389. 'stringField2',
  390. 'stringField3',
  391. 'stringField4',
  392. 'stringField5',
  393. ];
  394. $value = [
  395. 1,
  396. 2,
  397. 3,
  398. 4,
  399. 5,
  400. ];
  401. $data = $redis->mSet([
  402. "{$field[0]}" => $value[0],
  403. "{$field[1]}" => $value[1],
  404. "{$field[2]}" => $value[2],
  405. "{$field[3]}" => $value[3],
  406. "{$field[4]}" => $value[4],
  407. ]);
  408. $this->assertTrue($data);
  409. $data = $redis->mGet([$field[3], $field[2], $field[1]]);
  410. $this->assertEquals([$value[3], $value[2], $value[1]], $data);
  411. $data = $redis->setEx($key, 2, $value[0] . $value[0]);
  412. $this->assertTrue($data);
  413. $this->assertEquals($value[0] . $value[0], $redis->get($key));
  414. $data = $redis->pSetEx($key, 2, $value[0]);
  415. $this->assertTrue($data);
  416. $this->assertEquals($value[0], $redis->get($key));
  417. $redis->del($key);
  418. $data = $redis->setNx($key, 1);
  419. $this->assertEquals(1, $data);
  420. $this->assertEquals(1, $redis->get($key));
  421. $redis->del($field[0]);
  422. $data = $redis->mSetNx([
  423. "{$field[0]}" => $value[0],
  424. "{$field[1]}" => $value[1],
  425. ]);
  426. $this->assertEquals(0, $data);
  427. $this->assertEquals($value[1], $redis->get($field[1]));
  428. $redis->del($field[1]);
  429. $data = $redis->mSetNx([
  430. "{$field[0]}" => $value[0] + 1,
  431. "{$field[1]}" => $value[1] + 1,
  432. ]);
  433. $this->assertEquals(1, $data);
  434. $this->assertEquals($value[0] + 1, $redis->get($field[0]));
  435. //迭代测试
  436. $cursor = 0;
  437. $redis->flushAll();
  438. $redis->set('xxxa', '仙士可');
  439. $redis->set('xxxb', '仙士可');
  440. $redis->set('xxxc', '仙士可');
  441. $redis->set('xxxd', '仙士可');
  442. $data = [];
  443. do {
  444. $keys = $redis->scan($cursor, 'xxx*', 1);
  445. $data = array_merge($data, $keys);
  446. } while ($cursor);
  447. $this->assertEquals(4, count($data));
  448. $this->assertTrue(in_array('xxxa', $data));
  449. }
  450. /**
  451. * testHash
  452. * @author Tioncico
  453. * Time: 11:54
  454. */
  455. function testHash()
  456. {
  457. $key = 'hKey';
  458. $field = [
  459. 'hField1',
  460. 'hField2',
  461. 'hField3',
  462. 'hField4',
  463. 'hField5',
  464. ];
  465. $value = [
  466. 1,
  467. 2,
  468. 3,
  469. 4,
  470. 5,
  471. ];
  472. $redis = $this->redis;
  473. $redis->del($key);
  474. $data = $redis->hSet($key, $field[0], $value[0]);
  475. $this->assertNotFalse($data);
  476. $data = $redis->hGet($key, $field[0]);
  477. $this->assertEquals($data, $value[0]);
  478. $data = $redis->hExists($key, $field[0]);
  479. $this->assertEquals(1, $data);
  480. $data = $redis->hDel($key, $field[0]);
  481. $this->assertEquals(1, $data, $redis->getErrorMsg());
  482. $data = $redis->hExists($key, $field[0]);
  483. $this->assertEquals(0, $data);
  484. $data = $redis->hMSet($key, [
  485. "{$field[0]}" => $value[0],
  486. "{$field[1]}" => $value[1],
  487. "{$field[2]}" => $value[2],
  488. "{$field[3]}" => $value[3],
  489. "{$field[4]}" => $value[4],
  490. ]);
  491. $this->assertTrue($data);
  492. $data = $redis->hValS($key);
  493. sort($data);
  494. $this->assertEquals($value, $data);
  495. $data = $redis->hGetAll($key);
  496. $keyTmp = array_keys($data);
  497. sort($keyTmp);
  498. $this->assertEquals($field, $keyTmp);
  499. $valueTmp = array_values($data);
  500. sort($valueTmp);
  501. $this->assertEquals($value, $valueTmp);
  502. $this->assertEquals($value, [
  503. $data[$field[0]],
  504. $data[$field[1]],
  505. $data[$field[2]],
  506. $data[$field[3]],
  507. $data[$field[4]],
  508. ]);
  509. $data = $redis->hKeys($key);
  510. sort($data);
  511. $this->assertEquals($field, $data);
  512. $data = $redis->hLen($key);
  513. $this->assertEquals(count($field), $data);
  514. $data = $redis->hMGet($key, [$field[0], $field[1], $field[2]]);
  515. $this->assertEquals([$field[0]=>1, $field[1]=>2, $field[2]=>3], $data);
  516. $data = $redis->hIncrBy($key, $field[4], 1);
  517. $this->assertEquals($value[4] + 1, $data);
  518. $data = $redis->hIncrByFloat($key, $field[1], 1.1);
  519. $this->assertEquals($value[1] + 1.1, $data);
  520. $data = $redis->hSetNx($key, $field[0], 1);
  521. $this->assertEquals(0, $data);
  522. $data = $redis->hSetNx($key, $field[0] . 'a', 1);
  523. $this->assertEquals(1, $data);
  524. $this->assertEquals(1, $redis->hGet($key, $field[0] . 'a'));
  525. $cursor = 0;
  526. $redis->del('a');
  527. $redis->hMSet('a', [
  528. 'a' => 'tioncico',
  529. 'b' => 'tioncico',
  530. 'c' => 'tioncico',
  531. 'd' => 'tioncico',
  532. 'e' => 'tioncico',
  533. 'f' => 'tioncico',
  534. 'g' => 'tioncico',
  535. 'h' => 'tioncico',
  536. ]);
  537. $data = [];
  538. do {
  539. $keys = $redis->hScan('a', $cursor);
  540. $data = array_merge($data, $keys);
  541. } while ($cursor);
  542. $this->assertEquals(8, count($data));
  543. }
  544. /**
  545. * testHash序列化测试
  546. * @author Tioncico
  547. * Time: 11:54
  548. */
  549. function testHashSerialize()
  550. {
  551. $key = 'hKey';
  552. $field = [
  553. 'hField1',
  554. 'hField2',
  555. 'hField3',
  556. 'hField4',
  557. 'hField5',
  558. ];
  559. $value = [
  560. 1,
  561. 2,
  562. 3,
  563. 4,
  564. 5,
  565. ];
  566. $redis = $this->redisPHPSerialize;
  567. $redis->del($key);
  568. $data = $redis->hSet($key, $field[0], $value[0]);
  569. $this->assertNotFalse($data);
  570. $data = $redis->hGet($key, $field[0]);
  571. $this->assertEquals($data, $value[0]);
  572. $data = $redis->hExists($key, $field[0]);
  573. $this->assertEquals(1, $data);
  574. $data = $redis->hDel($key, $field[0]);
  575. $this->assertEquals(1, $data, $redis->getErrorMsg());
  576. $data = $redis->hExists($key, $field[0]);
  577. $this->assertEquals(0, $data);
  578. $data = $redis->hMSet($key, [
  579. "{$field[0]}" => $value[0],
  580. "{$field[1]}" => $value[1],
  581. "{$field[2]}" => $value[2],
  582. "{$field[3]}" => $value[3],
  583. "{$field[4]}" => $value[4],
  584. ]);
  585. $this->assertTrue($data);
  586. $data = $redis->hValS($key);
  587. sort($data);
  588. $this->assertEquals($value, $data);
  589. $data = $redis->hGetAll($key);
  590. $keyTmp = array_keys($data);
  591. sort($keyTmp);
  592. $this->assertEquals($field, $keyTmp);
  593. $valueTmp = array_values($data);
  594. sort($valueTmp);
  595. $this->assertEquals($value, $valueTmp);
  596. $this->assertEquals($value, [
  597. $data[$field[0]],
  598. $data[$field[1]],
  599. $data[$field[2]],
  600. $data[$field[3]],
  601. $data[$field[4]],
  602. ]);
  603. $data = $redis->hKeys($key);
  604. sort($data);
  605. $this->assertEquals($field, $data);
  606. $data = $redis->hLen($key);
  607. $this->assertEquals(count($field), $data);
  608. $data = $redis->hMGet($key, [$field[0], $field[1], $field[2]]);
  609. $this->assertEquals([$field[0]=>1, $field[1]=>2, $field[2]=>3], $data);
  610. $data = $redis->hSetNx($key, $field[0], 1);
  611. $this->assertEquals(0, $data);
  612. $data = $redis->hSetNx($key, $field[0] . 'a', 1);
  613. $this->assertEquals(1, $data);
  614. $this->assertEquals(1, $redis->hGet($key, $field[0] . 'a'));
  615. $cursor = 0;
  616. $redis->del('a');
  617. $redis->hMSet('a', [
  618. 'a' => 'tioncico',
  619. 'b' => 'tioncico',
  620. 'c' => 'tioncico',
  621. 'd' => 'tioncico',
  622. 'e' => 'tioncico',
  623. 'f' => 'tioncico',
  624. 'g' => 'tioncico',
  625. 'h' => 'tioncico',
  626. ]);
  627. $data = [];
  628. do {
  629. $keys = $redis->hScan('a', $cursor);
  630. $data = array_merge($data, $keys);
  631. } while ($cursor);
  632. $this->assertEquals(8, count($data));
  633. $this->assertEquals('tioncico', $data['a']);
  634. }
  635. /**
  636. * testList
  637. * @author tioncico
  638. * Time: 下午8:17
  639. */
  640. function testList()
  641. {
  642. $redis = $this->redis;
  643. $key = [
  644. 'listKey1',
  645. 'listKey2',
  646. 'listKey3',
  647. ];
  648. $value = [
  649. 'a', 'b', 'c', 'd'
  650. ];
  651. $redis->flushAll();
  652. //测试null的时候
  653. $data = $redis->bLPop([$key[0], $key[1]], 1);
  654. $this->assertNull($data);
  655. $data = $redis->lPush($key[0], $value[0], $value[1]);
  656. $this->assertEquals(2, $data);
  657. //测试null的时候
  658. $data = $redis->bLPop([$key[1]], 1);
  659. $this->assertNull($data);
  660. $data = $redis->bRPop([$key[1]], 1);
  661. $this->assertNull($data);
  662. $data = $redis->bLPop([$key[0], $key[1]], 1);
  663. $this->assertTrue(!!$data);
  664. $data = $redis->bRPop([$key[0], $key[1]], 1);
  665. $this->assertTrue(!!$data);
  666. $redis->del($key[0]);
  667. $redis->lPush($key[0], $value[0], $value[1]);
  668. $data = $redis->bRPopLPush($key[0], $key[1], 1);
  669. $this->assertEquals($value[0], $data);
  670. $redis->del($key[0]);
  671. $redis->lPush($key[0], $value[0], $value[1]);
  672. $data = $redis->rPopLPush($key[0], $key[1]);
  673. $this->assertEquals($value[0], $data);
  674. $redis->del($key[0]);
  675. $redis->lPush($key[0], $value[0], $value[1]);
  676. $data = $redis->lIndex($key[0], 1);
  677. $this->assertEquals($value[0], $data);
  678. $data = $redis->lLen($key[0]);
  679. $this->assertEquals(2, $data);
  680. $data = $redis->lInsert($key[0], true, 'b', 'c');
  681. $this->assertEquals($redis->lLen($key[0]), $data);
  682. $data = $redis->lInsert($key[0], true, 'd', 'c');
  683. $this->assertEquals(-1, $data);
  684. $redis->del($key[1]);
  685. $data = $redis->rPush($key[1], $value[0], $value[2], $value[1]);
  686. $this->assertEquals(3, $data);
  687. $data = $redis->lRange($key[1], 0, 3);
  688. $this->assertEquals(count($data), 3);
  689. $data = $redis->lPop($key[1]);
  690. $this->assertEquals($value[0], $data);
  691. $data = $redis->rPop($key[1]);
  692. $this->assertEquals($value[1], $data);
  693. $data = $redis->lPuShx($key[1], 'x');
  694. $this->assertEquals($redis->lLen($key[1]), $data);
  695. $this->assertEquals('x', $redis->lPop($key[1]));
  696. $data = $redis->rPuShx($key[1], 'z');
  697. $this->assertEquals($redis->lLen($key[1]), $data);
  698. $this->assertEquals('z', $redis->rPop($key[1]));
  699. $redis->del($key[1]);
  700. $redis->rPush($key[1], $value[0], $value[0], $value[0]);
  701. $data = $redis->lRem($key[1], 1, $value[0]);
  702. $this->assertEquals(1, $data);
  703. $data = $redis->lSet($key[1], 0, 'xx');
  704. $this->assertTrue($data);
  705. $this->assertEquals('xx', $redis->lPop($key[1]));
  706. $data = $redis->lTrim($key[1], 0, 2);
  707. $this->assertTrue($data);
  708. $this->assertEquals(1, $redis->lLen($key[1]));
  709. }
  710. /**
  711. * testList序列化测试
  712. * @author tioncico
  713. * Time: 下午8:17
  714. */
  715. function testListSerialize()
  716. {
  717. $redis = $this->redisPHPSerialize;
  718. $key = [
  719. 'listKey1',
  720. 'listKey2',
  721. 'listKey3',
  722. ];
  723. $value = [
  724. 'a', 'b', 'c', 'd'
  725. ];
  726. $redis->flushAll();
  727. //测试null的时候
  728. $data = $redis->bLPop([$key[0], $key[1]], 1);
  729. $this->assertNull($data);
  730. $data = $redis->lPush($key[0], $value[0], $value[1]);
  731. $this->assertEquals(2, $data);
  732. //测试null的时候
  733. $data = $redis->bLPop([$key[1]], 1);
  734. $this->assertNull($data);
  735. $data = $redis->bRPop([$key[1]], 1);
  736. $this->assertNull($data);
  737. $data = $redis->bLPop([$key[0], $key[1]], 1);
  738. $this->assertTrue(!!$data);
  739. $data = $redis->bRPop([$key[0], $key[1]], 1);
  740. $this->assertTrue(!!$data);
  741. $redis->del($key[0]);
  742. $redis->lPush($key[0], $value[0], $value[1]);
  743. $data = $redis->bRPopLPush($key[0], $key[1], 1);
  744. $this->assertEquals($value[0], $data);
  745. $redis->del($key[0]);
  746. $redis->lPush($key[0], $value[0], $value[1]);
  747. $data = $redis->rPopLPush($key[0], $key[1]);
  748. $this->assertEquals($value[0], $data);
  749. $redis->del($key[0]);
  750. $redis->lPush($key[0], $value[0], $value[1]);
  751. $data = $redis->lIndex($key[0], 1);
  752. $this->assertEquals($value[0], $data);
  753. $data = $redis->lLen($key[0]);
  754. $this->assertEquals(2, $data);
  755. $data = $redis->lInsert($key[0], true, 'b', 'c');
  756. $this->assertEquals($redis->lLen($key[0]), $data);
  757. $data = $redis->lInsert($key[0], true, 'd', 'c');
  758. $this->assertEquals(-1, $data);
  759. $redis->del($key[1]);
  760. $data = $redis->rPush($key[1], $value[0], $value[2], $value[1]);
  761. $this->assertEquals(3, $data);
  762. $data = $redis->lRange($key[1], 0, 3);
  763. $this->assertEquals([$value[0], $value[2], $value[1]], $data);
  764. $data = $redis->lPop($key[1]);
  765. $this->assertEquals($value[0], $data);
  766. $data = $redis->rPop($key[1]);
  767. $this->assertEquals($value[1], $data);
  768. $data = $redis->lPuShx($key[1], 'x');
  769. $this->assertEquals($redis->lLen($key[1]), $data);
  770. $this->assertEquals('x', $redis->lPop($key[1]));
  771. $data = $redis->rPuShx($key[1], 'z');
  772. $this->assertEquals($redis->lLen($key[1]), $data);
  773. $this->assertEquals('z', $redis->rPop($key[1]));
  774. $redis->del($key[1]);
  775. $redis->rPush($key[1], $value[0], $value[0], $value[0]);
  776. $data = $redis->lRem($key[1], 1, $value[0]);
  777. $this->assertEquals(1, $data);
  778. $data = $redis->lSet($key[1], 0, 'xx');
  779. $this->assertTrue($data);
  780. $this->assertEquals('xx', $redis->lPop($key[1]));
  781. $data = $redis->lTrim($key[1], 0, 2);
  782. $this->assertTrue($data);
  783. $this->assertEquals(1, $redis->lLen($key[1]));
  784. }
  785. /**
  786. * 集合测试
  787. * testMuster
  788. * @author Tioncico
  789. * Time: 9:10
  790. */
  791. function testMuster()
  792. {
  793. $redis = $this->redis;
  794. $key = [
  795. 'muster1',
  796. 'muster2',
  797. 'muster3',
  798. 'muster4',
  799. 'muster5',
  800. ];
  801. $value = [
  802. '1',
  803. '2',
  804. '3',
  805. '4',
  806. ];
  807. $redis->del($key[0]);
  808. $redis->del($key[1]);
  809. $data = $redis->sAdd($key[0], $value[0], $value[1]);
  810. $this->assertEquals(2, $data);
  811. $data = $redis->sCard($key[0]);
  812. $this->assertEquals(2, $data);
  813. $redis->sAdd($key[1], $value[0], $value[2]);
  814. $data = $redis->sDiff($key[0], $key[1]);
  815. $this->assertEquals([$value[1]], $data);
  816. $data = $redis->sDiff($key[1], $key[0]);
  817. $this->assertEquals([$value[2]], $data);
  818. $data = $redis->sMembers($key[0]);
  819. $this->assertEquals([$value[0], $value[1]], $data);
  820. $data = $redis->sMembers($key[1]);
  821. $this->assertEquals([$value[0], $value[2]], $data);
  822. $data = $redis->sDiffStore($key[2], $key[0], $key[1]);
  823. $this->assertEquals(1, $data);
  824. $data = $redis->sMembers($key[2]);
  825. $this->assertEquals([$value[1]], $data);
  826. $data = $redis->sInter($key[0], $key[1]);
  827. $this->assertEquals([$value[0]], $data);
  828. $data = $redis->sInterStore($key[3], $key[0], $key[1]);
  829. $this->assertEquals(1, $data);
  830. $this->assertEquals([$value[0]], $redis->sMembers($key[3]));
  831. $data = $redis->sIsMember($key[0], $value[0]);
  832. $this->assertEquals(1, $data);
  833. $data = $redis->sIsMember($key[0], $value[3]);
  834. $this->assertEquals(0, $data);
  835. $data = $redis->sMove($key[0], $key[1], $value[1]);
  836. $this->assertEquals(1, $data);
  837. $data = $redis->sPop($key[0]);
  838. $this->assertEquals(1, $data);
  839. $redis->sAdd('myset','one','two','three');
  840. $this->assertCount(2,new \ArrayIterator($redis->sPop('myset',2)));
  841. $this->assertIsString( $redis->sPop('myset'));
  842. $redis->del('myset');
  843. $redis->del($key[3]);
  844. $redis->sAdd($key[3], $value[0], $value[1], $value[2], $value[3]);
  845. $data = $redis->sRandMember($key[3], 4);
  846. $this->assertEquals(4, count($data));
  847. $data = $redis->sRandMember($key[3].rand(1,1111));
  848. $this->assertEquals(null, $data);
  849. $data = $redis->sRandMember($key[3]);
  850. $this->assertTrue(in_array($data,$value));
  851. $data = $redis->sRem($key[3], $value[0], $value[1], $value[2], $value[3]);
  852. $this->assertEquals(4, $data);
  853. $this->assertEquals([], $redis->sMembers($key[3]));
  854. $data = $redis->sUnion($key[0], $key[1]);
  855. $this->assertEquals([$value[0], $value[1], $value[2]], $data);
  856. $redis->del($key[1]);
  857. $redis->del($key[2]);
  858. $redis->del($key[3]);
  859. $redis->del($key[4]);
  860. $redis->sAdd($key[1], 1, 2, 3, 4);
  861. $redis->sAdd($key[2], 5);
  862. $redis->sAdd($key[3], 6, 7);
  863. $data = $redis->sUnIonStore($key[4], $key[1], $key[2], $key[3]);
  864. $this->assertEquals(7, $data);
  865. $cursor = 0;
  866. $redis->del('a');
  867. $redis->sAdd('a', 'a1', 'a2', 'a3', 'a4', 'a5');
  868. $data = [];
  869. do {
  870. $keys = $redis->sScan('a', $cursor, '*', 1);
  871. $data = array_merge($data, $keys);
  872. } while ($cursor);
  873. $this->assertEquals(5, count($data));
  874. }
  875. /**
  876. * 集合序列化测试
  877. * testMuster
  878. * @author Tioncico
  879. * Time: 9:10
  880. */
  881. function testMusterSerialize()
  882. {
  883. $redis = $this->redisPHPSerialize;
  884. $key = [
  885. 'muster1',
  886. 'muster2',
  887. 'muster3',
  888. 'muster4',
  889. 'muster5',
  890. ];
  891. $value = [
  892. '1',
  893. '2',
  894. '3',
  895. '4',
  896. ];
  897. $redis->del($key[0]);
  898. $redis->del($key[1]);
  899. $data = $redis->sAdd($key[0], $value[0], $value[1]);
  900. $this->assertEquals(2, $data);
  901. $data = $redis->sCard($key[0]);
  902. $this->assertEquals(2, $data);
  903. $redis->sAdd($key[1], $value[0], $value[2]);
  904. $data = $redis->sDiff($key[0], $key[1]);
  905. // auth2.0
  906. $this->assertEquals([$value[1]], $data);
  907. $data = $redis->sDiff($key[1], $key[0]);
  908. $this->assertEquals([$value[2]], $data);
  909. $data = $redis->sMembers($key[0]);
  910. sort($data);
  911. $this->assertEquals([$value[0], $value[1]], $data);
  912. $data = $redis->sMembers($key[1]);
  913. $this->assertEquals([$value[2], $value[0]], $data);
  914. $data = $redis->sDiffStore($key[2], $key[0], $key[1]);
  915. $this->assertEquals(1, $data);
  916. $data = $redis->sMembers($key[2]);
  917. $this->assertEquals([$value[1]], $data);
  918. $data = $redis->sInter($key[0], $key[1]);
  919. $this->assertEquals([$value[0]], $data);
  920. $data = $redis->sInterStore($key[3], $key[0], $key[1]);
  921. $this->assertEquals(1, $data);
  922. $this->assertEquals([$value[0]], $redis->sMembers($key[3]));
  923. $data = $redis->sIsMember($key[0], $value[0]);
  924. $this->assertEquals(1, $data);
  925. $data = $redis->sIsMember($key[0], $value[3]);
  926. $this->assertEquals(0, $data);
  927. $data = $redis->sMove($key[0], $key[1], $value[1]);
  928. $this->assertEquals(1, $data);
  929. $data = $redis->sPop($key[0]);
  930. $this->assertEquals(1, $data);
  931. $redis->del($key[3]);
  932. $redis->sAdd($key[3], $value[0], $value[1], $value[2], $value[3]);
  933. $data = $redis->sRandMember($key[3], 4);
  934. $this->assertEquals(4, count($data));
  935. $data = $redis->sRandMember($key[3].rand(1,1111));
  936. $this->assertEquals(null, $data);
  937. $data = $redis->sRandMember($key[3]);
  938. $this->assertTrue(in_array($data,$value));
  939. $data = $redis->sRem($key[3], $value[0], $value[1], $value[2], $value[3]);
  940. $this->assertEquals(4, $data);
  941. $this->assertEquals([], $redis->sMembers($key[3]));
  942. $data = $redis->sUnion($key[0], $key[1]);
  943. $this->assertTrue(!!$data);
  944. $redis->del($key[1]);
  945. $redis->del($key[2]);
  946. $redis->del($key[3]);
  947. $redis->del($key[4]);
  948. $redis->sAdd($key[1], 1, 2, 3, 4);
  949. $redis->sAdd($key[2], 5);
  950. $redis->sAdd($key[3], 6, 7);
  951. $data = $redis->sUnIonStore($key[4], $key[1], $key[2], $key[3]);
  952. $this->assertEquals(7, $data);
  953. $cursor = 0;
  954. $redis->del('a');
  955. $redis->sAdd('a', 'a1', 'a2', 'a3', 'a4', 'a5');
  956. $data = [];
  957. do {
  958. $keys = $redis->sScan('a', $cursor, '*', 1);
  959. $data = array_merge($data, $keys);
  960. } while ($cursor);
  961. $this->assertEquals(5, count($data));
  962. $this->assertTrue(in_array('a1', $data));
  963. }
  964. /**
  965. * 有序集合测试
  966. * testSortMuster
  967. * @author Tioncico
  968. * Time: 14:17
  969. */
  970. function testSortMuster()
  971. {
  972. $redis = $this->redis;
  973. $key = [
  974. 'sortMuster1',
  975. 'sortMuster2',
  976. 'sortMuster3',
  977. 'sortMuster4',
  978. 'sortMuster5',
  979. ];
  980. $member = [
  981. 'member1',
  982. 'member2',
  983. 'member3',
  984. 'member4',
  985. 'member5',
  986. ];
  987. $score = [
  988. 1,
  989. 2,
  990. 3,
  991. 4,
  992. ];
  993. $redis->del($key[0]);
  994. $data = $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  995. $this->assertEquals(2, $data);
  996. $data = $redis->zCard($key[0]);
  997. $this->assertEquals(2, $data);
  998. $data = $redis->zCount($key[0], 0, 3);
  999. $this->assertEquals(2, $data);
  1000. $data = $redis->zInCrBy($key[0], 1, $member[1]);
  1001. $this->assertEquals($score[1] + 1, $data);
  1002. $redis->del($key[0]);
  1003. $redis->del($key[1]);
  1004. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  1005. $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
  1006. $data = $redis->zInTerStore($key[2], [$key[0], $key[1]], [1, 2]);
  1007. $this->assertEquals(1, $data);
  1008. $data = $redis->zLexCount($key[0], '-', '+');
  1009. $this->assertEquals(2, $data);
  1010. $redis->del($key[0]);
  1011. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1012. $data = $redis->zRange($key[0], 0, -1, true);
  1013. $this->assertEquals([
  1014. $member[0] => $score[0],
  1015. $member[1] => $score[1],
  1016. $member[2] => $score[2],
  1017. ], $data);
  1018. $data = $redis->zRange($key[0], 0, -1, false);
  1019. $this->assertEquals([
  1020. $member[0],
  1021. $member[1],
  1022. $member[2],
  1023. ], $data);
  1024. $data = $redis->zRangeByLex($key[0], '-', '+');
  1025. $this->assertEquals(3, count($data));
  1026. $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => true, 'limit' => array(0, 2)]);
  1027. $this->assertEquals([
  1028. $member[1] => $score[1],
  1029. $member[2] => $score[2],
  1030. ], $data);
  1031. $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => false, 'limit' => array(0, 2)]);
  1032. $this->assertEquals([
  1033. $member[1],
  1034. $member[2],
  1035. ], $data);
  1036. $data = $redis->zRank($key[0], $member[1]);
  1037. $this->assertEquals(1, $data);
  1038. $data = $redis->zRem($key[0], $member[1], $member[2]);
  1039. $this->assertEquals(2, $data);
  1040. $redis->del($key[0]);
  1041. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1042. $data = $redis->zRemRangeByLex($key[0], '-', '+');
  1043. $this->assertEquals(3, $data);
  1044. $redis->del($key[0]);
  1045. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1046. $data = $redis->zRemRangeByRank($key[0], 0, 2);
  1047. $this->assertEquals(3, $data);
  1048. $redis->del($key[0]);
  1049. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1050. $data = $redis->zRemRangeByScore($key[0], 0, 3);
  1051. $this->assertEquals(3, $data);
  1052. $redis->del($key[0]);
  1053. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1054. $data = $redis->zRevRange($key[0], 0, 3);
  1055. $this->assertEquals([
  1056. $member[2],
  1057. $member[1],
  1058. $member[0],
  1059. ], $data);
  1060. $redis->del($key[0]);
  1061. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1062. $data = $redis->zRevRange($key[0], 0, 3, true);
  1063. $this->assertEquals([
  1064. $member[2] => $score[2],
  1065. $member[1] => $score[1],
  1066. $member[0] => $score[0],
  1067. ], $data);
  1068. $redis->del($key[0]);
  1069. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1070. $data = $redis->zRevRangeByScore($key[0], 3, 0, ['withScores' => true, 'limit' => array(0, 3)]);
  1071. $this->assertEquals([
  1072. $member[2] => $score[2],
  1073. $member[1] => $score[1],
  1074. $member[0] => $score[0],
  1075. ], $data);
  1076. $redis->del($key[0]);
  1077. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1078. $data = $redis->zReVRangeByScore($key[0], 3, 0, ['withScores' => false, 'limit' => array(0, 3)]);
  1079. $this->assertEquals([
  1080. $member[2],
  1081. $member[1],
  1082. $member[0],
  1083. ], $data);
  1084. $data = $redis->zRevRank($key[0], $member[0]);
  1085. $this->assertEquals(2, $data);
  1086. $data = $redis->zScore($key[0], $member[0]);
  1087. $this->assertEquals($score[0], $data);
  1088. $redis->del($key[0]);
  1089. $redis->del($key[1]);
  1090. $redis->del($key[2]);
  1091. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  1092. $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
  1093. $data = $redis->zUnionStore($key[2], [$key[1], $key[0]]);
  1094. $this->assertEquals(3, $data);
  1095. $cursor = 0;
  1096. $redis->del('a');
  1097. $redis->zAdd('a', 1, 'a1', 2, 'a2', 3, 'a3', 4, 'a4', 5, 'a5');
  1098. $data = [];
  1099. do {
  1100. $keys = $redis->zScan('a', $cursor, '*', 1);
  1101. $data = array_merge($data, $keys);
  1102. } while ($cursor);
  1103. $this->assertEquals(5, count($data));
  1104. $this->redisPHPSerialize->zAdd('myzset',3,'three');
  1105. $this->redisPHPSerialize->zAdd('myzset',2,'two');
  1106. $this->redisPHPSerialize->zAdd('myzset',1,'one');
  1107. $this->assertEquals(['three' => 3, 'two' => 2], $this->redisPHPSerialize->zPopMax('myzset',2));
  1108. $redis->del('myzset');
  1109. $this->redisPHPSerialize->zAdd('myzset',3,'three');
  1110. $this->redisPHPSerialize->zAdd('myzset',2,'two');
  1111. $this->redisPHPSerialize->zAdd('myzset',1,'one');
  1112. $this->assertEquals(['two' => 2, 'one' => 1], $this->redisPHPSerialize->zPopMin('myzset',2));
  1113. $this->assertEquals(['three' => 3], $this->redisPHPSerialize->zPopMin('myzset',2));
  1114. $this->assertEquals([], $this->redisPHPSerialize->zPopMin('myzset',2));
  1115. $redis->del('myzset');
  1116. $this->redisPHPSerialize->zAdd('myzset',3,'three');
  1117. $this->redisPHPSerialize->zAdd('myzset',2,'two');
  1118. $this->redisPHPSerialize->zAdd('myzset',1,'one');
  1119. $this->assertEquals(['myzset','three',3], $this->redisPHPSerialize->bZPopMax('myzset',1));
  1120. $this->assertEquals(['myzset','one',1], $this->redisPHPSerialize->bZPopMin('myzset',1));
  1121. $redis->del('myzset');
  1122. }
  1123. /**
  1124. * 有序集合序列化测试
  1125. * testSortMuster
  1126. * @author Tioncico
  1127. * Time: 14:17
  1128. */
  1129. function testSortMusterSerialize()
  1130. {
  1131. $redis = $this->redisPHPSerialize;
  1132. $key = [
  1133. 'sortMuster1',
  1134. 'sortMuster2',
  1135. 'sortMuster3',
  1136. 'sortMuster4',
  1137. 'sortMuster5',
  1138. ];
  1139. $member = [
  1140. 'member1',
  1141. 'member2',
  1142. 'member3',
  1143. 'member4',
  1144. 'member5',
  1145. ];
  1146. $score = [
  1147. 1,
  1148. 2,
  1149. 3,
  1150. 4,
  1151. ];
  1152. $redis->del($key[0]);
  1153. $data = $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  1154. $this->assertEquals(2, $data);
  1155. $data = $redis->zCard($key[0]);
  1156. $this->assertEquals(2, $data);
  1157. $data = $redis->zCount($key[0], 0, 3);
  1158. $this->assertEquals(2, $data);
  1159. $redis->del($key[0]);
  1160. $redis->del($key[1]);
  1161. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  1162. $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
  1163. $data = $redis->zInTerStore($key[2], [$key[0], $key[1]], [1, 2]);
  1164. $this->assertEquals(1, $data);
  1165. $data = $redis->zLexCount($key[0], '-', '+');
  1166. $this->assertEquals(2, $data);
  1167. $redis->del($key[0]);
  1168. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1169. $data = $redis->zRange($key[0], 0, -1, true);
  1170. $this->assertEquals([
  1171. $member[0] => $score[0],
  1172. $member[1] => $score[1],
  1173. $member[2] => $score[2],
  1174. ], $data);
  1175. $data = $redis->zRange($key[0], 0, -1, false);
  1176. $this->assertEquals([
  1177. $member[0],
  1178. $member[1],
  1179. $member[2],
  1180. ], $data);
  1181. $data = $redis->zRangeByLex($key[0], '-', '+');
  1182. $this->assertEquals(3, count($data));
  1183. $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => true, 'limit' => array(0, 2)]);
  1184. $this->assertEquals([
  1185. $member[1] => $score[1],
  1186. $member[2] => $score[2],
  1187. ], $data);
  1188. $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => false, 'limit' => array(0, 2)]);
  1189. $this->assertEquals([
  1190. $member[1],
  1191. $member[2],
  1192. ], $data);
  1193. $data = $redis->zRank($key[0], $member[1]);
  1194. $this->assertEquals(1, $data);
  1195. $data = $redis->zRem($key[0], $member[1], $member[2]);
  1196. $this->assertEquals(2, $data);
  1197. $redis->del($key[0]);
  1198. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1199. $data = $redis->zRemRangeByLex($key[0], '-', '+');
  1200. $this->assertEquals(3, $data);
  1201. $redis->del($key[0]);
  1202. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1203. $data = $redis->zRemRangeByRank($key[0], 0, 2);
  1204. $this->assertEquals(3, $data);
  1205. $redis->del($key[0]);
  1206. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1207. $data = $redis->zRemRangeByScore($key[0], 0, 3);
  1208. $this->assertEquals(3, $data);
  1209. $redis->del($key[0]);
  1210. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1211. $data = $redis->zRevRange($key[0], 0, 3);
  1212. $this->assertEquals([
  1213. $member[2],
  1214. $member[1],
  1215. $member[0],
  1216. ], $data);
  1217. $redis->del($key[0]);
  1218. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1219. $data = $redis->zRevRange($key[0], 0, 3, true);
  1220. $this->assertEquals([
  1221. $member[2] => $score[2],
  1222. $member[1] => $score[1],
  1223. $member[0] => $score[0],
  1224. ], $data);
  1225. $redis->del($key[0]);
  1226. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1227. $data = $redis->zRevRangeByScore($key[0], 3, 0, ['withScores' => true, 'limit' => array(0, 3)]);
  1228. $this->assertEquals([
  1229. $member[2] => $score[2],
  1230. $member[1] => $score[1],
  1231. $member[0] => $score[0],
  1232. ], $data);
  1233. $redis->del($key[0]);
  1234. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
  1235. $data = $redis->zReVRangeByScore($key[0], 3, 0, ['withScores' => false, 'limit' => array(0, 3)]);
  1236. $this->assertEquals([
  1237. $member[2],
  1238. $member[1],
  1239. $member[0],
  1240. ], $data);
  1241. $data = $redis->zRevRank($key[0], $member[0]);
  1242. $this->assertEquals(2, $data);
  1243. $data = $redis->zScore($key[0], $member[0]);
  1244. $this->assertEquals($score[0], $data);
  1245. $redis->del($key[0]);
  1246. $redis->del($key[1]);
  1247. $redis->del($key[2]);
  1248. $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
  1249. $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
  1250. $data = $redis->zUnionStore($key[2], [$key[1], $key[0]], [1, 2]);
  1251. $this->assertEquals(3, $data);
  1252. $cursor = 0;
  1253. $redis->del('a');
  1254. $redis->zAdd('a', 1, 'a1', 2, 'a2', 3, 'a3', 4, 'a4', 5, 'a5');
  1255. $data = [];
  1256. do {
  1257. $keys = $redis->zScan('a', $cursor, '*', 1);
  1258. $data = array_merge($data, $keys);
  1259. } while ($cursor);
  1260. $this->assertEquals(1, $data['a1']);
  1261. }
  1262. /**
  1263. * Stream 测试
  1264. * @author gaobinzhan <gaobinzhan@gmail.com>
  1265. */
  1266. function tream(){
  1267. $redis = $this->redis;
  1268. $id = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1269. $this->assertIsString($id);
  1270. $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1271. $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1272. $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1273. $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy'], 1);
  1274. $length = $redis->xLen('test');
  1275. $this->assertEquals(1, $length);
  1276. $ids[] = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1277. $ids[] = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1278. $delNum = $redis->xDel('test',$ids);
  1279. $this->assertEquals(2, $delNum);
  1280. $result = $redis->xRange('test','-','+',0);
  1281. $this->assertEmpty($result);
  1282. $result = $redis->xRange('test','-','+');
  1283. $this->assertIsArray($result);
  1284. $result = $redis->xRevRange('test','+','-',0);
  1285. $this->assertEmpty($result);
  1286. $result = $redis->xRevRange('test','+','-');
  1287. $this->assertIsArray($result);
  1288. $redis->del('test1');
  1289. $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
  1290. $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
  1291. $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
  1292. $len = $redis->xTrim('test1',2);
  1293. $this->assertEquals(1, $len);
  1294. $result = $redis->xRead(['test1' => '0-0'],1,0);
  1295. $this->assertIsArray($result);
  1296. $this->assertCount(1,$result['test1']);
  1297. $result = $redis->xRead(['test1' => '0-0'],2,0);
  1298. $this->assertCount(2,$result['test1']);
  1299. $result = $redis->xRead(['test' => '$'],1,1000);
  1300. $this->assertFalse($result);
  1301. go(function (){
  1302. $result = (new Redis(new RedisConfig([
  1303. 'host' => REDIS_HOST,
  1304. 'port' => REDIS_PORT,
  1305. 'auth' => REDIS_AUTH
  1306. ])))->xRead(['test1' => '$'],1,1000);
  1307. $this->assertIsArray($result);
  1308. });
  1309. go(function (){
  1310. (new Redis(new RedisConfig([
  1311. 'host' => REDIS_HOST,
  1312. 'port' => REDIS_PORT,
  1313. 'auth' => REDIS_AUTH
  1314. ])))->xAdd('test2','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1315. });
  1316. $array = $redis->xGroup('HELP');
  1317. $this->assertIsArray($array);
  1318. $groupName = time();
  1319. $bool = $redis->xGroup('CREATE','mystream',$groupName,'$',true);
  1320. $this->assertTrue($bool);
  1321. $bool = $redis->xGroup('SETID','mystream',$groupName,0);
  1322. $this->assertTrue($bool);
  1323. $int = $redis->xGroup('DELCONSUMER','mystream',$groupName,1);
  1324. $this->assertEquals(0,$int);
  1325. $num = $redis->xGroup('DESTROY','mystream',$groupName);
  1326. $this->assertEquals(1,$num);
  1327. $array = $redis->xInfo('STREAM','mystream');
  1328. $this->assertIsArray($array);
  1329. $groupName =time();
  1330. $redis->xGroup('CREATE','mystream',$groupName,'$',true);
  1331. $array = $redis->xInfo('GROUPS','mystream');
  1332. $this->assertIsArray($array);
  1333. $array = $redis->xPending('mystream',$groupName);
  1334. $this->assertIsArray($array);
  1335. $array = $redis->xPending('mystream',$groupName,'-','+',10);
  1336. $this->assertIsArray($array);
  1337. $array = $redis->xPending('mystream',$groupName,'-','+',10,'consumer-123');
  1338. $this->assertIsArray($array);
  1339. $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],1,1000);
  1340. $this->assertFalse($result);
  1341. go(function () use ($groupName){
  1342. $result = (new Redis(new RedisConfig([
  1343. 'host' => REDIS_HOST,
  1344. 'port' => REDIS_PORT,
  1345. 'auth' => REDIS_AUTH
  1346. ])))->xReadGroup($groupName,'consumer-111',['mystream' => '>'],1,1000);
  1347. $this->assertIsArray($result);
  1348. });
  1349. go(function (){
  1350. (new Redis(new RedisConfig([
  1351. 'host' => REDIS_HOST,
  1352. 'port' => REDIS_PORT,
  1353. 'auth' => REDIS_AUTH
  1354. ])))->xAdd('mystream','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
  1355. });
  1356. $redis->xAdd('mystream','*',['name' => 1]);
  1357. $num = $redis->xAck('mystream',$groupName,[0]);
  1358. $this->assertEquals(0,$num);
  1359. $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],1,1000);
  1360. $num = $redis->xAck('mystream',$groupName,array_keys($result['mystream']));
  1361. $this->assertEquals(1,$num);
  1362. $redis->xAdd('mystream','*',['name' => 1]);
  1363. $redis->xAdd('mystream','*',['name' => 1]);
  1364. $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],2,0);
  1365. $num = $redis->xAck('mystream',$groupName,array_keys($result['mystream']));
  1366. $this->assertEquals(2,$num);
  1367. $redis->xAdd('mystream','*',['name' => 1]);
  1368. $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],2,0);
  1369. $result = $redis->xPending('mystream',$groupName,'-','+',2);
  1370. $ids[] = $result[0][0];
  1371. $ids[] = $result[1][0];
  1372. $array = $redis->xClaim('mystream',$groupName,'abc',2,$ids);
  1373. $this->assertIsArray($array);
  1374. }
  1375. /**
  1376. * 基数统计 测试
  1377. * testHyperLog
  1378. * @author Tioncico
  1379. * Time: 14:59
  1380. */
  1381. function testHyperLog()
  1382. {
  1383. $redis = $this->redis;
  1384. $key = [
  1385. 'hp1',
  1386. 'hp2',
  1387. 'hp3',
  1388. 'hp4',
  1389. 'hp5',
  1390. ];
  1391. $redis->del($key[0]);
  1392. $redis->del($key[1]);
  1393. $data = $redis->pfAdd($key[0], [1, 2, 2, 3, 3]);
  1394. $this->assertEquals(1, $data);
  1395. $redis->pfAdd($key[1], [1, 2, 2, 3, 3]);
  1396. $data = $redis->pfCount([$key[0], $key[1]]);
  1397. $this->assertEquals(3, $data);
  1398. $data = $redis->pfMerge($key[2], [$key[0], $key[1]]);
  1399. $this->assertEquals(1, $data);
  1400. }
  1401. /**
  1402. * 发布订阅测试
  1403. * testSubscribe
  1404. * @author Tioncico
  1405. * Time: 14:59
  1406. */
  1407. function testSubscribe()
  1408. {
  1409. // $this->assertEquals(1,1);
  1410. go(function () {
  1411. $redis = new Redis(new RedisConfig([
  1412. 'host' => REDIS_HOST,
  1413. 'port' => REDIS_PORT,
  1414. 'auth' => REDIS_AUTH
  1415. ]));
  1416. $redis->pSubscribe(function (Redis $redis, $pattern, $str) {
  1417. $redis2 = new Redis(new RedisConfig([
  1418. 'host' => REDIS_HOST,
  1419. 'port' => REDIS_PORT,
  1420. 'auth' => REDIS_AUTH
  1421. ]));
  1422. var_dump($redis2);
  1423. var_dump($redis2->set('a',2133));
  1424. var_dump($redis2->get('a'));
  1425. $this->assertEquals('test', $str);
  1426. $data = $redis->unsubscribe('test');
  1427. $this->assertTrue(!!$data);
  1428. $redis->setSubscribeStop(true);
  1429. }, 'test', 'test1', 'test2');
  1430. });
  1431. go(function () {
  1432. $redis = new Redis(new RedisConfig([
  1433. 'host' => REDIS_HOST,
  1434. 'port' => REDIS_PORT,
  1435. 'auth' => REDIS_AUTH
  1436. ]));
  1437. $redis->subscribe(function (Redis $redis, $pattern, $str) {
  1438. $this->assertEquals('test', $str);
  1439. $data = $redis->unsubscribe('test');
  1440. $this->assertTrue(!!$data);
  1441. $redis->setSubscribeStop(true);
  1442. }, 'test', 'test1', 'test2');
  1443. });
  1444. $redis = $this->redis;
  1445. $data = $redis->pubSub('CHANNELS');
  1446. $this->assertIsArray($data);
  1447. Coroutine::sleep(1);
  1448. $data = $redis->publish('test2', 'test');
  1449. $this->assertGreaterThan(0, $data);
  1450. //
  1451. $data = $redis->pUnSubscribe('test');
  1452. $this->assertTrue(!!$data);
  1453. }
  1454. /**
  1455. * 事务测试
  1456. * testTransaction
  1457. * @author tioncico
  1458. * Time: 下午5:40
  1459. */
  1460. function testTransaction()
  1461. {
  1462. $redis = $this->redis;
  1463. $data = $redis->multi();
  1464. $this->assertTrue($data);
  1465. $this->assertEquals(true, $redis->getTransaction()->isTransaction());
  1466. $redis->del('ha');
  1467. $data = $redis->hset('ha', 'a', 1);
  1468. $this->assertEquals('QUEUED', $data);
  1469. $data = $redis->hset('ha', 'b', '2');
  1470. $this->assertEquals('QUEUED', $data);
  1471. $data = $redis->hset('ha', 'c', '3');
  1472. $this->assertEquals('QUEUED', $data);
  1473. $data = $redis->hGetAll('ha');
  1474. $this->assertEquals('QUEUED', $data);
  1475. $data = $redis->exec();
  1476. $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3], $data[4]);
  1477. $this->assertEquals(false, $redis->getTransaction()->isTransaction());
  1478. $redis->multi();
  1479. $this->assertEquals(true, $redis->getTransaction()->isTransaction());
  1480. $data = $redis->discard();
  1481. $this->assertEquals(true, $data);
  1482. $this->assertEquals(false, $redis->getTransaction()->isTransaction());
  1483. $data = $redis->watch('a', 'b', 'c');
  1484. $data = $redis->unwatch();
  1485. $this->assertEquals(1, $data);
  1486. }
  1487. /**
  1488. * 管道测试
  1489. * testTransaction
  1490. * @author tioncico
  1491. * Time: 下午5:40
  1492. */
  1493. function testPipe()
  1494. {
  1495. $this->assertEquals(1, 1);
  1496. $redis = $this->redis;
  1497. $redis->get('a');
  1498. $data = $redis->startPipe();
  1499. $this->assertTrue($data);
  1500. $this->assertEquals(true, $redis->getPipe()->isStartPipe());
  1501. $redis->del('ha');
  1502. $data = $redis->hset('ha', "a", "a\r\nb\r\nc");
  1503. $this->assertEquals('PIPE', $data);
  1504. $data = $redis->hset('ha', 'b', '2');
  1505. $this->assertEquals('PIPE', $data);
  1506. $data = $redis->hset('ha', 'c', '3');
  1507. $this->assertEquals('PIPE', $data);
  1508. $data = $redis->hGetAll('ha');
  1509. $this->assertEquals('PIPE', $data);
  1510. $data = $redis->execPipe();
  1511. $this->assertEquals(['a' => "a\r\nb\r\nc", 'b' => 2, 'c' => 3], $data[4]);
  1512. $this->assertEquals(false, $redis->getPipe()->isStartPipe());
  1513. $redis->startPipe();
  1514. $this->assertEquals(true, $redis->getPipe()->isStartPipe());
  1515. $data = $redis->set("a", '1');
  1516. $this->assertTrue($data);
  1517. $this->assertEquals('Set', ($redis->getPipe()->getCommandLog()[0][0]));
  1518. $data = $redis->discardPipe();
  1519. $this->assertEquals(true, $data);
  1520. $this->assertEquals(false, $redis->getPipe()->isStartPipe());
  1521. }
  1522. /**
  1523. * 脚本执行测试
  1524. * testScript
  1525. * @author tioncico
  1526. * Time: 下午5:41
  1527. */
  1528. function testScript()
  1529. {
  1530. $this->assertEquals(1, 1);
  1531. $redis = $this->redis;
  1532. // $data = $redis->eval('s','s','a','1','2','a');
  1533. // $this->assertEquals(1,$data);
  1534. //
  1535. // $data = $redis->evalsha('a','g','g','1','a','a');
  1536. // $this->assertEquals(1,$data);
  1537. //
  1538. // $data = $redis->scriptExists('a','f');
  1539. // $this->assertEquals(1,$data);
  1540. //
  1541. // $data = $redis->scriptFlush();
  1542. // $this->assertEquals(1,$data);
  1543. //
  1544. // $data = $redis->scriptKill();
  1545. // $this->assertEquals(1,$data);
  1546. //
  1547. // $data = $redis->scriptLoad('a');
  1548. // $this->assertEquals(1,$data);
  1549. }
  1550. /**
  1551. * 服务器命令测试
  1552. * testServer
  1553. * @author tioncico
  1554. * Time: 下午5:41
  1555. */
  1556. function testServer()
  1557. {
  1558. $redis = $this->redis;
  1559. $data = $redis->bgRewriteAof();
  1560. $this->assertEquals('Background append only file rewriting started', $data);
  1561. Coroutine::sleep(1);
  1562. $data = $redis->bgSave();
  1563. $this->assertEquals('Background saving started', $data);
  1564. $data = $redis->clientList();
  1565. $this->assertIsArray($data);
  1566. $data = $redis->clientSetName('test');
  1567. $this->assertTrue($data);
  1568. $data = $redis->clientGetName();
  1569. $this->assertEquals('test', $data);
  1570. $data = $redis->clientPause(1);
  1571. $this->assertEquals(1, $data);
  1572. $data = $redis->command();
  1573. $this->assertIsArray($data);
  1574. $data = $redis->commandCount();
  1575. $this->assertGreaterThan(0, $data);
  1576. $data = $redis->commandGetKeys('MSET', 'a', 'b', 'c', 'd');
  1577. $this->assertEquals(['a', 'c'], $data);
  1578. $data = $redis->time();
  1579. $this->assertIsArray($data);
  1580. $data = $redis->commandInfo('get', 'set');
  1581. $this->assertIsArray($data);
  1582. $data = $redis->configGet('*max-*-entries*');
  1583. $this->assertIsArray($data);
  1584. $data = $redis->configSet('appendonly', 'yes');
  1585. $this->assertTrue($data);
  1586. $data = $redis->configRewrite();
  1587. $this->assertTrue($data);
  1588. $data = $redis->configResetStat();
  1589. $this->assertTrue($data);
  1590. $data = $redis->dBSize();
  1591. $this->assertGreaterThanOrEqual(0, $data);
  1592. $redis->set('a', 1);
  1593. $data = $redis->debugObject('a');
  1594. $this->assertIsString($data);
  1595. $data = $redis->flushAll();
  1596. $this->assertTrue($data);
  1597. $data = $redis->flushDb();
  1598. $this->assertTrue($data);
  1599. $data = $redis->info();
  1600. $this->assertIsArray($data);
  1601. $data = $redis->lastSave();
  1602. $this->assertNotFalse($data);
  1603. go(function () {
  1604. $redis = new Redis(new RedisConfig([
  1605. 'host' => REDIS_HOST,
  1606. 'port' => REDIS_PORT,
  1607. 'auth' => REDIS_AUTH
  1608. ]));
  1609. $redis->monitor(function (Redis $redis, $data) {
  1610. $this->assertIsString($data);
  1611. $redis->set('a', 1);
  1612. $redis->setMonitorStop(true);
  1613. });
  1614. });
  1615. go(function () {
  1616. $redis = new Redis(new RedisConfig([
  1617. 'host' => REDIS_HOST,
  1618. 'port' => REDIS_PORT,
  1619. 'auth' => REDIS_AUTH
  1620. ]));
  1621. Coroutine::sleep(1);
  1622. $redis->set('a', 1);
  1623. });
  1624. $data = $redis->save();
  1625. $this->assertTrue($data);
  1626. $data = $redis->clientList();
  1627. $data = $redis->clientKill($data[0]['addr']);
  1628. $this->assertTrue($data);
  1629. try {
  1630. $redis->slowLog('get', 'a');
  1631. }catch (RedisException $exception){
  1632. $this->assertEquals('ERR value is not an integer or out of range',$exception->getMessage());
  1633. }
  1634. }
  1635. /**
  1636. * geohash测试
  1637. * testGeohash
  1638. * @author tioncico
  1639. * Time: 下午6:11
  1640. */
  1641. function testGeohash()
  1642. {
  1643. $redis = $this->redis;
  1644. $key = 'testGeohash';
  1645. $redis->del($key);
  1646. $data = $redis->geoAdd($key, [
  1647. ['118.6197800000', '24.88849', 'user1',],
  1648. ['118.6197800000', '24.88859', 'user2',],
  1649. ['114.8197800000', '25.88849', 'user3'],
  1650. ['118.8197800000', '22.88849', 'user4'],
  1651. ]);
  1652. $this->assertEquals(4, $data);
  1653. $data = $redis->geoAdd($key, [
  1654. ['118.6197800000', '24.88869', 'user5',],
  1655. ]);
  1656. $data = $redis->geoDist($key, 'user1', 'user2');
  1657. $this->assertGreaterThan(10, $data);
  1658. $data = $redis->geoHash($key, 'user1', 'user2');
  1659. $this->assertIsArray($data);
  1660. $data = $redis->geoPos($key, 'user1', 'user2');
  1661. $this->assertIsArray($data);
  1662. $data = $redis->geoRadius($key, '118.6197800000', '24.88849', 100, 'm', false, false, false, null,'desc');
  1663. $this->assertEquals(['user5','user2', 'user1'], $data);
  1664. $data = $redis->geoRadiusByMember($key, 'user1', 100, 'm', false, false, false, 2,'DESC');
  1665. //限制为2,并且desc
  1666. $this->assertEquals(['user5', 'user2'], $data);
  1667. $data = $redis->geoRadiusByMember($key, 'user1', 100, 'm', false, false, false, null,'asc');
  1668. //不限制并且asc
  1669. $this->assertEquals(['user1', 'user2', 'user5'], $data);
  1670. }
  1671. /**
  1672. * bit map
  1673. */
  1674. public function testBitMap()
  1675. {
  1676. $redis = $this->redis;
  1677. $redis->set('w', 'hello');
  1678. // bitcount
  1679. $result = $redis->bitCount('w');
  1680. $this->assertEquals(21, $result);
  1681. $result = $redis->bitCount('w', 0, 0);
  1682. $this->assertEquals(3, $result);
  1683. $result = $redis->bitCount('w', 0, 1);
  1684. $this->assertEquals(7, $result);
  1685. // bitpos
  1686. $result = $redis->bitPos('w',0);
  1687. $this->assertEquals(0, $result);
  1688. $result = $redis->bitPos('w',1);
  1689. $this->assertEquals(1, $result);
  1690. $result = $redis->bitPos('w',1,1,1);
  1691. $this->assertEquals(9, $result);
  1692. $result = $redis->bitPos('w',1,2,2);
  1693. $this->assertEquals(17, $result);
  1694. // bitop
  1695. $redis->set('key1','foobar');
  1696. $redis->set('key2','abcdef');
  1697. $result = $redis->bitOp('AND','dest','key1','key2');
  1698. $this->assertEquals(6, $result);
  1699. $result = $redis->get('dest');
  1700. $this->assertEquals("`bc`ab", $result);
  1701. $result = $redis->bitOp('OR','dest','key1','key2','w');
  1702. $this->assertEquals(6, $result);
  1703. $result = $redis->get('dest');
  1704. $this->assertEquals("ooonov", $result);
  1705. // bitfield
  1706. $redis->del('mykey');
  1707. $result = $redis->bitField('mykey',['INCRBY','i5',100,1,'GET','u4',0]);
  1708. $this->assertEquals([1,0], $result);
  1709. $result = $redis->bitField('mykey',[['INCRBY','i5',100,1,], ['GET','u4',0]]);
  1710. $this->assertEquals([2,0], $result);
  1711. $redis->del('mystring');
  1712. $result = $redis->bitField('mystring',['SET' ,'i8' ,'#0' ,100 ,'SET' ,'i8' ,'#1' ,200]);
  1713. $this->assertEquals([0,0],$result);
  1714. $result = $redis->bitField('mystring',[['SET' ,'i8' ,'#0' ,100 ] ,['SET' ,'i8' ,'#1' ,200]]);
  1715. $this->assertEquals([100,-56],$result);
  1716. $redis->del('mykey');
  1717. $result = $redis->bitField('mykey',['INCRBY','u2',100,1], 'SAT',['INCRBY','u2',102,1]);
  1718. $this->assertEquals([1,1], $result);
  1719. $result = $redis->bitField('mykey',['INCRBY','u2',100,1], 'SAT',[['INCRBY','u2',102,1]]);
  1720. $this->assertEquals([2,2], $result);
  1721. $result = $redis->bitField('mykey',['INCRBY','u2',100,1], 'SAT',[['INCRBY','u2',102,1]]);
  1722. $this->assertEquals([3,3], $result);
  1723. $result = $redis->bitField('mykey',['INCRBY','u2',100,1], 'SAT',[['INCRBY','u2',102,1]]);
  1724. $this->assertEquals([0,3], $result);
  1725. $result = $redis->bitField('mykey',[],'FAIL',[['incrby','u2',102,1]]);
  1726. $this->assertEquals([NULL], $result);
  1727. }
  1728. }