5百万 uid 白名单 之 PHP Bit地图 处理
程序员文章站
2024-01-18 19:06:22
...
5百万 uid 白名单 之 PHP Bitmap 处理
近日,有同事说有个 5百万 的白名单处理, 到网上查找 相似案例, 说是有个bitmap的解法,
再次 google ,baidu 无相关 PHP 的解法, 于是自己写了一个:
?
handler = @fopen($file , 'r+') OR die('open bitmap file failed'); else $this->handler = @fopen($file , 'w+') OR die('open bitmap file failed'); $this->max = file_exists($file) ? (filesize($file) * 8 - 1) : 0; } public function __destruct() { @fclose($this->handler); } private function binary_dump($binary_data) { return sprintf('%08d',decbin(hexdec(bin2hex($binary_data)))); } private function num_check($num) { ($num > -1) OR die('number must be greater than -1'); ($num max handler, 0, SEEK_END); fwrite($this->handler , str_repeat("\x00",ceil(($num - $this->max)/8))); // fill with 0 $this->max = ceil($num/8)*8 - 1; } } public function set($num) { $this->num_check($num); fseek($this->handler, floor($num/8), SEEK_SET); $bin = fread($this->handler, 1) | pack('C',0x100 >> fmod($num,8)+1); // mark with 1 fseek($this->handler, ftell($this->handler)-1, SEEK_SET); // write a new byte fwrite($this->handler, $bin); fflush($this->handler); } public function del($num) { $this->num_check($num); fseek($this->handler, floor($num/8), SEEK_SET); $bin = fread($this->handler, 1) & ~pack('C',0x100 >> fmod($num,8)+1); // mark with 0 fseek($this->handler, ftell($this->handler)-1, SEEK_SET); // write a new byte fwrite($this->handler, $bin); fflush($this->handler); } public function find($num) { if (fseek($this->handler, floor($num/8), SEEK_SET) == -1) return FALSE; $bin = fread($this->handler , 1); if ($bin === FALSE || strlen($bin) == 0) return FALSE; $bin = $bin & pack('C',0x100 >> fmod($num,8)+1); if($bin === "\x00") return FALSE; return TRUE; } } $b = new Bitmap('/dev/shm/bitmapdata'); // 设置白名单 $b->set(1); $b->set(3); $b->set(5); $b->set(7); $b->set(9); $b->set(501); $uid = 501; var_dump($b->find($uid)); // 查找白名单 $b->del($uid); // 删除白名单 var_dump($b->find($uid)); // 查找白名单
?
其中 “$b = new Bitmap('/dev/shm/bitmapdata');”? 把文件,放在内存里,增加读写速度
执行结果如下:
hu@xunleiman-desktop:~/web/test/shm$ /bin/sh ./geany_run_script.sh
bool(true)
bool(false)
?
生成的bitmapdata文件 在附件里面:
?
?
相关文章
相关视频
上一篇: apache装配文件在哪
下一篇: ipad-php中为IPAD加载CSS