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.

/application/config/config.php
/system/database/DB_cache.php

  • 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.

Enjoy…!!!

Shyam has written 29 articles

Shyam is senior full stack developer, who loves to explore new technologies and work on them. He's passionate about coding so can code 24/7. He uses PHP as a backend programming language.

He knows Laravel, MySQL, AngularJS, ReactJS, Redis, Kubernetes, Git, CodeIgniter, PHP, MVC pattern, Lodash, jQuery, VanilaJS, Teamcity and many other technologies and tools.

Shyam writes notes and hacks on his blog (https://shyammakwana.me). In spare time he can be found @ StackOverflow or crafting any new open source application.

Passionate Programmer and Meditator #PERIOD.