简体   繁体   English

Laravel帮助页面加载缓慢

[英]Laravel helper page loading slow

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: 我有一个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. 当我转到所有产品(890个产品)所在的类别时,页面加载缓慢。 If I remove Helper::getPRBcurr() all is good. 如果删除Helper::getPRBcurr()一切都很好。 How I can fix this problem? 我该如何解决这个问题? Problem in function file_get_contents_curl 500ms timeout . 函数file_get_contents_curl 500ms timeout

Calling Helper::getPRBcurr() directly with the view file is a bad idea. 直接用视图文件调用Helper::getPRBcurr()是一个坏主意。 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. 将其移至异步调用将快速呈现页面,然后等待数据提取,这将带来更好的用户体验。

I believe the reason this method call is always slow is because your cache is not working. 我相信此方法调用总是很慢的原因是因为您的缓存无法正常工作。 The cache key you're reading is different from the one you're storing, so in effect you're always running a HTTP request, every time. 您正在读取的缓存密钥与要存储的密钥不同,因此实际上每次您始终在运行HTTP请求。

$currency = Cache::get('currency_prb_ee_agroTimeout');

And when setting the cache: 并且在设置缓存时:

Cache::put('currency_prb_agroTimeout', $currency, 720);

Notice the keys are different, currency_prb_ee_agroTimeout vs currency_prb_agroTimeout . 注意密钥不同, currency_prb_ee_agroTimeoutcurrency_prb_agroTimeout

However, as mentioned in the answer given by @btl running a HTTP request when serving a request will lead to a bad user experience and you should use AJAX to load in the result. 但是,正如@btl给出的答案中提到的那样,在为请求提供服务时运行HTTP请求会导致不良的用户体验,您应该使用AJAX加载结果。 By both fixing your caching issue and implementing this, everything would function significantly faster. 通过解决缓存问题和实施此问题,所有功能将显着加快运行速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM