doucang6739 2018-01-21 10:16
浏览 103
已采纳

Laravel帮助页面加载缓慢

I hava helper class and have functions:

  static public function getPRBcurr(){
    $currency = Cache::get('currency_prb_ee_agroTimeout');
    if($currency) return $currency;

    $contentCurrency = self::file_get_contents_curl(
        'https://www.agroprombank.com/xmlinformer.php'
    );

    if(!$contentCurrency) {
        $_currency = Storage::get('currency_prb_agro');
        Cache::put('currency_prb_agroTimeout', $_currency, 60);
        return $_currency;
    }

    $xmls = simplexml_load_string( $contentCurrency );

    $currency = (string)$xmls->course[1]->currency[5]->currencySell;
    $currency = ($currency > 0) ? $currency : null;
    $currency = (strpos($currency, '1.0') !== false) ? '0.99' : $currency;

    Cache::put('currency_prb_agroTimeout', $currency, 720);
    Storage::put('currency_prb_agro', $currency);
    return $currency;
}

static function file_get_contents_curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 500);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

And I have a product.blade:

<div class="price-prb">
            <span itemprop="price">
                @php
                    $prb = intval($product->price * Helper::getPRBcurr());
                @endphp
                {{ $prb }}
                <span itemprop="priceCurrency" content="PRB"> {!! trans_choice('messages.currency.RUB', $prb) !!}</span>
            </span>
        </div>

When I go to category, where all products (890 products), page loading slow. If I remove Helper::getPRBcurr() all is good. How I can fix this problem? Problem in function file_get_contents_curl 500ms timeout.

  • 写回答

2条回答 默认 最新

  • dongmangzong8006 2018-01-21 10:20
    关注

    Calling Helper::getPRBcurr() directly with the view file is a bad idea. The function must fully complete before the page with render, hence the slow loading. Moving it to an asychronous call will render the page quickly and then wait for the data to fetch, which is a much better user experience.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多