- 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);
- 传送门
PHP redis SCAN、SSCAN、ZSCAN、HSCAN 的使用
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- NOSQL类型简介键值对:会使用到一个哈希表,表中有一个特定的键和一个指针指向特定的数据,如redis,volde...
- =========================================================...