MrZyb Always in fear of one's own ignorance
  • SCAN
    • SCAN cursor [MATCH pattern] [COUNT count]
    • 作用:迭代当前数据库中的数据库键
    • SCAN 使用 demo
      <?php
      $redis = new Redis();
      $redis->connect('127.0.0.1', 6379);
      /* Options for the SCAN family of commands, indicating whether to abstract
      empty results from the user.  If set to SCAN_NORETRY (the default), phpredis
      will just issue one SCAN command at a time, sometimes returning an empty
      array of results.  If set to SCAN_RETRY, phpredis will retry the scan command
      until keys come back OR Redis returns an iterator of zero */
      $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
      $iterator = null;
      $count = 1000; // 测试时redis中大概有20w个key,用时约2s,当count为500时用时约4s,count越大时扫描用时越短(当然需要根据你的业务需要来定)
      $prefix = date('Ymd');
      $time1 = msectime();
      $total = [];
      while ($arrKeys = $redis->scan($iterator, $prefix . '*', $count)) {
      $arrValues = $redis->mget($arrKeys);
      $ret = array_combine($arrKeys, $arrValues);
      $total = array_merge($total, $ret);
      }
      $time2 = msectime();
      $time = $time2 - $time1;
      echo 'time : ' . $time  . ' ms; total keys : ' . count($total) . PHP_EOL;
      // time : 2009 ms; total keys : 129798  (用时2009 ms,20w中共有129798个前缀为$prefix的key)
      function msectime() {
      list($msec, $sec) = explode(' ', microtime());
      $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
      return $msectime;
      }
  • SSCAN
    • SCAN cursor [MATCH pattern] [COUNT count]
    • 作用:用于迭代集合键中的元素
    • SSCAN使用demo
      <?php
      $iterator = null;
      $count = 1000;
      $mainKey = date('Ymd');
      $prefix = $mainKey;
      $total = [];
      $redis = new Redis();
      $redis->connect('127.0.0.1', 6379);
      $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
      $time1 = msectime();
      while ($arrKeys = $redis->sscan($mainKey, $iterator, $prefix . '*', $count)) { // 匹配前缀为当前日期的key
      $total += $arrKeys;
      }
      $time2 = msectime();
      $time = $time2 - $time1;
      echo 'use time : ' . $time  . ' ms; total keys : ' . count($total) . PHP_EOL;
      // use time : 649 ms; total keys : 90010 (这个集合中有20w个元素)
      ....... other code ......
  • ZSCAN
    • ZSCAN cursor [MATCH pattern] [COUNT count]
    • 作用:用于迭代有序集合中的元素
    • ZSCAN使用demo
      <?php
      $iterator = null;
      $count = 1000;
      $mainKey = 'test';
      $prefix = '10';
      $total = [];
      $redis = new Redis();
      $redis->connect('127.0.0.1', 6379);
      $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
      $time1 = msectime();
      while ($arrKeys = $redis->zscan($mainKey, $iterator, $prefix . '*', $count)) { // 匹配key前缀是 10 的所有key
      var_dump($arrKeys);
      $total += $arrKeys;
      }
      $time2 = msectime();
      $time = $time2 - $time1;
      echo 'use time : ' . $time  . ' ms; total keys : ' . count($total) . PHP_EOL;
      // use time : 317 ms; total keys : 1111 (这个集合中有10w个元素)
      ...... other code ......
  • HSCAN
    • HSCAN cursor [MATCH pattern] [COUNT count]
    • 作用:用于迭代哈希中的元素
    • HSACN 使用demo
      <?php
      $iterator = null;
      $count = 1000;
      $mainKey = 'my_hash_key';
      $match = "*key*";
      $total = [];
      $redis = new Redis();
      $redis->connect('127.0.0.1', 6379);
      $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
      $time1 = msectime();
      while ($arrKeys = $redis->hscan($mainKey, $iterator, $match, $count)) { // 匹配含有'key'的键
      var_dump($arrKeys);
      $total += $arrKeys;
      }
      $time2 = msectime();
      $time = $time2 - $time1;
      echo 'use time : ' . $time  . ' ms; total keys : ' . count($total) . PHP_EOL;
      // use time : 1484 ms; total keys : 100000
      ...... other code ......
  • redis pipe 代码demo (快速向有序集合添加100000个key, 其他操作类似, 只要修改for中的操作即可)
    $pipe = $redis->multi(Redis::PIPELINE);
    for ($i = 0; $i < 100000; $i++) {
       $redis->zAdd($key, $i, $i);
    }
    $curValues = $pipe->exec();
    $val = $redis->zRange($key, 0, -1, true);
    var_dump($val);
  • 传送门
0.003441s