dow5001 2014-02-20 09:02
浏览 44
已采纳

PHP脚本耗尽内存

Hi im trying to code a funktion that will import trailers from a remote server The code is matching product in our magento store with the trailers on the trailer server . The problem is that the script eating up all memory. is there any way of optimize this to not consume memory .

here are my function

    function getTrailers() {

    echo "<pre>
";
    $this->_acquireLock();

    $_productCollection = Mage::getModel('catalog/product')->getCollection()

      ->addAttributeToFilter('attribute_set_id', array('eq' => '9'));
     //->addAttributeToFilter('Sku', array('eq' => '843594'));


    $this->_logLine(sprintf("Starting import of Trailers" ));
    $i=0;
        $server = "http://trailer.server.com/trailers";
        foreach($_productCollection as $product) {
        $thispro  =  Mage::getModel('catalog/product')->load($product->getId());
        $attributeValue = $thispro->getFaktaId();

            //echo memory_get_usage() . "<br>
";
            //echo memory_get_peak_usage() . "<br>
";

        if($thispro->getMainTrailer()=="") {
            if($attributeValue != "") {
                $im = $attributeValue.".mp4";
                    $url = $server.$im;


        $exist = $this->file_exists_remote($url);


        if($exist) {
            $i++;
            $product->setMainTrailer($url);
            $product->save();
        } else {

            }
        }
    }
}
    $this->_logLine(sprintf("Imported %d Trailers...", $i));
    $this->_releaseLock();

    echo "</pre>
";

}

  • 写回答

4条回答 默认 最新

  • douzhou7656 2014-02-20 09:42
    关注

    Your main problem is that you keep collection of products you never use. Actually you don't need these products. You only need their IDs.

    So you have to edit your code like this:

    function getIds() {
        return Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToFilter('attribute_set_id', array('eq' => '9'))
            ->getAllIds();
    }
    
    ...
    foreach(getIds() as $id) {
        $thispro = Mage::getModel('catalog/product')->load($id);
        ...
        // Further you have to replace all $product occurences with $thispro
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制