RedisTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Units;
  3. use HyperfTest\HttpTestCase;
  4. use Psr\SimpleCache\CacheInterface;
  5. class RedisTest extends HttpTestCase
  6. {
  7. protected $redisClient = null;
  8. protected function setUp():void
  9. {
  10. parent::setUp();
  11. $this->redisClient = new \Redis();
  12. $redisParams = [
  13. 'host' => '127.0.0.1',
  14. 'port' => 6379,
  15. 'connectTimeout' => 2.5,
  16. 'auth' => 'qt3t1cjRbEF',
  17. 'ssl' => ['verify_peer' => false],
  18. ];
  19. $this->redisClient->connect(
  20. $redisParams['host'],
  21. $redisParams['port'],
  22. );
  23. $this->redisClient->auth($redisParams['auth']);
  24. }
  25. protected function tearDown():void
  26. {
  27. parent::tearDown();
  28. }
  29. private function string2bin($str)
  30. {
  31. //1.列出每个字符
  32. $arr = preg_split('/(?<!^)(?!$)/u', $str);
  33. //2.unpack字符
  34. foreach ($arr as &$v) {
  35. $temp = unpack('H*', $v);
  36. $v = base_convert($temp[1], 16, 2);
  37. unset($temp);
  38. }
  39. return join(' ', $arr);
  40. }
  41. public function testSetbitmap()
  42. {
  43. $cache = make(CacheInterface::class);
  44. $pickNum = 9;
  45. $key = 'test_bit';
  46. for ($i = 0; $i < $pickNum; $i++) {
  47. $value = $i % 3 == 0 ? 1 : 0;
  48. $value = 1;
  49. $this->redisClient->rawCommand('setBit', $key, $i, $value);
  50. }
  51. $result = $this->redisClient->rawCommand('bitfield', $key, 'get', 'u'. strval($pickNum + 1), 0);
  52. $r1 = decbin($result[0]);
  53. $len1 = strlen($r1);
  54. $str1 = str_pad($r1, $pickNum + 2, '0', STR_PAD_LEFT);
  55. // 首尾永远是0,多出来2个0.
  56. var_dump($result, $len1, $r1, $str1);
  57. $a1 = 'a';
  58. $b1 = str_pad($a1, 2, '0', STR_PAD_LEFT);
  59. var_dump($b1);
  60. $this->assertIsBool(true);
  61. }
  62. }