Question

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.

Was it helpful?

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

OTHER TIPS

I think this can be verified enabling the Profiler in your controller and check if any query is done. Make sure this is enabled only for your IP if you're using it in Production environment.

$this->output->enable_profiler(TRUE);

-- EDIT 1 --

This will be visible only once. Soon after the cached page is stored, the profiles result won't be visible again (so you might wanna delete the file and refresh the page).

-- EDIT 2 --

You might also use:

log_message('info', 'message');

inside your model, then change in config.php, $config['log_threshold'] to 3 and check the log file.

-- EDIT 3 --

For sure the selection will be done unless you have enabled the database cache. In this case, in the cache folder you'll see the database selection cached.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top