2012-04-10 62 views
1

我使用$this->output->cache(n)來緩存網頁,但我無法弄清楚它是如何工作的。我沒有在system/cache文件夾下找到任何緩存文件...並且在我編輯頁面並再次顯示它之後,內容改變,所以看起來這個頁面並沒有真正被緩存。任何人都可以提供幫助嗎? (我用菲爾的模板LIB) 我的代碼:codeigniter輸出緩存不起作用?

function show(){ 

    $this->output->cache(5); 

    $this->load->model('page_model'); 
    $var = $this->uri->segment(3, 0); //get About page 
    $row = $this->page_model->getPage($var); 
    $this->template->title('about') 
        ->set_layout('default') 
        ->set_partial('styles', 'css') 
        ->set('data', $row->body) 
        ->build('about'); 

} 

謝謝!

回答

3

兩件事情,as outlined in the documentation

警告:由於方式笨存儲內容的輸出,如果您是一個view控制器產生顯示緩存纔會工作。

也許不使用「本機」視圖是一個問題?

此外:

注:緩存文件可以寫之前,你必須設置文件權限您的應用程序/緩存文件夾,使得它是可寫的。

您確定您的application/cache目錄具有正確的權限嗎?

+1

+1也許沒有使用「原生」的觀點是一個問題? – Philip 2012-04-10 15:59:37

+0

嗨科林和菲利普,謝謝你的回覆!但是還有其他方法可以使用這種緩存方法嗎?因爲它似乎無法擺脫模板,我不得不使用它來構建網站......另外,如何更改權限?我無法在系統下找到緩存文件夾,但在應用程序下有一個緩存文件夾...對不起,我是CI新手 – Mario 2012-04-10 16:14:11

+0

@Mario:關於你最後一個問題,那是我的錯。該目錄確實是'application/cache'。關於如何讓CI的緩存方法在沒有CI視圖的情況下工作,你必須看一下[Output class](http://codeigniter.com/user_guide/libraries/output.html)的「底層」,找到了解它的工作原理,並根據需要進行擴展。 – 2012-04-10 16:28:55

0

調試此文件,並檢查它的實際寫入高速緩存: 系統/核心/ Output.php

// Do we need to write a cache file? Only if the controller does not have its 
// own _output() method and we are not dealing with a cache file, which we 
// can determine by the existence of the $CI object above 
if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output')) 
{ 
    $this->_write_cache($output); 
} 

這很適合我:

function _output($content) { 
     // Load the base template with output content available as $content 
     $data['content'] = &$content; 

     $this->output->cache(600); 

     $output = $this->load->view('base', $data, true); 

     $this->output->_write_cache($output); 

     echo $output; 
    } 
0

你確定你的應用程序/緩存目錄具有正確的權限?

你你的目錄應用程序/緩存中的cPanel,權限成爲777 OK