2011-08-04 32 views
0

我嘗試使用我的cron php腳本創建下一個發送郵件。 我使用Zend_View來渲染電子郵件模板。 我有5萬用戶,但有3000封電子郵件是用64MB內存限制和7200用128MB創建的。在Zend Framework視圖渲染需要大量內存

foreach ($users as $user) { 
..... 
$text = Mailer::getInstance()->prepareEmailBody($template, $vars); 
..... 
} 

代碼渲染電子郵件

public function prepareEmailBody($template, $templates) 
{ 
    $view = new Zend_View(array('basePath' => './application/views')); 
    $template_file_name = $template . '.phtml'; 
    foreach ($templates as $key => $value) { 
     $view->$key = $value; 
    } 
    $body = $view->render('mails/' . $template_file_name); 
    return $body 
} 

,並使用此方法的請指點如何優化代碼。

+0

您還沒有提供足夠的代碼。你剛剛粘貼的只是一個Zend_View。無論如何,歡迎來到Zend_Framework的世界。這是內存密集型..這就是爲什麼你應該使用APC和緩存一切。 – Layke

+0

我無法緩存呈現的視圖,因爲每個用戶的數據都粘貼在此模板中。 – Dmitro

+0

真正的問題是使用視圖渲染,因爲如果我寫$ text ='一些文本';然後創建所有50k電子郵件。 – Dmitro

回答

1

您可以嘗試使用一個View對象和部分幫助器來代替,這可能會改進一些事情(或可能會使其變慢)。

public function getView() 
{ 
    if (!$this->_view) { 
     $this->_view = new Zend_View(array('basePath' => './application/views')); 
    } 

    return $this->_view; 
} 

public function prepareEmailBody($template, $templates) 
{ 
    $template_file_name = $template . '.phtml'; 

    $body = $this->getView()->partial($template_file_name, $templates); 
    return $body 
} 
+0

此代碼使其變慢。部分幫助器使用渲染方法,但在使用$ view = $ this-> cloneView();我認爲這條線讓它變慢。我無法理解方法渲染中內存泄漏的位置:'public function render($ name) { //使用父私有方法找到腳本文件名稱 $ this - > _ file = $ this - > _ script($名稱); 未設置($ name); //從本地範圍刪除$ name ob_start(); $ this - > _ run($ this - > _ file); return $ this - > _ filter(ob_get_clean()); //過濾器輸出 }'我以爲ob_get_clean()清除內存。 – Dmitro

+0

值得一試!也許相反,你可以嘗試使用一個View對象,但使用render,並在返回$ body之前從視圖中取消設置變量? –

+0

如果使用一個View對象,那麼變量(它具有相同的名稱)將被內存中的新值替換,並且它不會使用更多的內存? – Dmitro