123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Tioncico
- * Date: 2019/9/24 0024
- * Time: 16:16
- */
- namespace Test;
- use EasySwoole\Redis\Client;
- use EasySwoole\Redis\ClusterClient;
- use EasySwoole\Redis\Config\RedisClusterConfig;
- use EasySwoole\Redis\Config\RedisConfig;
- use EasySwoole\Redis\Exception\RedisClusterException;
- use EasySwoole\Redis\Redis;
- use EasySwoole\Redis\RedisCluster;
- use PHPUnit\Framework\TestCase;
- use Swoole\Coroutine;
- class RedisClusterTest extends TestCase
- {
- /**
- * @var $redis RedisCluster
- */
- protected $redis;
- /**
- * @var $redisPHPSerialize RedisCluster
- */
- protected $redisPHPSerialize;
- protected function setUp():void
- {
- parent::setUp(); // TODO: Change the autogenerated stub
- $this->redis = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
- 'auth' => REDIS_CLUSTER_AUTH,
- ]));
- $this->redisPHPSerialize = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
- 'auth' => REDIS_CLUSTER_AUTH,
- 'serialize' => RedisConfig::SERIALIZE_PHP
- ]));
- $redis = $this->redis;
- $redis->flushAll();
- $data = $redis->rawCommand(['set', 'a', '1']);
- $this->assertEquals('OK', $data);
- $data = $redis->rawCommand(['get', 'a']);
- $this->assertEquals('1', $data);
- $redis->del('a');
- }
- function testConnect()
- {
- $this->assertTrue($this->redis->connect());
- $data = $this->redis->pingAll();
- foreach ($data as $value) {
- $this->assertEquals('PONG', $value);
- }
- }
- public function testAuth()
- {
- if (!empty(REDIS_CLUSTER_AUTH)) {
- $this->assertTrue($this->redis->auth(REDIS_CLUSTER_AUTH));
- }
- $this->assertTrue(true);
- }
- /**
- * 单元测试忽略方法,否则会出错
- * atestCluster
- * @throws \EasySwoole\Redis\Exception\RedisClusterException
- * @author Tioncico
- * Time: 15:20
- */
- function atestCluster()
- {
- $redis = $this->redis;
- $data = $redis->clusterNodes();
- $this->assertIsArray($data);
- $data = $redis->clusterKeySlot('key1');
- $this->assertGreaterThan(0, $data);
- // $data = $redis->clusterDelSlots([1,2,3]);
- // $this->assertTrue($data);
- // $data = $redis->clusterAddSlots([1,3]);
- // $this->assertTrue($data);
- $data = $redis->clusterCountFailureReports(current($redis->getNodeList())['name']);
- $this->assertIsInt($data);
- $data = $redis->clusterCountKeySinSlot(1);
- $this->assertIsInt($data);
- $data = $redis->clusterFailOver('FORCE');
- $this->assertFalse($data);
- $redis->tryConnectServerList();
- $data = $redis->clusterForget(array_column(($redis->getNodeList()), 'name')[0]);
- $this->assertTrue($data);
- $redis->set('a', 1);
- $data = $redis->clusterKeySlot('a');
- $data = $redis->clusterGetKeySinSlot($data, 1);
- $this->assertIsArray($data);
- $data = $redis->clusterInfo();
- $this->assertIsArray($data);
- $data = $redis->clusterKeySlot('b');
- $this->assertIsInt($data);
- $data = $redis->clusterMeet('172.16.253.156', '9005');
- $this->assertTrue($data);
- // $redis->tryConnectServerList();
- // $data = $redis->clusterReplicate(array_column(($redis->getNodeList()),'name')[0]);
- // var_dump($data,$redis->getErrorMsg());return;
- // $data = $redis->clusterReset();
- // var_dump($data,$redis->getErrorMsg());return;
- $data = $redis->clusterSaveConfig();
- $this->assertTrue($data);
- // $data = $redis->clusterSetConfigEpoch('1');
- // var_dump($data,$redis->getErrorMsg());return;
- $redis->tryConnectServerList();
- $data = $redis->clusterSetSlot(1, 'NODE', current($redis->getNodeList())['name']);
- $this->assertTrue($data);
- // $redis->tryConnectServerList();
- // $data = $redis->clusterSlaves(current($redis->getNodeList())['name']);
- // var_dump($data,$redis->getErrorMsg());
- // $this->assertIsArray($data);
- $data = $redis->clusterSlots();
- $this->assertIsArray($data);
- $data = $redis->readonly();
- $this->assertTrue($data);
- $data = $redis->readwrite();
- $this->assertTrue($data);
- }
- /**
- * 集群逻辑方法测试(单元测试忽略方法,否则会出错)
- * testClusterMethod
- * @author Tioncico
- * Time: 17:26
- */
- function atestClusterMethod()
- {
- $redis = $this->redis;
- $redis->tryConnectServerList();
- $client = $redis->getClient();
- $this->assertTrue($client->isConnected());
- $data = ['a' => 1, 'b' => 1, 'c' => 1, 'd' => 1,];
- $redis->mSet($data);
- $defaultClientPort = $redis->getDefaultClient()->getPort();
- $recv = [];
- $defaultKey = '';
- foreach ($data as $k => $va) {
- $client->sendCommand(['get', $k]);
- $response = $client->recv();
- if ($response->getErrorType() == 'MOVED') {
- $recv = $response;
- $defaultKey = $k;
- break;
- }
- }
- $this->assertTrue(!!$recv);
- $data = $redis->getMoveNodeId($recv);
- $this->assertTrue(!!$data);
- $client = $redis->getClient($data);
- //断言这个客户端和之前的不一样
- $this->assertNotEquals($client->getPort(), $defaultClientPort);
- $this->assertTrue($client instanceof ClusterClient);
- //进行一次get 命令的操作,还是默认的客户端
- $redis->get($defaultKey);
- $this->assertEquals($redis->getDefaultClient()->getPort(), $defaultClientPort);
- //重新设置默认客户端
- $redis->setDefaultClient($client);
- $this->assertNotEquals($redis->getDefaultClient()->getPort(), $defaultClientPort);
- $client = $redis->getDefaultClient();
- $client->sendCommand(['get', $defaultKey]);
- $response = $client->recv();
- $this->assertEquals($response, 1);
- // $data = $redis->getSlotNodeId(3300);
- $data = $redis->getNodeClientList();
- $this->assertIsArray($data);
- $data = $redis->getNodeList();
- $this->assertIsArray($data);
- $data = $redis->clientAuth($client, REDIS_CLUSTER_AUTH);
- }
- /**
- * key值操作测试
- * testKey
- * @author Tioncico
- * Time: 10:02
- */
- function testKey()
- {
- $redis = $this->redis;
- $key = 'test123213Key';
- $redis->select(0);
- $redis->set($key, 123);
- $data = $redis->dump($key);
- $this->assertTrue(!!$data);
- $data = $redis->dump($key . 'x');
- $this->assertNull($data);
- $data = $this->redis->exists($key);
- $this->assertEquals(1, $data);
- $data = $this->redis->expire($key, 1);
- $this->assertEquals(1, $data);
- Coroutine::sleep(2);
- $this->assertEquals(0, $this->redis->exists($key));
- $redis->set($key, 123);
- $data = $this->redis->pExpire($key, 1000);
- $this->assertEquals(1, $data);
- $this->assertEquals(123, $this->redis->get($key));
- Coroutine::sleep(2);
- $this->assertEquals(0, $this->redis->pExpire($key));
- $redis->expireAt($key, 1 * 100);
- Coroutine::sleep(0.1);
- $this->assertEquals(0, $this->redis->exists($key));
- $redis->set($key, 123);
- $data = $redis->keys("{$key}");
- $this->assertEquals($key, $data[0]);
- // $redis->select(1);
- $redis->del($key);
- $redis->select(0);
- try {
- $data = $redis->move($key, 1);
- $this->assertEquals(false, $data);
- }catch (RedisClusterException $exception){
- $this->assertEquals('ERR MOVE is not allowed in cluster mode', $redis->getErrorMsg());
- }
- // var_dump($data,$redis->recv());
- $data = $redis->exists($key);
- $this->assertEquals(0, $data);
- $redis->select(0);
- $redis->set($key, 123);
- $data = $redis->expire($key, 1);
- $this->assertEquals(1, $data);
- $data = $redis->persist($key);
- $this->assertEquals(1, $data);
- $redis->expire($key, 1);
- $data = $redis->pTTL($key);
- $this->assertLessThanOrEqual(1000, $data);
- $data = $redis->ttl($key);
- $this->assertLessThanOrEqual(1, $data);
- $data = $redis->randomKey();
- $this->assertTrue(!!$data);
- // $data = $redis->rename($key, $key . 'new');
- // $this->assertFalse($data);
- // $this->assertEquals(1, $redis->expire($key . 'new'));
- // $this->assertEquals(0, $redis->expire($key));
- // $data = $redis->renameNx($key, $key . 'new');
- // var_dump($data,$redis->getErrorMsg());
- // $this->assertEquals(0, $data);
- // $redis->renameNx($key . 'new', $key);
- // $data = $redis->renameNx($key, $key . 'new');
- // $this->assertEquals(1, $data);
- $redis->del($key);
- $data = $redis->type($key);
- $this->assertEquals('none', $data);
- $redis->set($key . 'new', 1);
- $data = $redis->type($key . 'new');
- $this->assertEquals('string', $data);
- //del单元测试新增
- $keyArr = ['a1', 'a2', 'a3', 'a4'];
- $valueArr = ['a1', 'a2', 'a3', 'a4'];
- $redis->mSet([
- $keyArr[0] => $valueArr[0],
- $keyArr[1] => $valueArr[1],
- $keyArr[2] => $valueArr[2],
- $keyArr[3] => $valueArr[3],
- ]);
- $data = $redis->del($keyArr[0]);
- $this->assertEquals(1, $data);
- $data = $redis->del($keyArr[1], $keyArr[2], $keyArr[3]);
- $this->assertEquals(3, $data);
- $redis->mSet([
- $keyArr[0] => $valueArr[0],
- $keyArr[1] => $valueArr[1],
- $keyArr[2] => $valueArr[2],
- $keyArr[3] => $valueArr[3],
- ]);
- $data = $redis->del([$keyArr[1], $keyArr[2], $keyArr[3]]);
- $this->assertEquals(3, $data);
- //unlink命令新增
- $keyArr = ['a1', 'a2', 'a3', 'a4'];
- $valueArr = ['a1', 'a2', 'a3', 'a4'];
- $redis->mSet([
- $keyArr[0] => $valueArr[0],
- $keyArr[1] => $valueArr[1],
- $keyArr[2] => $valueArr[2],
- $keyArr[3] => $valueArr[3],
- ]);
- $data = $redis->unlink($keyArr[0]);
- $this->assertEquals(1, $data);
- $data = $redis->unlink($keyArr[0] . 'test');
- $this->assertEquals(0, $data);
- $data = $redis->unlink($keyArr[1], $keyArr[2], $keyArr[3]);
- $this->assertEquals(3, $data);
- $redis->mSet([
- $keyArr[0] => $valueArr[0],
- $keyArr[1] => $valueArr[1],
- $keyArr[2] => $valueArr[2],
- $keyArr[3] => $valueArr[3],
- ]);
- $data = $redis->unlink([$keyArr[1], $keyArr[2], $keyArr[3]]);
- $this->assertEquals(3, $data);
- }
- /**
- * 字符串单元测试
- * testString
- * @author tioncico
- * Time: 下午9:41
- */
- function testString()
- {
- $redis = $this->redis;
- $key = 'test';
- $value = 1;
- $data = $redis->del($key);
- $this->assertNotFalse($data);
- $data = $redis->set($key, $value);
- $this->assertTrue($data);
- $redis->set($key, $value);
- $data = $redis->get($key);
- $this->assertEquals($data, $value);
- //set的其他作用测试(超时作用)
- $keyTTL = $key . 'ttl';
- $redis->set($keyTTL, $value, 20);
- $this->assertGreaterThan(18, $redis->ttl($keyTTL));
- $redis->set($keyTTL, $value, ['EX' => 20]);
- $this->assertGreaterThan(18, $redis->ttl($keyTTL));
- $redis->set($keyTTL, $value, ['PX' => 20000]);
- $this->assertGreaterThan(18000, $redis->pTTL($keyTTL));
- $this->assertGreaterThan(18, $redis->ttl($keyTTL));
- //set的其他作用测试(存在以及不存在时候设置)
- $keyExist = $key . 'exist';
- $redis->del($keyExist);
- $data = $redis->set($keyExist, '1', 'XX');
- $this->assertNull($data);
- $data = $redis->set($keyExist, '1', 'NX');
- $this->assertTrue($data);
- $this->assertEquals('1', $redis->get($keyExist));
- $data = $redis->set($keyExist, '1', 'NX');
- $this->assertNull($data);
- $data = $redis->set($keyExist, '2', 'XX');
- $this->assertTrue($data);
- $this->assertEquals('2', $redis->get($keyExist));
- //set组合测试
- $data = $redis->set($keyExist, '3', ['XX', 'EX' => 10]);
- $this->assertTrue($data);
- $this->assertEquals('3', $redis->get($keyExist));
- $this->assertGreaterThan(8, $redis->ttl($keyTTL));
- $data = $redis->set($keyExist, '3', ['NX', 'EX' => 10]);
- $this->assertNull($data);
- $redis->del($keyExist);;
- $redis->set($keyExist, '4', ['NX', 'EX' => 20]);
- $this->assertEquals('4', $redis->get($keyExist));
- $this->assertGreaterThan(18, $redis->ttl($keyTTL));
- $data = $redis->get($key);
- $this->assertEquals($data, $value);
- $data = $redis->exists($key);
- $this->assertEquals(1, $data);
- $data = $redis->set($key, $value);
- $this->assertTrue($data);
- $value += 1;
- $data = $redis->incr($key);
- $this->assertEquals($value, $data);
- $value += 10;
- $data = $redis->incrBy($key, 10);
- $this->assertEquals($value, $data);
- $value -= 1;
- $data = $redis->decr($key);
- $this->assertEquals($value, $data);
- $value -= 10;
- $data = $redis->decrBy($key, 10);
- $this->assertEquals($value, $data);
- $key = 'stringTest';
- $value = 'tioncico';
- $redis->set($key, $value);
- $data = $redis->getRange($key, 1, 2);
- $this->assertEquals('io', $data);
- $data = $redis->getSet($key, $value . 'a');
- $this->assertEquals($data, $value);
- $redis->set($key, $value);
- $bitKey = 'testBit';
- $bitValue = 10000;
- $redis->set($bitKey, $bitValue);
- $data = $redis->setBit($bitKey, 1, 0);
- $this->assertEquals(0, $data);
- $data = $redis->getBit($key, 1);
- $this->assertEquals(1, $data);
- $field = [
- 'stringField1',
- 'stringField2',
- 'stringField3',
- 'stringField4',
- 'stringField5',
- ];
- $value = [
- 1,
- 2,
- 3,
- 4,
- 5,
- ];
- $data = $redis->mSet([
- "{$field[0]}" => $value[0],
- "{$field[1]}" => $value[1],
- "{$field[2]}" => $value[2],
- "{$field[3]}" => $value[3],
- "{$field[4]}" => $value[4],
- ]);
- // var_dump($redis->getErrorMsg());
- $this->assertTrue($data);
- $data = $redis->mGet([$field[3], $field[2], $field[1]]);
- $this->assertEquals([$value[3], $value[2], $value[1]], $data);
- $data = $redis->setEx($key, 1, $value[0] . $value[0]);
- $this->assertTrue($data);
- $this->assertEquals($value[0] . $value[0], $redis->get($key));
- $data = $redis->pSetEx($key, 1, $value[0]);
- $this->assertTrue($data);
- $this->assertEquals($value[0], $redis->get($key));
- $redis->del($key);
- $data = $redis->setNx($key, 1);
- $this->assertEquals(1, $data);
- $redis->del($field[0]);
- $data = $redis->mSetNx([
- "{$field[0]}" => $value[0],
- "{$field[1]}" => $value[1],
- ]);
- $this->assertEquals([1, 0], $data);
- $this->assertEquals($value[1], $redis->get($field[1]));
- $redis->del($field[1]);
- $data = $redis->mSetNx([
- "{$field[0]}" => $value[0] + 1,
- "{$field[1]}" => $value[1] + 1,
- ]);
- $this->assertEquals([0, 1], $data);
- $this->assertEquals($value[1] + 1, $redis->get($field[1]));
- $redis->set($field[0], $value[0]);
- $data = $redis->setRange($field[0], 1, 1);
- $this->assertEquals(2, $data);
- $this->assertEquals('1' . $value[0], $redis->get($field[0]));
- $data = $redis->strLen($field[0]);
- $this->assertEquals(2, $data);
- $redis->set($key, 1);
- $data = $redis->incrByFloat($key, 0.1);
- $this->assertEquals(1.1, $data);
- $data = $redis->appEnd($field[0], '1');
- $this->assertEquals($redis->strLen($field[0]), $data);
- $this->assertEquals('1' . $value[0] . '1', $redis->get($field[0]));
- //迭代测试
- // $cursor = 0;
- // $redis->flushAll();
- // $redis->set('xxxa', '仙士可');
- // $redis->set('xxxb', '仙士可');
- // $redis->set('xxxc', '仙士可');
- // $redis->set('xxxd', '仙士可');
- // $data = [];
- // do {
- // $keys = $redis->scan($cursor, 'xxx*', 1);
- // $data = array_merge($data,$keys);
- // } while ($cursor);
- // $this->assertEquals(4,count($data));
- }
- /**
- * 序列化字符串单元测试
- * testString
- * @author tioncico
- * Time: 下午9:41
- */
- function testStringSerialize()
- {
- $redis = $this->redisPHPSerialize;
- $key = 'test';
- $value = 1;
- $data = $redis->del($key);
- $this->assertNotFalse($data);
- $data = $redis->set($key, $value);
- $this->assertTrue($data);
- $redis->set($key, $value);
- $data = $redis->get($key);
- $this->assertEquals($data, $value);
- //set的其他作用测试(超时作用)
- $keyTTL = $key . 'ttl';
- $redis->set($keyTTL, $value, 20);
- $this->assertGreaterThan(18, $redis->ttl($keyTTL));
- $redis->set($keyTTL, $value, ['EX' => 20]);
- $this->assertGreaterThan(18, $redis->ttl($keyTTL));
- $redis->set($keyTTL, $value, ['PX' => 20000]);
- $this->assertGreaterThan(18000, $redis->pTTL($keyTTL));
- $this->assertGreaterThan(18, $redis->ttl($keyTTL));
- //set的其他作用测试(存在以及不存在时候设置)
- $keyExist = $key . 'exist';
- $redis->del($keyExist);
- $data = $redis->set($keyExist, '1', 'XX');
- $this->assertNull($data);
- $data = $redis->set($keyExist, '1', 'NX');
- $this->assertTrue($data);
- $this->assertEquals('1', $redis->get($keyExist));
- $data = $redis->set($keyExist, '1', 'NX');
- $this->assertNull($data);
- $data = $redis->set($keyExist, '2', 'XX');
- $this->assertTrue($data);
- $this->assertEquals('2', $redis->get($keyExist));
- //set组合测试
- $data = $redis->set($keyExist, '3', ['XX', 'EX' => 10]);
- $this->assertTrue($data);
- $this->assertEquals('3', $redis->get($keyExist));
- $this->assertGreaterThan(8, $redis->ttl($keyTTL));
- $data = $redis->set($keyExist, '3', ['NX', 'EX' => 10]);
- $this->assertNull($data);
- $redis->del($keyExist);;
- $redis->set($keyExist, '4', ['NX', 'EX' => 20]);
- $this->assertEquals('4', $redis->get($keyExist));
- $this->assertGreaterThan(18, $redis->ttl($keyTTL));
- $data = $redis->get($key);
- $this->assertEquals($data, $value);
- $data = $redis->exists($key);
- $this->assertEquals(1, $data);
- $key = 'stringTest';
- $value = 'tioncico';
- $redis->set($key, $value);
- $data = $redis->getSet($key, $value . 'a');
- $this->assertEquals($data, $value);
- $redis->set($key, $value);
- $field = [
- 'stringField1',
- 'stringField2',
- 'stringField3',
- 'stringField4',
- 'stringField5',
- ];
- $value = [
- 1,
- 2,
- 3,
- 4,
- 5,
- ];
- $data = $redis->mSet([
- "{$field[0]}" => $value[0],
- "{$field[1]}" => $value[1],
- "{$field[2]}" => $value[2],
- "{$field[3]}" => $value[3],
- "{$field[4]}" => $value[4],
- ]);
- $this->assertTrue($data);
- $data = $redis->mGet([$field[3], $field[2], $field[1]]);
- $this->assertEquals([$value[3], $value[2], $value[1]], $data);
- $data = $redis->setEx($key, 1, $value[0] . $value[0]);
- $this->assertTrue($data);
- $this->assertEquals($value[0] . $value[0], $redis->get($key));
- $data = $redis->pSetEx($key, 1, $value[0]);
- $this->assertTrue($data);
- $this->assertEquals($value[0], $redis->get($key));
- $redis->del($key);
- $data = $redis->setNx($key, 1);
- $this->assertEquals(1, $data);
- $redis->del($field[0]);
- $data = $redis->mSetNx([
- "{$field[0]}" => $value[0],
- "{$field[1]}" => $value[1],
- ]);
- $this->assertEquals([1, 0], $data);
- $this->assertEquals($value[1], $redis->get($field[1]));
- $redis->del($field[1]);
- $data = $redis->mSetNx([
- "{$field[0]}" => $value[0] + 1,
- "{$field[1]}" => $value[1] + 1,
- ]);
- $this->assertEquals([0, 1], $data);
- $this->assertEquals($value[1] + 1, $redis->get($field[1]));
- //迭代测试
- // $cursor = 0;
- // $redis->flushAll();
- // $redis->set('xxxa', '仙士可');
- // $redis->set('xxxb', '仙士可');
- // $redis->set('xxxc', '仙士可');
- // $redis->set('xxxd', '仙士可');
- // $data = [];
- // do {
- // $keys = $redis->scan($cursor, 'xxx*', 1);
- // $data = array_merge($data,$keys);
- // } while ($cursor);
- // $this->assertEquals(4,count($data));
- // $this->assertTrue(in_array('xxxa',$data));
- }
- /**
- * testHash
- * @author Tioncico
- * Time: 11:54
- */
- function testHash()
- {
- $key = 'hKey';
- $field = [
- 'hField1',
- 'hField2',
- 'hField3',
- 'hField4',
- 'hField5',
- ];
- $value = [
- 1,
- 2,
- 3,
- 4,
- 5,
- ];
- $redis = $this->redis;
- $redis->del($key);
- $data = $redis->hSet($key, $field[0], $value[0]);
- $this->assertNotFalse($data);
- $data = $redis->hGet($key, $field[0]);
- $this->assertEquals($data, $value[0]);
- $data = $redis->hExists($key, $field[0]);
- $this->assertEquals(1, $data);
- $data = $redis->hDel($key, $field[0]);
- $this->assertEquals(1, $data, $redis->getErrorMsg());
- $data = $redis->hExists($key, $field[0]);
- $this->assertEquals(0, $data);
- $data = $redis->hMSet($key, [
- "{$field[0]}" => $value[0],
- "{$field[1]}" => $value[1],
- "{$field[2]}" => $value[2],
- "{$field[3]}" => $value[3],
- "{$field[4]}" => $value[4],
- ]);
- $this->assertTrue($data);
- $data = $redis->hValS($key);
- sort($data);
- $this->assertEquals($value, $data);
- $data = $redis->hGetAll($key);
- $keyTmp = array_keys($data);
- sort($keyTmp);
- $this->assertEquals($field, $keyTmp);
- $valueTmp = array_values($data);
- sort($valueTmp);
- $this->assertEquals($value, $valueTmp);
- $this->assertEquals($value, [
- $data[$field[0]],
- $data[$field[1]],
- $data[$field[2]],
- $data[$field[3]],
- $data[$field[4]],
- ]);
- $data = $redis->hKeys($key);
- sort($data);
- $this->assertEquals($field, $data);
- $data = $redis->hLen($key);
- $this->assertEquals(count($field), $data);
- $data = $redis->hMGet($key, [$field[0], $field[1], $field[2]]);
- $this->assertEquals([$field[0]=>1, $field[1]=>2, $field[2]=>3], $data);
- $data = $redis->hIncrBy($key, $field[4], 1);
- $this->assertEquals($value[4] + 1, $data);
- $data = $redis->hIncrByFloat($key, $field[1], 1.1);
- $this->assertEquals($value[1] + 1.1, $data);
- $data = $redis->hSetNx($key, $field[0], 1);
- $this->assertEquals(0, $data);
- $data = $redis->hSetNx($key, $field[0] . 'a', 1);
- $this->assertEquals(1, $data);
- $this->assertEquals(1, $redis->hGet($key, $field[0] . 'a'));
- $cursor = 0;
- $redis->del('a');
- $redis->hMSet('a', [
- 'a' => 'tioncico',
- 'b' => 'tioncico',
- 'c' => 'tioncico',
- 'd' => 'tioncico',
- 'e' => 'tioncico',
- 'f' => 'tioncico',
- 'g' => 'tioncico',
- 'h' => 'tioncico',
- ]);
- $data = [];
- do {
- $keys = $redis->hScan('a', $cursor);
- $data = array_merge($data, $keys);
- } while ($cursor);
- $this->assertEquals(8, count($data));
- }
- /**
- * testHash序列化测试
- * @author Tioncico
- * Time: 11:54
- */
- function testHashSerialize()
- {
- $key = 'hKey';
- $field = [
- 'hField1',
- 'hField2',
- 'hField3',
- 'hField4',
- 'hField5',
- ];
- $value = [
- 1,
- 2,
- 3,
- 4,
- 5,
- ];
- $redis = $this->redisPHPSerialize;
- $redis->del($key);
- $data = $redis->hSet($key, $field[0], $value[0]);
- $this->assertNotFalse($data);
- $data = $redis->hGet($key, $field[0]);
- $this->assertEquals($data, $value[0]);
- $data = $redis->hExists($key, $field[0]);
- $this->assertEquals(1, $data);
- $data = $redis->hDel($key, $field[0]);
- $this->assertEquals(1, $data, $redis->getErrorMsg());
- $data = $redis->hExists($key, $field[0]);
- $this->assertEquals(0, $data);
- $data = $redis->hMSet($key, [
- "{$field[0]}" => $value[0],
- "{$field[1]}" => $value[1],
- "{$field[2]}" => $value[2],
- "{$field[3]}" => $value[3],
- "{$field[4]}" => $value[4],
- ]);
- $this->assertTrue($data);
- $data = $redis->hValS($key);
- sort($data);
- $this->assertEquals($value, $data);
- $data = $redis->hGetAll($key);
- $keyTmp = array_keys($data);
- sort($keyTmp);
- $this->assertEquals($field, $keyTmp);
- $valueTmp = array_values($data);
- sort($valueTmp);
- $this->assertEquals($value, $valueTmp);
- $this->assertEquals($value, [
- $data[$field[0]],
- $data[$field[1]],
- $data[$field[2]],
- $data[$field[3]],
- $data[$field[4]],
- ]);
- $data = $redis->hKeys($key);
- sort($data);
- $this->assertEquals($field, $data);
- $data = $redis->hLen($key);
- $this->assertEquals(count($field), $data);
- $data = $redis->hMGet($key, [$field[0], $field[1], $field[2]]);
- $this->assertEquals([$field[0]=>1, $field[1]=>2, $field[2]=>3], $data);
- $data = $redis->hSetNx($key, $field[0], 1);
- $this->assertEquals(0, $data);
- $data = $redis->hSetNx($key, $field[0] . 'a', 1);
- $this->assertEquals(1, $data);
- $this->assertEquals(1, $redis->hGet($key, $field[0] . 'a'));
- $cursor = 0;
- $redis->del('a');
- $redis->hMSet('a', [
- 'a' => 'tioncico',
- 'b' => 'tioncico',
- 'c' => 'tioncico',
- 'd' => 'tioncico',
- 'e' => 'tioncico',
- 'f' => 'tioncico',
- 'g' => 'tioncico',
- 'h' => 'tioncico',
- ]);
- $data = [];
- do {
- $keys = $redis->hScan('a', $cursor);
- $data = array_merge($data, $keys);
- } while ($cursor);
- $this->assertEquals(8, count($data));
- $this->assertEquals('tioncico', $data['a']);
- }
- /**
- * testList
- * @author tioncico
- * Time: 下午8:17
- */
- function testList()
- {
- $redis = $this->redis;
- $key = [
- 'listKey1',
- 'listKey2',
- 'listKey3',
- ];
- $value = [
- 'a', 'b', 'c', 'd'
- ];
- $redis->del($key[0]);
- $data = $redis->lPush($key[0], $value[0], $value[1]);
- $this->assertEquals(2, $data);
- $data = $redis->bLPop($key[0], 1);
- $this->assertTrue(!!$data);
- $data = $redis->bRPop($key[0], 1);
- $this->assertTrue(!!$data);
- // $redis->del($key[0]);
- // $redis->lPush($key[0], $value[0], $value[1]);
- // $data = $redis->bRPopLPush($key[0], $key[1], 1);
- // var_dump($data,$redis->getErrorMsg());
- // $this->assertEquals($value[0], $data);
- // $redis->del($key[0]);
- // $redis->lPush($key[0], $value[0], $value[1]);
- // $data = $redis->rPopLPush($key[0], $key[1]);
- // $this->assertEquals($value[0], $data);
- $redis->del($key[0]);
- $redis->lPush($key[0], $value[0], $value[1]);
- $data = $redis->lIndex($key[0], 1);
- $this->assertEquals($value[0], $data);
- $data = $redis->lLen($key[0]);
- $this->assertEquals(2, $data);
- $data = $redis->lInsert($key[0], true, 'b', 'c');
- $this->assertEquals($redis->lLen($key[0]), $data);
- $data = $redis->lInsert($key[0], true, 'd', 'c');
- $this->assertEquals(-1, $data);
- $redis->del($key[1]);
- $data = $redis->rPush($key[1], $value[0], $value[2], $value[1]);
- $this->assertEquals(3, $data);
- $data = $redis->lRange($key[1], 0, 3);
- $this->assertEquals(count($data), 3);
- $data = $redis->lPop($key[1]);
- $this->assertEquals($value[0], $data);
- $data = $redis->rPop($key[1]);
- $this->assertEquals($value[1], $data);
- $data = $redis->lPuShx($key[1], 'x');
- $this->assertEquals($redis->lLen($key[1]), $data);
- $this->assertEquals('x', $redis->lPop($key[1]));
- $data = $redis->rPuShx($key[1], 'z');
- $this->assertEquals($redis->lLen($key[1]), $data);
- $this->assertEquals('z', $redis->rPop($key[1]));
- $redis->del($key[1]);
- $redis->rPush($key[1], $value[0], $value[0], $value[0]);
- $data = $redis->lRem($key[1], 1, $value[0]);
- $this->assertEquals(1, $data);
- $data = $redis->lSet($key[1], 0, 'xx');
- $this->assertTrue($data);
- $this->assertEquals('xx', $redis->lPop($key[1]));
- $data = $redis->lTrim($key[1], 0, 2);
- $this->assertTrue($data);
- $this->assertEquals(1, $redis->lLen($key[1]));
- }
- /**
- * testList序列化测试
- * @author tioncico
- * Time: 下午8:17
- */
- function testListSerialize()
- {
- $redis = $this->redisPHPSerialize;
- $key = [
- 'listKey1',
- 'listKey2',
- 'listKey3',
- ];
- $value = [
- 'a', 'b', 'c', 'd'
- ];
- $redis->del($key[0]);
- $data = $redis->lPush($key[0], $value[0], $value[1]);
- $this->assertEquals(2, $data);
- $data = $redis->bLPop($key[0], 1);
- $this->assertTrue(!!$data);
- $data = $redis->bRPop($key[0], 1);
- $this->assertTrue(!!$data);
- // $redis->del($key[0]);
- // $redis->lPush($key[0], $value[0], $value[1]);
- // $data = $redis->bRPopLPush($key[0], $key[1], 1);
- // $this->assertEquals($value[0], $data);
- //
- // $redis->del($key[0]);
- // $redis->lPush($key[0], $value[0], $value[1]);
- // $data = $redis->rPopLPush($key[0], $key[1]);
- // $this->assertEquals($value[0], $data);
- $redis->del($key[0]);
- $redis->lPush($key[0], $value[0], $value[1]);
- $data = $redis->lIndex($key[0], 1);
- $this->assertEquals($value[0], $data);
- $data = $redis->lLen($key[0]);
- $this->assertEquals(2, $data);
- $data = $redis->lInsert($key[0], true, 'b', 'c');
- $this->assertEquals($redis->lLen($key[0]), $data);
- $data = $redis->lInsert($key[0], true, 'd', 'c');
- $this->assertEquals(-1, $data);
- $redis->del($key[1]);
- $data = $redis->rPush($key[1], $value[0], $value[2], $value[1]);
- $this->assertEquals(3, $data);
- $data = $redis->lRange($key[1], 0, 3);
- $this->assertEquals([$value[0], $value[2], $value[1]], $data);
- $data = $redis->lPop($key[1]);
- $this->assertEquals($value[0], $data);
- $data = $redis->rPop($key[1]);
- $this->assertEquals($value[1], $data);
- $data = $redis->lPuShx($key[1], 'x');
- $this->assertEquals($redis->lLen($key[1]), $data);
- $this->assertEquals('x', $redis->lPop($key[1]));
- $data = $redis->rPuShx($key[1], 'z');
- $this->assertEquals($redis->lLen($key[1]), $data);
- $this->assertEquals('z', $redis->rPop($key[1]));
- $redis->del($key[1]);
- $redis->rPush($key[1], $value[0], $value[0], $value[0]);
- $data = $redis->lRem($key[1], 1, $value[0]);
- $this->assertEquals(1, $data);
- $data = $redis->lSet($key[1], 0, 'xx');
- $this->assertTrue($data);
- $this->assertEquals('xx', $redis->lPop($key[1]));
- $data = $redis->lTrim($key[1], 0, 2);
- $this->assertTrue($data);
- $this->assertEquals(1, $redis->lLen($key[1]));
- }
- /**
- * 集合测试
- * testMuster
- * @author Tioncico
- * Time: 9:10
- */
- function testMuster()
- {
- $redis = $this->redis;
- // $server_list = [
- // 'tcp://172.16.253.156:9001',
- // ];
- // $redis = new \RedisCluster(null,$server_list);
- $key = [
- 'muster1',
- 'muster2',
- 'muster3',
- 'muster4',
- 'muster5',
- ];
- $value = [
- '1',
- '2',
- '3',
- '4',
- ];
- $redis->del($key[0]);
- $redis->del($key[1]);
- $data = $redis->sAdd($key[0], $value[0], $value[1]);
- $this->assertEquals(2, $data);
- $data = $redis->sCard($key[0]);
- $this->assertEquals(2, $data);
- $redis->sAdd($key[1], $value[0], $value[2]);
- $data = $redis->sMembers($key[0]);
- $this->assertEquals([$value[0], $value[1]], $data);
- $data = $redis->sMembers($key[1]);
- $this->assertEquals([$value[0], $value[2]], $data);
- $redis->del($key[2]);
- $redis->sAdd($key[2], $value[1]);
- $data = $redis->sMembers($key[2]);
- $this->assertEquals([$value[1]], $data);
- $data = $redis->sIsMember($key[0], $value[0]);
- $this->assertEquals(1, $data);
- $data = $redis->sIsMember($key[0], $value[3]);
- $this->assertEquals(0, $data);
- $redis->del($key[0]);
- $redis->sAdd($key[0], $value[1]);
- $data = $redis->sPop($key[0]);
- $this->assertEquals(2, $data);
- $redis->del($key[3]);
- $redis->sAdd($key[3], $value[0], $value[1], $value[2], $value[3]);
- $data = $redis->sRandMember($key[3], 4);
- $this->assertEquals(4, count($data));
- $data = $redis->sRandMember($key[3] . rand(1, 1111));
- $this->assertEquals(null, $data);
- $data = $redis->sRandMember($key[3]);
- $this->assertTrue(in_array($data, $value));
- $data = $redis->sRem($key[3], $value[0], $value[1], $value[2], $value[3]);
- $this->assertEquals(4, $data);
- $this->assertEquals([], $redis->sMembers($key[3]));
- $cursor = 0;
- $redis->del('a');
- $redis->sAdd('a', 'a1', 'a2', 'a3', 'a4', 'a5');
- $data = [];
- do {
- $keys = $redis->sScan('a', $cursor, '*', 1);
- $data = array_merge($data, $keys);
- } while ($cursor);
- $this->assertEquals(5, count($data));
- }
- /**
- * 集合序列化测试
- * testMuster
- * @author Tioncico
- * Time: 9:10
- */
- function testMusterSerialize()
- {
- $redis = $this->redisPHPSerialize;
- $key = [
- 'muster1',
- 'muster2',
- 'muster3',
- 'muster4',
- 'muster5',
- ];
- $value = [
- '1',
- '2',
- '3',
- '4',
- ];
- // $server_list = [
- // 'tcp://172.16.253.156:9001',
- // ];
- // $redis = new \RedisCluster(null,$server_list);
- $redis->del($key[0]);
- $redis->del($key[1]);
- $data = $redis->sAdd($key[0], $value[0], $value[1]);
- $this->assertEquals(2, $data);
- $data = $redis->sCard($key[0]);
- $this->assertEquals(2, $data);
- $redis->sAdd($key[1], $value[0], $value[2]);
- // $data = $redis->sDiff($key[0], $key[1]);
- // $this->assertEquals([$value[1]], $data);
- // $data = $redis->sDiff($key[1], $key[0]);
- // $this->assertEquals([$value[2]], $data);
- $redis->del($key[1]);
- $redis->sAdd($key[1], $value[0], $value[2]);
- $data = $redis->sMembers($key[1]);
- $this->assertEquals([1,3], $data);
- // $data = $redis->sDiffStore($key[2], $key[0], $key[1]);
- // $this->assertEquals(1, $data);
- $redis->del($key[2]);
- $redis->sAdd($key[2], $value[1]);
- $data = $redis->sMembers($key[2]);
- $this->assertEquals([$value[1]], $data);
- $data = $redis->sMembers($key[2]);
- $this->assertEquals([$value[1]], $data);
- // $data = $redis->sInter($key[0], $key[1]);
- // $this->assertEquals([$value[0]], $data);
- // $data = $redis->sInterStore($key[3], $key[0], $key[1]);
- // $this->assertEquals(1, $data);
- // $this->assertEquals([$value[0]], $redis->sMembers($key[3]));
- $data = $redis->sIsMember($key[0], $value[0]);
- $this->assertEquals(1, $data);
- $data = $redis->sIsMember($key[0], $value[3]);
- $this->assertEquals(0, $data);
- // $data = $redis->sMove($key[0], $key[1], $value[1]);
- // $this->assertEquals(1, $data);
- $redis->del($key[0]);
- $redis->sAdd($key[0], $value[1]);
- $data = $redis->sPop($key[0]);
- $this->assertEquals($value[1], $data);
- $redis->del($key[3]);
- $redis->sAdd($key[3], $value[0], $value[1], $value[2], $value[3]);
- $data = $redis->sRandMember($key[3], 4);
- $this->assertEquals(4, count($data));
- $data = $redis->sRandMember($key[3] . rand(1, 1111));
- $this->assertEquals(null, $data);
- $data = $redis->sRandMember($key[3]);
- $this->assertTrue(in_array($data, $value));
- $data = $redis->sRem($key[3], $value[0], $value[1], $value[2], $value[3]);
- $this->assertEquals(4, $data);
- $this->assertEquals([], $redis->sMembers($key[3]));
- // $data = $redis->sUnion($key[0], $key[1]);
- // $this->assertTrue(!!$data);
- $redis->del($key[1]);
- $redis->del($key[2]);
- $redis->del($key[3]);
- $redis->del($key[4]);
- $redis->sAdd($key[1], 1, 2, 3, 4);
- $redis->sAdd($key[2], 5);
- $redis->sAdd($key[3], 6, 7);
- // $data = $redis->sUnIonStore($key[4], $key[1], $key[2], $key[3]);
- // $this->assertEquals(7, $data);
- $cursor = 0;
- $redis->del('a');
- $redis->sAdd('a', 'a1', 'a2', 'a3', 'a4', 'a5');
- $data = [];
- do {
- $keys = $redis->sScan('a', $cursor, '*', 1);
- $data = array_merge($data, $keys);
- } while ($cursor);
- $this->assertEquals(5, count($data));
- $this->assertTrue(in_array('a1', $data));
- }
- /**
- * 有序集合测试
- * testSortMuster
- * @author Tioncico
- * Time: 14:17
- */
- function testSortMuster()
- {
- $redis = $this->redis;
- // $server_list = [
- // 'tcp://172.16.253.156:9001',
- // ];
- // $redis = new \RedisCluster(null,$server_list);
- $key = [
- 'sortMuster1',
- 'sortMuster2',
- 'sortMuster3',
- 'sortMuster4',
- 'sortMuster5',
- ];
- $member = [
- 'member1',
- 'member2',
- 'member3',
- 'member4',
- 'member5',
- ];
- $score = [
- 1,
- 2,
- 3,
- 4,
- ];
- $redis->del($key[0]);
- $data = $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
- $this->assertEquals(2, $data);
- $data = $redis->zCard($key[0]);
- $this->assertEquals(2, $data);
- $data = $redis->zCount($key[0], 0, 3);
- $this->assertEquals(2, $data);
- $data = $redis->zInCrBy($key[0], 1, $member[1]);
- $this->assertEquals($score[1] + 1, $data);
- // $redis->del($key[0]);
- // $redis->del($key[1]);
- // $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
- // $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
- // $data = $redis->zInTerStore($key[2], [$key[0], $key[1]],[1]);
- // $this->assertEquals(1, $data);
- $data = $redis->zLexCount($key[0], '-', '+');
- $this->assertEquals(2, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRange($key[0], 0, -1, true);
- $this->assertEquals([
- $member[0] => $score[0],
- $member[1] => $score[1],
- $member[2] => $score[2],
- ], $data);
- $data = $redis->zRange($key[0], 0, -1, false);
- $this->assertEquals([
- $member[0],
- $member[1],
- $member[2],
- ], $data);
- $data = $redis->zRangeByLex($key[0], '-', '+');
- $this->assertEquals(3, count($data));
- $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => true, 'limit' => array(0, 2)]);
- $this->assertEquals([
- $member[1] => $score[1],
- $member[2] => $score[2],
- ], $data);
- $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => false, 'limit' => array(0, 2)]);
- $this->assertEquals([
- $member[1],
- $member[2],
- ], $data);
- $data = $redis->zRank($key[0], $member[1]);
- $this->assertEquals(1, $data);
- $data = $redis->zRem($key[0], $member[1], $member[2]);
- $this->assertEquals(2, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRemRangeByLex($key[0], '-', '+');
- $this->assertEquals(3, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRemRangeByRank($key[0], 0, 2);
- $this->assertEquals(3, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRemRangeByScore($key[0], 0, 3);
- $this->assertEquals(3, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRevRange($key[0], 0, 3);
- $this->assertEquals([
- $member[2],
- $member[1],
- $member[0],
- ], $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRevRange($key[0], 0, 3, true);
- $this->assertEquals([
- $member[2] => $score[2],
- $member[1] => $score[1],
- $member[0] => $score[0],
- ], $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRevRangeByScore($key[0], 3, 0, ['withScores' => true, 'limit' => array(0, 3)]);
- $this->assertEquals([
- $member[2] => $score[2],
- $member[1] => $score[1],
- $member[0] => $score[0],
- ], $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zReVRangeByScore($key[0], 3, 0, ['withScores' => false, 'limit' => array(0, 3)]);
- $this->assertEquals([
- $member[2],
- $member[1],
- $member[0],
- ], $data);
- $data = $redis->zRevRank($key[0], $member[0]);
- $this->assertEquals(2, $data);
- $data = $redis->zScore($key[0], $member[0]);
- $this->assertEquals($score[0], $data);
- // $redis->del($key[0]);
- // $redis->del($key[1]);
- // $redis->del($key[2]);
- // $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
- // $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
- // $data = $redis->zUnionStore($key[2], [$key[1], $key[0]],[1,2]);
- // $this->assertEquals(3, $data);
- $cursor = 0;
- $redis->del('a');
- $redis->zAdd('a', 1, 'a1', 2, 'a2', 3, 'a3', 4, 'a4', 5, 'a5');
- $data = [];
- do {
- $keys = $redis->zScan('a', $cursor, '*', 1);
- $data = array_merge($data, $keys);
- } while ($cursor);
- $this->assertEquals(5, count($data));
- }
- /**
- * 有序集合序列化测试
- * testSortMuster
- * @author Tioncico
- * Time: 14:17
- */
- function testSortMusterSerialize()
- {
- $redis = $this->redisPHPSerialize;
- // $server_list = [
- // 'tcp://172.16.253.156:9001',
- // ];
- // $redis = new \RedisCluster(null,$server_list);
- $key = [
- 'sortMuster1',
- 'sortMuster2',
- 'sortMuster3',
- 'sortMuster4',
- 'sortMuster5',
- ];
- $member = [
- 'member1',
- 'member2',
- 'member3',
- 'member4',
- 'member5',
- ];
- $score = [
- 1,
- 2,
- 3,
- 4,
- ];
- $redis->del($key[0]);
- $data = $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
- $this->assertEquals(2, $data);
- $data = $redis->zCard($key[0]);
- $this->assertEquals(2, $data);
- $data = $redis->zCount($key[0], 0, 3);
- $this->assertEquals(2, $data);
- // $redis->del($key[0]);
- // $redis->del($key[1]);
- // $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
- // $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
- // $data = $redis->zInTerStore($key[2], [$key[0], $key[1]]);
- // $this->assertEquals(1, $data);
- $data = $redis->zLexCount($key[0], '-', '+');
- $this->assertEquals(2, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRange($key[0], 0, -1, true);
- $this->assertEquals([
- $member[0] => $score[0],
- $member[1] => $score[1],
- $member[2] => $score[2],
- ], $data);
- $data = $redis->zRange($key[0], 0, -1, false);
- $this->assertEquals([
- $member[0],
- $member[1],
- $member[2],
- ], $data);
- $data = $redis->zRangeByLex($key[0], '-', '+');
- $this->assertEquals(3, count($data));
- $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => true, 'limit' => array(0, 2)]);
- $this->assertEquals([
- $member[1] => $score[1],
- $member[2] => $score[2],
- ], $data);
- $data = $redis->zRangeByScore($key[0], 2, 3, ['withScores' => false, 'limit' => array(0, 2)]);
- $this->assertEquals([
- $member[1],
- $member[2],
- ], $data);
- $data = $redis->zRank($key[0], $member[1]);
- $this->assertEquals(1, $data);
- $data = $redis->zRem($key[0], $member[1], $member[2]);
- $this->assertEquals(2, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRemRangeByLex($key[0], '-', '+');
- $this->assertEquals(3, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRemRangeByRank($key[0], 0, 2);
- $this->assertEquals(3, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRemRangeByScore($key[0], 0, 3);
- $this->assertEquals(3, $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRevRange($key[0], 0, 3);
- $this->assertEquals([
- $member[2],
- $member[1],
- $member[0],
- ], $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRevRange($key[0], 0, 3, true);
- $this->assertEquals([
- $member[2] => $score[2],
- $member[1] => $score[1],
- $member[0] => $score[0],
- ], $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zRevRangeByScore($key[0], 3, 0, ['withScores' => true, 'limit' => array(0, 3)]);
- $this->assertEquals([
- $member[2] => $score[2],
- $member[1] => $score[1],
- $member[0] => $score[0],
- ], $data);
- $redis->del($key[0]);
- $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1], $score[2], $member[2]);
- $data = $redis->zReVRangeByScore($key[0], 3, 0, ['withScores' => false, 'limit' => array(0, 3)]);
- $this->assertEquals([
- $member[2],
- $member[1],
- $member[0],
- ], $data);
- $data = $redis->zRevRank($key[0], $member[0]);
- $this->assertEquals(2, $data);
- $data = $redis->zScore($key[0], $member[0]);
- $this->assertEquals($score[0], $data);
- // $redis->del($key[0]);
- // $redis->del($key[1]);
- // $redis->del($key[2]);
- // $redis->zAdd($key[0], $score[0], $member[0], $score[1], $member[1]);
- // $redis->zAdd($key[1], $score[0], $member[0], $score[3], $member[3]);
- // $data = $redis->zUnionStore($key[2], 2, $key[1], $key[0]);
- // $this->assertEquals(3, $data);
- $cursor = 0;
- $redis->del('a');
- $redis->zAdd('a', 1, 'a1', 2, 'a2', 3, 'a3', 4, 'a4', 5, 'a5');
- $data = [];
- do {
- $keys = $redis->zScan('a', $cursor, '*', 1);
- $data = array_merge($data, $keys);
- } while ($cursor);
- $this->assertEquals(1, $data['a1']);
- }
- /**
- * Stream 测试
- * @author gaobinzhan <gaobinzhan@gmail.com>
- */
- function testStream(){
- $redis = $this->redis;
- $redis->del('test');
- $id = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- $this->assertIsString($id);
- $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy'], 1);
- $length = $redis->xLen('test');
- $this->assertEquals(1, $length);
- $ids[] = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- $ids[] = $redis->xAdd('test','*',['name'=>'gaobinzhan', 'sex'=>'boy']);
- $delNum = $redis->xDel('test',$ids);
- $this->assertEquals(2, $delNum);
- $result = $redis->xRange('test','-','+',0);
- $this->assertEmpty($result);
- $result = $redis->xRange('test','-','+');
- $this->assertIsArray($result);
- $result = $redis->xRevRange('test','+','-',0);
- $this->assertEmpty($result);
- $result = $redis->xRevRange('test','+','-');
- $this->assertIsArray($result);
- $redis->del('test1');
- $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
- $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
- $redis->xAdd('test1','*',['name' => 'gaobinzhan', 'sex' => 'boy']);
- $len = $redis->xTrim('test1',2);
- $this->assertEquals(1, $len);
- $result = $redis->xRead(['test1' => '0-0'],1,0);
- $this->assertIsArray($result);
- $this->assertCount(1,$result['test1']);
- $result = $redis->xRead(['test1' => '0-0'],2,0);
- $this->assertCount(2,$result['test1']);
- $result = $redis->xRead(['test' => '$'],1,1000);
- $this->assertFalse($result);
- $array = $redis->xGroup('HELP');
- $this->assertIsArray($array);
- $groupName = time();
- $bool = $redis->xGroup('CREATE','mystream',$groupName,'$',true);
- $this->assertTrue($bool);
- $bool = $redis->xGroup('SETID','mystream',$groupName,0);
- $this->assertTrue($bool);
- $int = $redis->xGroup('DELCONSUMER','mystream',$groupName,1);
- $this->assertEquals(0,$int);
- $num = $redis->xGroup('DESTROY','mystream',$groupName);
- $this->assertEquals(1,$num);
- $array = $redis->xInfo('STREAM','mystream');
- $this->assertIsArray($array);
- $groupName =time();
- $redis->xGroup('CREATE','mystream',$groupName,'$',true);
- $array = $redis->xInfo('GROUPS','mystream');
- $this->assertIsArray($array);
- $array = $redis->xPending('mystream',$groupName);
- $this->assertIsArray($array);
- $array = $redis->xPending('mystream',$groupName,'-','+',10);
- $this->assertIsArray($array);
- $array = $redis->xPending('mystream',$groupName,'-','+',10,'consumer-123');
- $this->assertIsArray($array);
- $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],1,1000);
- $this->assertFalse($result);
- $redis->xAdd('mystream','*',['name' => 1]);
- $num = $redis->xAck('mystream',$groupName,[0]);
- $this->assertEquals(0,$num);
- $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],1,1000);
- $num = $redis->xAck('mystream',$groupName,array_keys($result['mystream']));
- $this->assertEquals(1,$num);
- $redis->xAdd('mystream','*',['name' => 1]);
- $redis->xAdd('mystream','*',['name' => 1]);
- $result = $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],2,0);
- $num = $redis->xAck('mystream',$groupName,array_keys($result['mystream']));
- $this->assertEquals(2,$num);
- $redis->xAdd('mystream','*',['name' => 1]);
- $redis->xAdd('mystream','*',['name' => 1]);
- $redis->xReadGroup($groupName,'consumer-111',['mystream' => '>'],2,0);
- $result = $redis->xPending('mystream',$groupName,'-','+',2);
- $ids[] = $result[0][0];
- $ids[] = $result[1][0];
- $array = $redis->xClaim('mystream',$groupName,'abc',2,$ids);
- $this->assertIsArray($array);
- }
- /**
- * 基数统计 测试
- * testHyperLog
- * @author Tioncico
- * Time: 14:59
- */
- function testHyperLog()
- {
- $redis = $this->redis;
- // $server_list = [
- // 'tcp://172.16.253.156:9001',
- // ];
- // $redis = new \RedisCluster(null,$server_list);
- $key = [
- 'hp1',
- 'hp2',
- 'hp3',
- 'hp4',
- 'hp5',
- ];
- $redis->del($key[0]);
- $redis->del($key[1]);
- $data = $redis->pfAdd($key[0], [1, 2, 2, 3, 3]);
- $this->assertEquals(1, $data);
- // $redis->pfAdd($key[1], [1, 2, 2, 3, 3]);
- // $data = $redis->pfCount([$key[0], $key[1]]);
- // $this->assertEquals(3, $data);
- // $data = $redis->pfMerge($key[2], [$key[0], $key[1]]);
- // $this->assertEquals(1, $data);
- }
- /**
- * 发布订阅测试
- * testSubscribe
- * @author Tioncico
- * Time: 14:59
- */
- function testSubscribe()
- {
- // $this->assertEquals(1,1);
- go(function () {
- $redis = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
- 'auth' => REDIS_CLUSTER_AUTH,
- ]));
- $redis->pSubscribe(function (RedisCluster $redis, $pattern, $str) {
- $this->assertEquals('test', $str);
- $data = $redis->unsubscribe('test');
- $this->assertTrue(!!$data);
- $redis->setSubscribeStop(true);
- }, 'test', 'test1', 'test2');
- });
- go(function () {
- $redis = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
- 'auth' => REDIS_CLUSTER_AUTH,
- ]));
- $redis->subscribe(function (RedisCluster $redis, $pattern, $str) {
- $this->assertEquals('test', $str);
- $data = $redis->unsubscribe('test');
- $this->assertTrue(!!$data);
- $redis->setSubscribeStop(true);
- }, 'test', 'test1', 'test2');
- });
- $redis = $this->redis;
- $data = $redis->pubSub('CHANNELS');
- $this->assertIsArray($data);
- Coroutine::sleep(1);
- $data = $redis->publish('test2', 'test');
- $this->assertGreaterThan(0, $data);
- //
- $data = $redis->pUnSubscribe('test');
- $this->assertTrue(!!$data);
- }
- /**
- * 脚本执行测试
- * testScript
- * @author tioncico
- * Time: 下午5:41
- */
- function testScript()
- {
- $this->assertEquals(1, 1);
- $redis = $this->redis;
- // $data = $redis->eval('s','s','a','1','2','a');
- // $this->assertEquals(1,$data);
- //
- // $data = $redis->evalsha('a','g','g','1','a','a');
- // $this->assertEquals(1,$data);
- //
- // $data = $redis->scriptExists('a','f');
- // $this->assertEquals(1,$data);
- //
- // $data = $redis->scriptFlush();
- // $this->assertEquals(1,$data);
- //
- // $data = $redis->scriptKill();
- // $this->assertEquals(1,$data);
- //
- // $data = $redis->scriptLoad('a');
- // $this->assertEquals(1,$data);
- }
- /**
- * 服务器命令测试
- * testServer
- * @author tioncico
- * Time: 下午5:41
- */
- function testServer()
- {
- $redis = $this->redis;
- $data = $redis->bgRewriteAof();
- $this->assertEquals('Background append only file rewriting started', $data);
- Coroutine::sleep(1);
- $data = $redis->bgSave();
- $this->assertEquals('Background saving started', $data);
- $data = $redis->clientList();
- $this->assertIsArray($data);
- $data = $redis->clientSetName('test');
- $this->assertTrue($data);
- $data = $redis->clientGetName();
- $this->assertEquals('test', $data);
- $data = $redis->clientPause(1);
- $this->assertEquals(1, $data);
- $data = $redis->command();
- $this->assertIsArray($data);
- $data = $redis->commandCount();
- $this->assertGreaterThan(0, $data);
- $data = $redis->commandGetKeys('MSET', 'a', 'b', 'c', 'd');
- $this->assertEquals(['a', 'c'], $data);
- $data = $redis->time();
- $this->assertIsArray($data);
- $data = $redis->commandInfo('get', 'set');
- $this->assertIsArray($data);
- $data = $redis->configGet('*max-*-entries*');
- $this->assertIsArray($data);
- $data = $redis->configSet('appendonly', 'yes');
- $this->assertTrue($data);
- $data = $redis->configRewrite();
- $this->assertTrue($data);
- $data = $redis->configResetStat();
- $this->assertTrue($data);
- $data = $redis->dBSize();
- $this->assertGreaterThanOrEqual(0, $data);
- $redis->set('a', 1);
- $data = $redis->debugObject('a');
- $this->assertIsString($data);
- $data = $redis->flushAll();
- $this->assertTrue($data);
- $data = $redis->flushDb();
- $this->assertTrue($data);
- $data = $redis->info();
- $this->assertIsArray($data);
- $data = $redis->lastSave();
- $this->assertNotFalse($data);
- go(function () {
- $redis = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
- 'auth' => REDIS_CLUSTER_AUTH,
- ]));
- $redis->monitor(function (RedisCluster $redis, $data) {
- $this->assertIsString($data);
- $redis->set('a', 1);
- $redis->setMonitorStop(true);
- });
- });
- go(function () {
- $redis = new RedisCluster(new RedisClusterConfig(REDIS_CLUSTER_SERVER_LIST, [
- 'auth' => REDIS_CLUSTER_AUTH,
- ]));
- Coroutine::sleep(1);
- $redis->set('a', 1);
- });
- $data = $redis->save();
- $this->assertEquals(1, $data);
- // $data = $redis->clientKill($data[0]['addr']);
- // $this->assertTrue($data);
- // $data = $redis->slowLog('get', 'a');
- // var_dump($data,$redis->getErrorMsg());
- // $this->assertTrue(!!$data);
- }
- /**
- * geohash测试
- * testGeohash
- * @author tioncico
- * Time: 下午6:11
- */
- function testGeohash()
- {
- $redis = $this->redis;
- $key = 'testGeohash';
- $redis->del($key);
- $redis->del($key . 'new');
- $data = $redis->geoAdd($key, [
- ['118.6197800000', '24.88849', 'user1',],
- ['118.6197800000', '24.88859', 'user2',],
- ['114.8197800000', '25.88849', 'user3'],
- ['118.8197800000', '22.88849', 'user4'],
- ]);
- $this->assertEquals(4, $data);
- $data = $redis->geoAdd($key . 'new', [
- ['longitude' => '118.6197800000', 'latitude' => '24.88849', 'name' => 'user1',],
- ['longitude' => '118.6197800000', 'latitude' => '24.88859', 'name' => 'user2',],
- ['longitude' => '114.8197800000', 'latitude' => '25.88849', 'name' => 'user3'],
- ['longitude' => '118.8197800000', 'latitude' => '22.88849', 'name' => 'user4'],
- ]);
- $this->assertEquals(4, $data);
- $data = $redis->geoDist($key, 'user1', 'user2');
- $this->assertGreaterThan(10, $data);
- $data = $redis->geoHash($key, 'user1', 'user2');
- $this->assertIsArray($data);
- $data = $redis->geoPos($key, 'user1', 'user2');
- $this->assertIsArray($data);
- $data = $redis->geoRadius($key, '118.6197800000', '24.88849', 100, 'm', false, false, false, null, 'desc');
- $this->assertEquals(['user2', 'user1'], $data);
- $data = $redis->geoRadiusByMember($key, 'user1', 100, 'm', false, false, false, null, 'desc');
- $this->assertEquals(['user2', 'user1'], $data);
- }
- }
|