handler = fopen($dataDir.$dbname.'.db', 'a+'); } /** * 写入一行数据 * @return bool */ public function putLine($data) { if ($this->handler != null) { fwrite($this->handler, $this->seralize($data)); } return false; } /** * 分页获取数据列表 * @param $key * @return array|null */ public function getDataList($page, $pagesize) { if($page <= 0) { $page = 1; } $offset = ($page - 1) * $pagesize; //循环读取数据 $datas = []; $counter = 0; while (!feof($this->handler)) { if ($counter < $offset) { fgets($this->handler); //移动指针到下一行 $counter++; continue; } if (count($datas) == $pagesize) { break; } $line = fgets($this->handler); if (!empty($line)) { $datas[] = $this->unseralize($line); } } return $datas; } /** * 序列化数据 * @param $data * @return string */ private function seralize($data) { $break = "\n"; //换行符 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $break = "\r\n"; } return json_encode($data, JSON_UNESCAPED_UNICODE).$break; } /** * 反序列化 * @param $data * @return mixed */ private function unseralize($data) { return json_decode($data, true); } /** * 关闭文件 */ public function __destruct() { if ($this->handler != null) { fclose($this->handler); } } }