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 < 4294967296) or die('number must be less than 4294967296'); // 2^32if ($this->max < $num) {fseek($this->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 1fseek($this->handler, ftell($this->handler)-1, SEEK_SET); // write a new bytefwrite($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 0fseek($this->handler, ftell($this->handler)-1, SEEK_SET); // write a new bytefwrite($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');”? 把檔案,放在記憶體裡,增加讀寫速度
執行結果如下:
[email protected]:~/web/test/shm$ /bin/sh ./geany_run_script.sh
bool(true)
bool(false)
?
產生的bitmapdata檔案 在附件裡面:
?
?