zRevRangeByScore命令的使用示例
一个使用案例
$pagesize = "10";//这里pagesize是string类型
$redis->zRevRangeByScore(
"example",
"+inf",
"-inf",
['withscores' => true, 'limit' => [0, $pagesize]]
);
$redis->zRevRangeByScore(
"example",
"+inf",
"-inf",
['withscores' => true, 'limit' => [0, $pagesize]]
);
猜猜会有什么惊喜?
答案是分页失效了,这个命令会查出example集合的所有数据
查看了源码,才发现有类型检查,string类型的pagesize在参数类型判断的时候会返回false,这样limit的参数就会不生效,导致分页失效
//redis_commands.c 399-411行
......
// LIMIT
if ((z_ele = zend_hash_str_find(ht_opt,"limit", sizeof("limit") - 1)) != NULL) {
HashTable *ht_limit = Z_ARRVAL_P(z_ele);
zval *z_off, *z_cnt;
if ((z_cnt = zend_hash_index_find(ht_limit,0)) != NULL &&
(z_off = zend_hash_index_find(ht_limit,1)) != NULL &&
Z_TYPE_P(z_off)==IS_LONG && Z_TYPE_P(z_cnt) == IS_LONG)//类型检查
{
has_limit = 1;
limit_low = Z_LVAL_P(z_off);
limit_high = Z_LVAL_P(z_cnt);
}
}
......
......
// LIMIT
if ((z_ele = zend_hash_str_find(ht_opt,"limit", sizeof("limit") - 1)) != NULL) {
HashTable *ht_limit = Z_ARRVAL_P(z_ele);
zval *z_off, *z_cnt;
if ((z_cnt = zend_hash_index_find(ht_limit,0)) != NULL &&
(z_off = zend_hash_index_find(ht_limit,1)) != NULL &&
Z_TYPE_P(z_off)==IS_LONG && Z_TYPE_P(z_cnt) == IS_LONG)//类型检查
{
has_limit = 1;
limit_low = Z_LVAL_P(z_off);
limit_high = Z_LVAL_P(z_cnt);
}
}
......
正确的写法
$pagesize = 10;//这里pagesize是int类型
$redis->zRevRangeByScore(
"example",
"+inf",
"-inf",
['withscores' => true, 'limit' => [0, (int)$pagesize]]//pagesize参数可能是其他地方传递过来的,所有进行强制类型转化,当然能保证类似是int,则不需要强制类型转换
);
$redis->zRevRangeByScore(
"example",
"+inf",
"-inf",
['withscores' => true, 'limit' => [0, (int)$pagesize]]//pagesize参数可能是其他地方传递过来的,所有进行强制类型转化,当然能保证类似是int,则不需要强制类型转换
);
转载请注明:小Y » phpredis扩展zRevRangeByScore命令的一个坑