How To Work With Codeigniter Caching In PHP

 How To Work With Codeigniter Caching In PHP  

For websites/web applications, caching plays a very important role. Today, in this tutorial, I will explain how we can start using codeigniter caching techniques for improving our web application to a new level. I am assuming you have some codeigniter basics knowledge already and have experience to write simple codes using Codeigniter standards.

Why Caching?

codeigniter caching
“I am coding and my code is working well, why the hell I need to implement caching techniques?”
Well, this kind of thinking is natural. To understand the main concern behind, we will need to come out from the domain of “only coding the functionality” to a little higher level of thinking. Two most important factors are as follows:

Applications Performance: It’s vital because it’s the visitors (your customers) are experiencing it, to whom your application is for. If your application is giving your users a boring/slow performance result, its gonna be a lot noticeable/annoying to them. Faster web application is always brings peace to a visitor’s mind.

Reduce Server Overhead: The more your web application/site becomes popular, the more your server have to work more. It’s always better to think about lessen the server’s computing/communication load as much as possible. Otherwise, for overloaded or spike traffic, server may crash often. You can have a highly configured server to handle this, but, will cost a lot which you don’t need to at all. As this doesn’t happen always, most of the time, server capability will remain unused, thus wasted.

Caching can be a good answer to these. We will see, how codeigniter caching can help in these regards to our web sites/applications.

Read The Complete PHP CodeIgniter Tutorials Series By

How Many Different Type Of Caching Can be Implemented?

In a traditional web application architecture, there are few layers where caching techniques can be implemented such as:

  • Browser caching
  • View page caching
  • Object caching
  • Database caching

Luckily most of them can be controlled by your server application. lets dig into them one by one.

CodeIgniter Cache Control Technique For Browser:

By default, web browsers do cache the results which our server generates.Here, we can tell the browser not to cache. Usually, we won’t want to prevent browser from caching except some special cases where our outputs are updated very often. So, instead of setting this commands as global/base controller, lets practice to use it only in the controllers/controller functions where it’s really needed. You can use the following code snippets only when you really want to prevent caching for browsers:

$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache"); 

CodeIgniter Caching For View Pages:

web cache workflow
OK, so let us move one level more deeper. We now wan to cache the output codeigniter is generating for visitors. That means. The result of ‘controller function+view code’ combined. Well, if you are using any third-party view render-er such as smarty template engine, then you don’t have to worry at all, it will take care of the view caching. If not using any, then codeigniter’s internal support is really very helpful. Use the code example as below in the controller function, where you want caching:

function my_controller_function()
       //your controller codes goes her.....

       //n = number of minutes to cache output

       //your controller codes goes here....

Notice that, I have mentioned in the code that, you can set this in any place of your controller function. It doesn’t matter where you place it, it will cause caching the entire controller’s functionality. Check out codeigniter documentaiton on web page caching for more details description.

CodeIgniter PHP Objects Caching:

Well, now we are about to use some advance level codeigniter caching techniques. Codeigniter can internally interpret with other third-party cache drivers, to get the most out of your application. This usually includes support for APC, Memcache and file/disk based cache. All your caching code will be similar, you just will have to change the driver identifier to the proper one. It makes it very easy/efficient to switch between cache drivers.

Moving application to new server which doesn’t support any caching?
Well, you might be wondering, what we will do in such cases. Would we change code to disable all cache? No, we won’t need so. For this purpose, Codeigniter has support for “Dummy Cache” mode. It doesn’t cache anything really, but your code can be unchanged. Isn’t it cool?

Lets see simple use of PHP object caching in Codeigniter. A typical caching code snippet would be as follows:

function my_function()
     if ( ! $cache_data = $this->cache->get('cache_key'))
          //here goes your codes...
          $some_object = (object) NULL;
          $some_object->test_property = "test date";
          // Save into the cache for 2 minutes
          $this->cache->save('cache_key', $some_object, 120);
          $cache_data = $some_object;
     return $cache_data; 

Whenever the above function is being called, it will return data from its cache up to 2 minutes, and then cache will expire. Then it will execute all logic you entered to generate new objects. This can be placed anywhere inside controller functions,model functions or in libraries, totally depends on your requirements. Check Codeigniter documentation on caching library for more in depth knowledge.

CodeIgniter Caching For Database Results:

Yeah, Codeigniter does provide a very nice way to cache the database query results as well! It’s very helpful to balance the load on your database by caching the ‘most frequently used’/’less frequently updated’ database contents. If you are using some kind of third party database abstraction layer such as doctrine orm with codeigniter, then you won’t need to worry about this level of caching as it’s already provided by them. But, if you are not, and are using codeigniter active record or so, then you can use this caching technique to boost performance of your application.

Luckily, using this is also as simple as other codeigniter functionality. We just need to provide command to cache result when we are performing some database operations:


$result1 = $this->db->query("SELECT * FROM some_table");

$result2 = $this->db->get();

as soon as we give the instruction to cache ‘on’, it will start caching all the results following it. If you need somewhere to disable caching, just use ‘cache_off’ function before the execution. See complete documentation on database caching for more information.

Hope this brief codeigniter caching tutorial will help you understand and start to improve your web application to be more robust/scalable. Do not hesitate to ask if you have any questions/comments. Happy coding :)


  1. says

    Thank for good tutorial.

    But I have one problem in cahing file. When I update template, Cache didn’t update. It update when cache time expire.
    Please help me how can I update cache when I update template.

Leave a Reply