我的网站 daisy.camorada.com 运行非常缓慢。它运行缓慢的原因是我正在拉多个 RSS 提要来创建显示在页面上的每个砌体。

我对你们的问题基本上是,我应该如何构建我的网站以使其快速、高效和可扩展?

我曾考虑使用 Code Igniter 将 RSS 提要放入数据库,然后在页面刷新时从该数据库中提取。我该怎么做?

这是我正在考虑的结构图:

这是当前拉取提要的 PHP 代码(我知道它非常困惑,抱歉):
https://gist.github.com/3506863

最佳答案

这是您如何将您的提要缓存到您的文件系统上,不需要数据库,并从那里拉出一段时间,它将大大加快您的应用程序的速度。也许它有一些兴趣。

<?php
//Have a list of feeds
$feeds = array(
'http://rss.cnn.com/rss/cnn_topstories.rss',
'http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=breakingnews',
'http://www.nytimes.com/services/xml/rss/nyt/pop_top.xml',
'http://news.yahoo.com/rss',
);

$cache_for = 3600; //in seconds
$feed_results = array();

/**
 * Loop through each feed and check if its age is older then $cache_for
 * Grab the feed and store in ./feeds_data
 * On next refresh feed is pulled from cache until $cache_for expires
 */
foreach($feeds as $feed){
    if(cache(sha1($feed), 'check', null, './feeds_data', $cache_for) == false){
        $result = curl_get($feed);
        $feed_result[$feed] = cache(sha1($feed), 'put', $result, './feeds_data', $cache_for);
    }else{
        $feed_result[$feed] = cache(sha1($feed), 'get', null, './feeds_data', $cache_for);
    }
}

//Loop through each feed result and render
foreach($feed_result as $result){
    render_feed_newz_caption($result);
}

//The curl function, curl is considerably faster then fopen that simplexml_load_file uses
function curl_get($url){
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Firefox/5.0');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_ENCODING,'gzip,deflate');
    curl_setopt($ch, CURLOPT_AUTOREFERER,true);
    $output = curl_exec($ch);
    curl_close($ch);

    return $output;
}

function cache($key, $do, $result=null, $storepath, $cacheTime=86400){
    switch($do){
        case "check":
            if(file_exists($storepath.'/'.sha1($key).'.php')){
                if((time() - $cacheTime < filemtime($storepath.'/'.sha1($key).'.php'))){
                    return true;
                }
                return false;
            }else{
                return false;
            }
            break;
        case "put":
            //Compress
            $compressed = gzdeflate($result,  9);
            $compressed = gzdeflate($compressed, 9);
            file_put_contents($storepath.'/'.sha1($key).'.php', base64_encode($compressed));
            return $result;
            break;
        case "get":
            $cache = base64_decode(file_get_contents($storepath.'/'.sha1($key).".php"));
            //De-compress
            $compressed = gzinflate($cache);
            $compressed = gzinflate($compressed);
            return $compressed;
            break;
        default:
            return false;
            break;
    }
}



//Function to wrap your parse
function render_feed_newz_caption($feed){
    //load XML string!
    $xml = simplexml_load_string($feed);
    libxml_use_internal_errors(true);

    if(isset($xml->channel->item))
    foreach($xml->channel->item as $YODEL){
        $title = $YODEL->title;
        $description = $YODEL->description;
        $link = $YODEL->link;
        $pubDate = $YODEL->pubDate;     //should be able to use Rutgers pubDate to sort newest.... get a better Drudge feed to do the same
        $image = $YODEL->image;
        //echo"<div class ='masonry_item' style='background: #FFFFFF;'><a href='". $link ."'>" . "RUT: " . $pubDate . "<br />" . $title . "</a> <!--" . $description . "--></div> <br>";

        echo "
    <div class='box'>
        <div class ='newz_caption' style='background: #FFFFFF;'>
            <h3>"  . $title . "</h3>
            <h5>CNN: "  . $pubDate . "</h5>
            <p>     " . $description .  "   </p>
            <p>
                <a class='btn btn-primary' href='" . $link . "'>    Source  </a>
                    <a class='btn' href='#'>Thumbs Up </a>
            </p>
        </div>
    </div>
    <br>";
    }
}

?>

关于php - 我应该如何构建我的 RSS 提要拉取站点以加快站点加载速度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12170942/

10-13 00:49