CodeIgniter Database Caching

In this tutorial you will learn about the CodeIgniter Database Caching and its application with practical example.

What Is Database Caching In CodeIgniter?

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.

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.

Step3:- Enable caching, either globally by setting the preference in your application/config/database.php file, or manually using following functions.

$this->db->cache_on()

This function is used to enable the caching manually.

$this->db->cache_off()

This function is used to disable the caching manually.

$this->db->cache_delete()

This function is used to delete the cache files associated with a particular page.

Syntax:-

$this->db->cache_delete_all()

This function is used to delete all cache files.

Example 1:-

Example 2:-

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 –

Recommended:-  CodeIgniter Delete Query

Example 3:-

This will clear cache files for the current URI.

 

In this tutorial we have learn about the CodeIgniter Database Caching and its application with practical example. I hope you will like this tutorial.