[英]Faster way to read RSS through Javascript/PHP
今天為您提供了另一個樂趣。
我有工作代碼,可以從各種RSS提要中提取代碼,並根據它們中哪個具有最新發布來對其進行排序。
我的問題是速度很慢。 這減慢了網站的加載速度。 不僅如此,由於它都是托管服務器端,因此如果有多個人同時訪問該站點,則會減慢響應速度。
因此,我想將其轉換為JavaScript函數,然后將標記的innerHTML填充到從數據庫中提取的內容中,或者將使用我希望任何人建議的其他選項(如果速度更快)。
無需進一步的理由:代碼:
PHP
function RSSFeeder() {
$client = buildCon();
//Query removed, simply gets the RSS URL from the database
$query = "";
$result = $client ->run($query);
$RSSList = array();
foreach($result ->getRecords() as $record)
{
$ComicArray = array();
$ComicName = $record ->value('Name');
$RSS = $record ->value('RSS');
$URL = $record ->value('URL');
$content = file_get_contents($RSS);
$x = new SimpleXmlElement($content);
for ($i = 0; $i < 1; $i++) {
$profile = $x ->channel ->item[$i];
$pubDate = $profile ->{ "pubDate"};
}
$ComicArray['URL'] = $URL;
$ComicArray['Comic'] = $ComicName;
$ComicArray['pubDate'] = $pubDate;
$RSSList[] = $ComicArray;
}
#usort($RSSList, "sortFunction");
usort($RSSList, "compareRSSTimes");
return $RSSList;
}
最后,您可能看到了usort方法,因此它是:
function compareRSSTimes($a, $b) {
$a = strtotime($a['pubDate']);
$b = strtotime($b['pubDate']);
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
從那里開始,將數組發送回PHP腳本,該腳本根據更新的時間順序構建輸出。 工作正常。 加載頁面只需要一點時間,如果/當更多用戶訪問該頁面時,我擔心我的Terribad服務器的可持續性。
建議?
您可能經常這樣做,而不是每次都這樣做。 如果無法使用數據庫,則使用老式方法將“大量”包裝在緩存文件存儲中可以非常快速地工作。
一個例子:
function RSSFeeder() {
$cachefile = '/path/to/RSSList.json';// <- must be local server path, not a URL
if (filemtime($cachefile) < strtotime('now -1 minute')) {
// if stale, rebuild it
// .. do your normal building of the $RSSList here ..
file_put_contents($cachefile,json_encode($RSSList));
return $RSSList;
} else {
// else output cache
return json_decode(file_get_contents($cachefile),true);
}
}
它可能更優雅……或者使用功能強大的頂級功能齊全的庫來做同樣的事情。
但這是可行的,可以自我修復,僅在需要時進行更新,不需要cron作業,並且比打開db連接,查詢db和輸出存儲的數據要快一點...但還不夠大。 因此,在那里您的偏好更為重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.