How should I structure my rss feed pulling site to make the site load quick? How should I structure my rss feed pulling site to make the site load quick? codeigniter codeigniter

How should I structure my rss feed pulling site to make the site load quick?


Props for using CodeIgniter, I hate seeing people build from the ground up without a framework to alleviate boilerplate code.

Look into setting up a CRONJOB or windows task scheduler, and have a CONTROLLER that handles fetching your RSS feeds (and some form of caching).

You can SIMPLY cache them using the built in CI cache or do it as you describe by storing the text in your DB.

How to run your cronjob via CLI: http://codeigniter.com/user_guide/general/cli.html

Or you can just look for an existing CI library to do the caching / fetching for you:http://codeigniter.com/forums/viewthread/160394/


From my experience in runninng RSS large scale aggregators.

I higly recommend that you use SimplePie. It makes your work drastically easier.It has caching mechanisim built in, so you can serve a cache to the home page while storing the content you want in your database.

SimplePie also has built in like-wordpress functions, that get the title, content, timestamp...etc. It also lets you mix multiple RSS feeds into one.

Sorry the answer ended up being about simplepie, but it's one of the best choices I've made when I ran those sites.


Heres how you can cache your feeds onto your file system, no db required, and pull from there for a curtain amount of time, it will considerably speed up your app. Perhaps its of some interest.

<?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 renderforeach($feed_result as $result){    render_feed_newz_caption($result);}//The curl function, curl is considerably faster then fopen that simplexml_load_file usesfunction 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 parsefunction 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>";    }}?>