CodeIgniter : Dynamic Database Query Caching

Purpose: This article is intended to show you how you can disable query caching for particular methods and enable it globally for all methods.

In CodeIgniter there is default option for query caching. If you enable query cache in database.php, by passing $db['default']['cache_on'] = TRUE; It will enable query cache for all controllers.

codeigniter database query cachingYou might be thinking that there are already defined methods to on/off caching. But what if you have large application with hundreds of methods. You won’t like to open every file and put  $this->db->cache_on() and  $this->db->cache_off() for all functions.

Thus setting cache_on to TRUE is only option. What if you want to disable caching for particular method call ? Here is a solution.

Hacking Core for Dynamic Query Caching

In your following files make following changes according to git embed script below.


  • In config file, there is an array containing list of URL’s to exclude from query caching.
  • In DB_cache.php file there is a ‘write’ method which writes database to folder, there we put an condition that if URL found from that array then ‘return false;’. So it won’t save to that cache folder.


Thank you.


Shyam Makwana has written 21 articles

I am PHP developer with passion of exploring IT world for new technologies. I do projects in WordPress, Joomla, Magento, Drupal, Open Cart, Code Igniter, MVC and CorePHP. Checkout my profile here to know more about me.