项⽬中使⽤Redis的游标scan的⼀些⼩问题最近项⽬中有⼀个需求就是在下拉中要筛选车辆列表,本来想着是在内存中全部⽤程序去遍历处理,但发现数据有点多,⼀个个去处理会有点慢。然后就到了redis的游标
感觉这个能满⾜我的需求,我可以把key存成车牌号,value为车辆id的值,通过正则匹配到车牌号就能获取车辆id来查信息了。思路有了,接下来就开⼯了。
在官⽹上到这么⼀段程序
/* Without enabling Redis::SCAN_RETRY (default condition) */
$it = NULL;
do {
// Scan for some keys
$arr_keys = $redis->scan($it);
// Redis may return empty results, so protect against that
if ($arr_keys !== FALSE) {
foreach($arr_keys as$str_key) {
echo "Here is a key: $str_key\n";
}
}
} while ($it > 0);
echo "No more keys to scan!\n";
/* With Redis::SCAN_RETRY enabled */
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
$it = NULL;
/* phpredis will retry the SCAN command if empty results are returned from the
setoption
server, so no empty results check is required. */
while ($arr_keys = $redis->scan($it)) {
foreach ($arr_keys as$str_key) {
echo "Here is a key: $str_key\n";
}
}
echo "No more keys to scan!\n";
这段程序在我的环境会有问题(php7). 会报 Parameter 1 to Redis::scan() expected to be a reference, value given
应该是那个游标对象$it得⽤引⽤。所以需要改成call_user_func_array调⽤
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
$it = NULL;
/* phpredis will retry the SCAN command if empty results are returned from the
server, so no empty results check is required. */
while ($array = call_user_func_array(array($redis, 'scan'), array(&$it)) {
foreach ($arr_keys as$str_key) {
echo "Here is a key: $str_key\n";
}
}
echo "No more keys to scan!\n";
⽤上⾯的程序,就能通过游标把所有符合的值取出来。在业务层使⽤就⾏了
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论