Two ways to enable database caching in Code Igniter

Database caching is a good feature to enable/activate if you feel that your database is heavily taxed and you want to lessen some of the load on it.

Basically the concept is that Code Igniter serializes result sets generated by the database and stores them in the “application/cache” folder of your Code Igniter application. Due to this reason the folder in question should be writable otherwise this will not work.

Code Igniter serializing result sets and saving into the cache folder

There are two ways to enable database caching in Code Igniter:

1. Open “application/config/database.php” and search for the following line in that file

‘cache_on’ => FALSE,

Change FALSE to TRUE and save the file. This will instruct Code Igniter to enable caching. Please note only SELECT queries will be cached and no DML or DCL directive will be cached.2.

2.  Open any controller file in your “application/controllers” directory. If you want to cache specific database logic like SELECTing all countries, countries or makes then you just need to put in the following statement before any code which fetches the data from the database

    /* Enables database caching */ 
    $this->db->cache_on(); 

    $data['info'] = $this->country_model->getCountries();       

    /* Disables database caching */ 
    $this->db->cache_off(); 

Both points do the same thing i.e. cache the result sets. Only difference is that point 1 enables it for the complete website where as point 2 allows you to cache particular sections of your code.

It all depends on what you require.

Hope the above helped.




Please follow and like us:

Be the first to comment

Leave a Reply

Your email address will not be published.


*