| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace Units;
- use HyperfTest\HttpTestCase;
- use Psr\SimpleCache\CacheInterface;
- class RedisTest extends HttpTestCase
- {
- protected $redisClient = null;
- protected function setUp():void
- {
- parent::setUp();
- $this->redisClient = new \Redis();
- $redisParams = [
- 'host' => '127.0.0.1',
- 'port' => 6379,
- 'connectTimeout' => 2.5,
- 'auth' => 'qt3t1cjRbEF',
- 'ssl' => ['verify_peer' => false],
- ];
- $this->redisClient->connect(
- $redisParams['host'],
- $redisParams['port'],
- );
- $this->redisClient->auth($redisParams['auth']);
- }
- protected function tearDown():void
- {
- parent::tearDown();
- }
- private function string2bin($str)
- {
- //1.列出每个字符
- $arr = preg_split('/(?<!^)(?!$)/u', $str);
- //2.unpack字符
- foreach ($arr as &$v) {
- $temp = unpack('H*', $v);
- $v = base_convert($temp[1], 16, 2);
- unset($temp);
- }
- return join(' ', $arr);
- }
- public function testSetbitmap()
- {
- $cache = make(CacheInterface::class);
- $pickNum = 9;
- $key = 'test_bit';
- for ($i = 0; $i < $pickNum; $i++) {
- $value = $i % 3 == 0 ? 1 : 0;
- $value = 1;
- $this->redisClient->rawCommand('setBit', $key, $i, $value);
- }
- $result = $this->redisClient->rawCommand('bitfield', $key, 'get', 'u'. strval($pickNum + 1), 0);
- $r1 = decbin($result[0]);
- $len1 = strlen($r1);
- $str1 = str_pad($r1, $pickNum + 2, '0', STR_PAD_LEFT);
- // 首尾永远是0,多出来2个0.
- var_dump($result, $len1, $r1, $str1);
- $a1 = 'a';
- $b1 = str_pad($a1, 2, '0', STR_PAD_LEFT);
- var_dump($b1);
- $this->assertIsBool(true);
- }
- }
|