【问题标题】:codeigniter output cache doesn't work?codeigniter 输出缓存不起作用?
【发布时间】:2016-06-15 22:04:16
【问题描述】:

我正在使用$this->output->cache(n) 来缓存网页,但我不知道它是如何工作的。我在system/cache 文件夹下没有找到任何缓存文件...在我编辑页面并显示之后再一次,内容发生了变化,所以页面似乎没有真正被缓存。有人可以帮忙吗? (我正在使用 phil 的模板库) 我的代码:

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');

}

谢谢!

【问题讨论】:

    标签: codeigniter caching web


    【解决方案1】:

    两件事,as outlined in the documentation:

    警告:由于 CodeIgniter 存储输出内容的方式,只有在您使用 view 为控制器生成显示时,缓存才会起作用。

    也许不使用“原生”视图是个问题?

    另外:

    注意:在写入缓存文件之前,您必须设置应用程序/缓存文件夹的文件权限,使其可写。

    您确定您的application/cache 目录具有正确的权限吗?

    【讨论】:

    • 您好科林和菲利普,感谢您的回复!但是还有其他方法可以使用这种缓存方法吗?因为似乎我无法摆脱模板,我必须使用它来构建网站......另外,如何更改权限?我在系统下找不到缓存文件夹,但应用程序下有一个缓存文件夹...对不起,我是 CI 新手
    • @Mario:关于你的最后一个问题,那是我的错误。该目录确实是application/cache。关于如何在没有 CI 视图的情况下使 CI 的缓存方法工作,您必须查看 Output class 的“幕后”,了解它是如何工作的,并根据需要进行扩展。
    • @Mario:我忘了解决你的另一个问题——“如何更改权限?”这取决于您使用的平台,但您可以通过Googling for it 轻松找到适用于 Windows、Linux 等的说明。希望对您有所帮助。
    【解决方案2】:

    调试这个文件并检查它是否真的在写入缓存: 系统/核心/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;
        }
    

    【讨论】:

      【解决方案3】:

      您确定您的应用程序/缓存目录具有正确的权限吗?

      你你到 cpanel 中的目录 application/cache ,权限变成 777 就可以了

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-27
        • 2018-01-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多