必须有办法让这个PHP更快

时间:2012-04-11 16:39:00

标签: php html performance mobile rss

我正在研究www.sciencedaily.com的小型移动应用版本。只是一个小小的项目。

我正在使用RSS_PHP来获取XML提要,而我的代码(效果很好)在DOCTYPE之前看起来像这样:

require_once '_/rss_php.php';

$featuredRSS = new rss_php;
$healthMedRSS = new rss_php;
$mindBrainRSS = new rss_php;
$plantsAnimalsRSS = new rss_php;
$earthClimateRSS = new rss_php;
$spaceTimeRSS = new rss_php;
$matterEnergyRSS = new rss_php;
$compMathRSS = new rss_php;
$archaeoRSS = new rss_php;

$featuredRSS->load('http://www.sciencedaily.com/rss/top_news/top_science.xml');
$healthMedRSS->load('http://www.sciencedaily.com/rss/health_medicine.xml');
$mindBrainRSS->load('http://www.sciencedaily.com/rss/mind_brain.xml');
$plantsAnimalsRSS->load('http://www.sciencedaily.com/rss/plants_animals.xml');
$earthClimateRSS->load('http://www.sciencedaily.com/rss/earth_climate.xml');
$spaceTimeRSS->load('http://www.sciencedaily.com/rss/space_time.xml');
$matterEnergyRSS->load('http://www.sciencedaily.com/rss/matter_energy.xml');
$compMathRSS->load('http://www.sciencedaily.com/rss/computers_math.xml');
$archaeoRSS->load('http://www.sciencedaily.com/rss/fossils_ruins.xml');

$featuredItems = $featuredRSS->getItems();
$healthMedItems = $healthMedRSS->getItems();
$mindBrainItems = $mindBrainRSS->getItems();
$plantsAnimalsItems = $plantsAnimalsRSS->getItems();
$earthClimateItems = $earthClimateRSS->getItems();
$spaceTimeItems = $spaceTimeRSS->getItems();
$matterEnergyItems = $matterEnergyRSS->getItems();
$compMathItems = $compMathRSS->getItems();
$archaeoItems = $archaeoRSS->getItems();

然后,在内容中,我通过使用像

这样的经典内容回应结果
foreach($items as $item) {

    echo $item['title'];
}
etc...

就像我说的,一切都很好。但它真的很慢。我知道应用程序的速度会受到限制,因为它必须抓取Feed,但RSS_PHP没有像SimplePie那样的缓存功能。

有关提高速度的想法吗?也许首先加载精选的东西然后其他所有东西?

提前致谢!!

5 个答案:

答案 0 :(得分:5)

使用CURL library。可以同时选择多个请求。所有请求将同时并行运行。查看此链接以获取示例和教程。 php curl parallel requests

<强>更新

查找有关php_rss的文档。使用

$testRSS->loadRSS($res); //$Res is string data from Curl instead of url

答案 1 :(得分:1)

我将尝试让您开始使用线程。为了清晰起见,我使用了数量较少的RSS Feed。

require_once '_/rss_php.php';

public class loadFeeds(){

private int workers = 0;

private function launchWorker(&$feed, $url) {//NOTE: pass by reference!
  $pid = pcntl_fork();
  switch ($pid) {
  case -1: // fork failed
    $this->clog("ERROR: Worker fork failure. Running inline.");
    $feed->load($url);
    break;
  case 0: // child fork
    $feed->load($url));
    break;
  default: // parent fork?
    $this->workers++;
    echo "$this->workers launched.\n";
    break;
}

public function load() {
  $featuredRSS = new rss_php;
  $healthMedRSS = new rss_php;
  $mindBrainRSS = new rss_php;

  //Start some threads
  launchWorker($featuredRSS, 'http://www.sciencedaily.com/rss/top_news/top_science.xml');
  launchWorker($healthMedRSS, 'http://www.sciencedaily.com/rss/top_news/top_science.xml');
  launchWorker($mindBrainRSS, 'http://www.sciencedaily.com/rss/top_news/top_science.xml');

  $status = 0;
  while ($this->workers > 0) {//wait until all workers are done.
    while ($pid = pcntl_wait($status, WNOHANG OR WUNTRACED)) {//wait for a thread to report
      if ($pid > 0) { // if we have a valid pid 
        $this->workers--;//recover worker
      }
    }
  }

  $featuredItems = $featuredRSS->getItems();
  $healthMedItems = $healthMedRSS->getItems();
  $mindBrainItems = $mindBrainRSS->getItems();
}

请注意,我还没有对此进行过测试,因为我目前尚未对其进行测试,但它为您提供了主要组件。如果遇到问题,可以在此处提出其他问题。

答案 2 :(得分:0)

你可以:

  • 实施某种形式的缓存,无论如何都会有所帮助。
  • 也许考虑使用一些ajax,页面会加载快,而RSS 一旦他们进入,他们将一个接一个地展示。你可能也会 能够同时打几个电话。

答案 3 :(得分:0)

实现缓存服务器端,这是我几年前创建的脚本(根据需要更正):

<强> rss-fetch.sh:

#!/bin/bash
#------------------------------------------------------------------
#
# This script will run via CRONTAB and fetch RSS feeds from the
# urls.txt file, which can be used internally.  This way we minimize
# the number of requests externally for data.
#
# - created by Jakub - March 26 2008
#
#------------------------------------------------------------------

basedir=/htdocs/RSS
storedir=/htdocs/RSS/read/
sourcefile=/htdocs/RSS/urls.txt

#------------------------------------------------------------------
# Read the URLS.TXT file to get the URL/filename
#
# Formatted:
# http://rss.slashdot.org/Slashdot/slashdot/slashdot.xml
# ^- URL                    ^- filename to save as

for s in `cat "$sourcefile"`; 
    do  
        geturl=`dirname $s`;
        filename=`basename $s`;
        wget -qN $geturl -pO "$storedir"$filename;
done;

#------------------------------------------------------------------

然后将本地RSS订阅源拉入PHP进行解析,您的延迟是获取外部源。

如果您在CRON上设置上面的脚本,它将以您想要的任何频率获取。 享受!

答案 4 :(得分:-1)

根据the documentation,它也可以从本地URI加载数据 - 为什么不在单独的脚本中提取远程提要,比如每15分钟一次,并且只在这里加载本地版本?它可以减少远程服务器上的负载减少带宽使用。