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:

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()
{
$this->output->cache(n);
}
|
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'))
{
$some_object = (object) NULL;
$some_object->test_property = "test date";
$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:
|
$this->db->cache_on();
$result1 = $this->db->query("SELECT * FROM some_table");
$this->db->select("id");
$this->db->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

No comments:
Post a Comment