1

I'm using $this->output->cache(n) to cache webpage, but i cannot figure out how does it work.. I didn't find any cache files under system/cache folder...and also after I edit the page and show it again, the content changes, so it seems that the page is not really cached. Can anyone give a help? (i'm using phil's template lib) my code:

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

}

THANKS!

3 Answers 3

3

Two things, as outlined in the documentation:

Warning: Because of the way CodeIgniter stores content for output, caching will only work if you are generating display for your controller with a view.

Perhaps not using the "native" views is an issue?

Additionally:

Note: Before the cache files can be written you must set the file permissions on your application/cache folder such that it is writable.

Are you sure your application/cache directory has the correct permissions?

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Colin and Philip, thank you for your reply! But is there other ways to use this cache method? because it seems that i cannot get rid of the template, i have to use it to build the site... Also,how to change the permission? I cannot find cache folder under system, but there is a cache folder under application... Sorry, i'm new to CI
@Mario: Regarding your last question, that was my mistake. The directory is indeed application/cache. Regarding how to make CI's cache method work without CI views, you'd have to take a look "under the hood" of the Output class, find out how it works, and extend it as needed.
@Mario: I forgot to address your other question - "How to change permissions?" It depends on what platform you're on, but you can easily find instructions for Windows, Linux, etc. by Googling for it. Hope that helps.
1

Debug this file and check it's actually writing the cache: system/core/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);
}

This works well for me:

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;
    }

Comments

1

Are you sure your application/cache directory has the correct permissions?

you you to directories application/cache in cpanel , permissions become 777 is OK

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.