CodeIgniter - Page Caching

Enable caching, disable caching

CodeIgniter - Page Caching

CodeIgniter - Page Caching

Page caching will improve page loading speed. If the page is cached, it will be saved in a fully rendered state. The next time the server receives a request for a cached page, it will be sent directly to the requested browser.

Cached files are stored in the application/cache folder . Caching can be enabled for every page. When enabling the cache, we need to set the time until which it must remain in the cached folder, and after this period it will be automatically deleted.

Enable caching

Caching can be enabled by executing the following line in any of the controller methods.

$this->output->cache($n);

Where $n is the number of minutes you want the page to remain cached between refreshes.

Disable caching

The cache file is deleted when it expires, but when you want to delete it manually, you need to disable it. You can disable caching by executing the following line.

// Deletes cache for the currently requested URI

$this->output->delete_cache();

 

// Delete cache for /foo/bar

$this->output->delete_cache('/foo/bar');

example

Create a controller named Cache_controller.php and save it to application/controller/Cache_controller.php

Create a view file named test.php and save it to application/views/test.php

 

  

      <metacharset="utf-8">

     

  

        

  

      CodeIgniter View Example

  

        

Edit the route.php file in application/config/rout.php to add the route for the above controller and add the following line at the end of the file.

$route['cachecontroller'] = 'Cache_controller';

$route['cachecontroller/delete'] = 'Cache_controller/delete_file_cache';

Enter the following URL in a browser to run the example.

http://yoursite.com/index.php/cachecontroller

After visiting the above URL, you will see that the cache file for this will be created in the application/cache folder . To delete a file, visit the following URL.

http://yoursite.com/index.php/cachecontroller/delete