我只有一個選項可以從該文本文件中獲取該數據,因此我無法使用數據庫來存儲該數據。從00:00起每天都會重新創建函數抓取數據的文件,所以它不會成爲一個真正的大問題。最大2 MB的大小,並在一天結束時最多6000 - 7000行。 我的問題是,它抓取數據並將其顯示在可以訪問很多次的網頁上(approximately up to 10,000 per day or less
) - 它會以某種方式真的使服務器超載使用file()
或對於這個小文件應該沒問題?請讓我知道。感謝您花時間閱讀我的問題,並可能回答。從.txt文件file()會影響大小約2 MB的文件的性能嗎?
示例行:
1,42,16, 201,stackoverflow_user, 1, 6762160, 39799, 9817242, 6762160, 39884, 10010545,stackoverflow_user, 2, 1351147, 1165, 483259, 1351147, 1115, 241630, 0
1,46,27, 201,[stackoverflow_user | stackoverflow_userother], 1, 4078465, 286991, 1594830, 4078465, 287036, 1643156,stackoverflow_user, 2, 1357147, 1115, 241630, 1357147, 1065, 120815, 0
我的功能:
# read a file into an array
$lines = file('C:/path/to/file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
# flip our array over so the last lines of the file are first.
$lines = array_reverse($lines);
$n = 1;
$wanted = 21; # or however many lines you want.
$content = '';
foreach ($lines as $l) {
# treat the data as comma-separated values
$arr = explode(",", $l);
# if col 5 has multiple values, take the first one
if (preg_match("/\[(.+?) \|/", $arr[4], $matches)) {
$arr[4] = $matches[1];
}
# is arr[4] the same as arr[12]?
if ($arr[4] !== $arr[12]) {
# these two are not equal, so use these values
$data = array('rank-pos' => $n++, 'rank-name' => $arr[4], 'rank-dmuser' => $arr[12]);
$content .= Template::Load('rankinguserdm-' . ($n % 2 == 1 ? 2 : 1), $data);
}
# have we got enough data?
if ($n === $wanted) {
break;
}
}
$this->content = Template::Load('user_rankingsdm', array('rankings' => $content));
}
如果文件每天只更改一次,我會在第一次加載時緩存頁面,並且僅在之後提供緩存版本 – kero 2014-10-17 21:56:01
不,它在白天更新(在一天中逐行添加行),所以它需要顯示來自行的最新新信息。所以我主要關心的是如果白天頁面加載多一點,大小這些小文件是否仍然會使服務器超載?它具有在安裝了nginx的Windows Server 2008上運行的16 GB RAM。 – Monk25 2014-10-17 21:58:23
如果不知道更多細節,很難給出是/否的答案。這臺服務器上還有什麼事情,腳本運行的頻率如何等等。PHP也不是最快的語言。將文件保存在RAM上可能會加快一點。我的建議:使用這個程序並監視你的服務器的負載一段時間。 – Rein 2014-10-17 22:30:57