pfadd
用法和sadd一样
pfcount
用法和scard一样
127.0.0.1:6379> get lan
(nil)
127.0.0.1:6379> pfadd lan js
(integer) 1
127.0.0.1:6379> pfadd lan php
(integer) 1
127.0.0.1:6379> pfcount lan
(integer) 2
127.0.0.1:6379> pfadd lan php
(integer) 0
127.0.0.1:6379> pfcount lan
(integer) 2
127.0.0.1:6379> pfadd lan java python lua
(integer) 1
127.0.0.1:6379> pfcount lan
(integer) 5
当数据大时看看不精确率
<?php
$num=$argv[1]??200;
echo $num.PHP_EOL;
$redis=new redis();
$redis->connect('127.0.0.1',6379);
$redis->del('users');
for($i=1;$i<=$num;$i++){
$r=$redis->pfadd('users',['user_'.$i]);
$total=$redis->pfcount('users');
if($total!=$i){
echo $r.'-----i='.$i."----pfcount=".$total.PHP_EOL;
break;
}
}
[root@centos1 php]# php redis_hyperloglog.php 500
500
1-----i=128----pfcount=129
第128个时出现误差
下面看误差几率
<?php
$num=$argv[1]??200;
echo $num.PHP_EOL;
$redis=new redis();
$redis->connect('127.0.0.1',6379);
$redis->del('users');
for($i=1;$i<=$num;$i++){
$r=$redis->pfadd('users',['user_'.$i]);
$total=$redis->pfcount('users');
if($i == $num){
echo 'i='.$i."----pfcount=".$total.'---->'.($total-$i).'----'.($total-$i)/$i.PHP_EOL;
}
}
[root@centos1 php]# php redis_hyperloglog.php 500
500
i=500----pfcount=500---->0----0
[root@centos1 php]# php redis_hyperloglog.php 1000
1000
i=1000----pfcount=999---->-1-----0.001
[root@centos1 php]# php redis_hyperloglog.php 5000
5000
i=5000----pfcount=4996---->-4-----0.0008
[root@centos1 php]# php redis_hyperloglog.php 50000
50000
i=50000----pfcount=50115---->115----0.0023
[root@centos1 php]# php redis_hyperloglog.php 10000
10000
i=10000----pfcount=10009---->9----0.0009
[root@centos1 php]# php redis_hyperloglog.php 100000
100000
i=100000----pfcount=99839---->-161-----0.00161
[root@centos1 php]# php redis_hyperloglog.php 1000000
1000000
i=1000000----pfcount=997593---->-2407-----0.002407
100w误差率在0.002407也可以接受
误差率也不算高。然后我们把上面的脚本再跑一边,也就相当于将数据重复加入一边,查看输出,可以发现,pfcount 的结果没有任何改变,还是 997593,说明它确实具备去重功能
pfmerge
- 用于将多个 pf 计数值累加在一起形成一个新的 pf 值
127.0.0.1:6379> pfadd boy Tom John
(integer) 1
127.0.0.1:6379> pfadd girl Lily lucy Andy
(integer) 1
127.0.0.1:6379> pfmerge student boy girl
OK
127.0.0.1:6379> pfcount student
(integer) 5