doujiaochan7317 2017-12-08 18:47 采纳率: 0%
浏览 44
已采纳

通过Javascript / PHP更快地阅读RSS

Got another fun one for you all today.

I have working code that pulls from various RSS feeds and orders them by which of them has most recent pubdate.

The issues I have is that it is slow. It slows down the loading of the site. Not only that, because it's all hosted server side, it slows down the response if multiple people are accessing the site simultaneously.

So, I'd like to convert this into either a JavaScript function, which would then fill the tag's innerHTML to what's pulled back from the database or another option that I would love for anyone to suggest (If it's faster).

Without further adieu: The Code:

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;
}

At the end, you probably saw the usort method, so here it is:

function compareRSSTimes($a, $b) {
    $a = strtotime($a['pubDate']);
    $b = strtotime($b['pubDate']);

    if ($a == $b) {
        return 0;
    }
    return ($a > $b) ? -1 : 1;
}

From there, the array is sent back to a PHP script that builds the output based on the chronological order of updates. It works fine. It just takes a little while to load the page and I'm worried about the sustainability of my terribad server if/when more users access the page.

Suggestions?

  • 写回答

1条回答 默认 最新

  • duanhan4763 2017-12-08 19:04
    关注

    You could probably get away with doing this every so often, instead of every single time. If not able to use a database, using an oldschool method of just wrapping the 'heavy bits' in a cache file storage can work very quickly.

    An example of that:

    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);
        }
    }
    

    It could be more elegant... or use a bloated over the top fully featured library to do the same thing.

    But this works, is self mending, only updates when called for, doesn't require a cron job, and is a weee bit faster than opening a db connection, querying the db, and outputting the data stored... but not huge enough. So your preference is more important there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥150 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装