首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cakephp Twitch.Tv接口加载时间

Cakephp Twitch.Tv接口加载时间
EN

Stack Overflow用户
提问于 2012-10-23 14:39:01
回答 1查看 215关注 0票数 0

我正在使用以下代码:

代码语言:javascript
复制
public function getUser($name) {
        $return = array();
        $file = array();
        $httpSocket = new HttpSocket();
        $url = $this->baseUrl . $name . $this->apiKey;
        $temp = $httpSocket->get($url);
        $file = $temp->body;
        $file = explode(',', $file);
        $i = 0;

        foreach ($file as $info) {
            $info = str_replace("{", "", $info);
            $info = str_replace("}", "", $info);
            $info = str_replace('"', "", $info);
            $info = str_replace("[", "", $info);
            $info = str_replace("]", "", $info);
            $temp = explode(':', $info, 2);

            if ($temp[0] == 'stream') {
                $temp[1] = str_replace("game:", "", $temp[1]);
                if ($temp[1] == 'StarCraft II: Wings of Liberty') {
                    $temp[1] = 'starcraft-II';
                }
                $return[$i]['game'] = $temp[1];
            } elseif ($temp[0] == 'teams') {
                $temp[1] = str_replace("name:", "", $temp[1]);
                if ($temp[1] != '') {
                    $return[$i]['teams'] = $temp[1];
                } else {
                    $return[$i]['teams'] = null;
                }
            } else {
                if (isset($temp[1])) {
                    $return[$i][$temp[0]] = $temp[1];
                }
            }
        }

        return $return;
    }

我想知道是否有任何我可以做的事情来减少这个脚本的加载时间。我从TwitchTV中提取了一个json文件进行记录。该功能在页面刷新/加载时效果很好,页面渲染会有明显的2-3秒延迟。一如既往,任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2012-10-23 16:28:48

你知道API的响应是json吗?为什么要手动解析响应,而不是使用json_decode()?使用json_decode()并查看它将生成的内容。您可以删除90%的代码。

此外,我会缓存API响应,根据您的需要,几分钟到几个小时。请阅读书籍http://book.cakephp.org/2.0/en/core-libraries/caching.html的缓存章节,页面应该在刷新时立即加载,因为它不需要进行另一个API调用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13024798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档