codeignitercachingpage-cachingoutput-caching

Codeigniter output cache: what parts of the called controller function get executed?


One of the main purposes of caching is to save resources and not do things like hit your database every request. In light of this, I'm confused by what all Codeigniter does in a controller when it encounters a cache() statement.

For example:

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

$data=$this->main_model->get_data_from_database();

$this->load->view("main/index", $data);

I realize that the cached main/index html file will show for the next 5 minutes, but during these 5 minutes will the controller still execute the get_data_from_database() step? Or will it just skip it?

Note: the Codeigniter documentation says you can put the cache() statement anywhere in the controller function, which confuses me even more about whats getting executed.


Solution

  • I can answer my own question. NOTHING in the controller function other than the cached output gets executed during the time in which the cache is set.

    To test this yourself, do a database INSERT or something that would be logged somehow (e.g. write to a blank file).

    I added the following code below my cache() statement and it only inserted into the some_table table the first time I loaded the controller function and not the 2nd time (within the 5 minute span).

    $this->db->insert('some_table', array('field_name' => 'value1') );