CodeIgniter Database Caching
CodeIgniter framework comes with built-in dynamic database caching mechanism. In CodeIgniter, when database caching is enabled, a web page is loaded first time , corresponding query result stored as a cache file. Next time when the same page is requested it is served using cache file instead of accessing your database again. This way it reduces the database load and result in performance gain. Database caching is only applicable with SELECT queries.
1)Enabling Caching In CodeIgniter:-
Before you start using cache, you must enable it using following steps –
Step1:- Create a directory with write permission on your server where the cache files can be stored.
Step2:- Set the path to your cache folder in your application/config/database.php file.
$db['default']['cachedir'] = 'application/cache';
Step3:- Enable caching, either globally by setting the preference in your application/config/database.php file, or manually using following functions.
This function is used to enable the caching manually.
This function is used to disable the caching manually.
This function is used to delete the cache files associated with a particular page.
// Turn caching on $this->db->cache_on(); $query = $this->db->query("SELECT * FROM employee_master"); // Turn caching off for this one query $this->db->cache_off(); $query = $this->db->query("SELECT * FROM employee_master WHERE emp_ID = '$id'"); // Turn caching back on $this->db->cache_on(); $query = $this->db->query("SELECT * FROM tbl_payroll");
If you are viewing a page at example.com/index.php/employee/profile, the caching system will put all cache files associated with it in a folder called employee+profile. Cache files for this page can be deleted as following
This will clear cache files for the current URI.