phpredis到底该如何使用hScan(范例)

phpredis到底该如何使用hScan呢?官方手册上写得很模糊,所以到处找资料。虽然最后没用得上,但还是把范例贴上,供有心人研究(范例不是本人写的,只是搜集的)。
范例一:

public function testHScan()
{
    if (version_compare($this->version, "2.8.0", "lt")) {
        $this->markTestSkipped();
        return;
    }
    // Never get empty sets
    $this->redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
    $this->redis->del('hash');
    $i_foo_mems = 0;
    for ($i = 0; $i < 100; $i++) {
        if ($i > 3) {
            $this->redis->hset('hash', "member:{$i}", "value:{$i}");
        } else {
            $this->redis->hset('hash', "foomember:{$i}", "value:{$i}");
            $i_foo_mems++;
        }
    }
    // Scan all of them
    $it = NULL;
    while ($arr_keys = $this->redis->hscan('hash', $it)) {
        $i -= count($arr_keys);
    }
    $this->assertEquals(0, $i);
    // Scan just *foomem* (should be 4)
    $it = NULL;
    while ($arr_keys = $this->redis->hscan('hash', $it, '*foomember*')) {
        $i_foo_mems -= count($arr_keys);
        foreach ($arr_keys as $str_mem => $str_val) {
            $this->assertTrue(strpos($str_mem, 'member') !== FALSE);
            $this->assertTrue(strpos($str_val, 'value') !== FALSE);
        }
    }
    $this->assertEquals(0, $i_foo_mems);
}

范例二:

connect('127.0.0.1', 6379);
// $redis->flushAll();
// exit();
$key = rand(0, 9999);
$fieldKey = rand(0, 49);
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
$keys = $redis->hscan('user' . $key, $it, '*field' . $fieldKey . '*', 50);

你可能感兴趣的:(phpredis到底该如何使用hScan(范例))