Codeigniter - SPLessons

CodeIgniter Page Caching

Home > Lesson > Chapter 19
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Page Caching

CodeIgniter Page Caching

shape Description

CodeIgniter Page Caching is one of the important factor to be discussed when working with any application. Normally, there are two important reasons to maintain cache for application pages. They are: Applications Performance: When working with any application, user expects the application to be fast. So CodeIgniter Page Caching is used to improve the performance of the application. Reduce Server Overhead: Servers should keep more efforts when there are multiple requests from the users as the application gets popular. To reduce the burden on the server and decrease the communicating with it, page cache is used.

Create Cache

shape Description

CodeIgniter Page Caching files will be present in the file path application/cache folder. Depending on the pages, the cache can be enabled. During caching process, the time has to be set so that the file remains in cached folder and after that given duration, the file is deleted automatically.

Cache Enabling

Below is the code for enabling the cache of any method of the controller. [php]$this->output->cache($n);[/php] where, $n => desired number of minutes that a page to be cached before refresh is applied.

Cache Disabling

The cache copy of the page will be deleted once the given time expires. Cache copy can be deleted manually also by disabling it. Below is the code for both the modes. [php] // Deletes cache for the currently requested URI $this->output->delete_cache(); // Deletes cache for /foo/bar $this->output->delete_cache('/foo/bar'); [/php]

shape Example

Step-1: Initially, create a controller cache.php and store it in the path application/controller. [php] <?php class Cache extends CI_Controller { public function index() { $this->output->cache(1); $this->load->view('cache_view'); } public function delete_file_cache() { $this->output->delete_cache('cache'); } } ?> [/php] Step-2 : Then create the view file cache_view.php in application/views folder. [html] <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>CodeIgniter View Example</title> </head> <body> CodeIgniter View Example </body> </html> [/html] Step-3 : Then add the routes for above created controller in routes.php file present in application/config and enter the below code at the end of the file. [html] $route['cache'] = 'Cache'; $route['cache/delete'] = 'Cache/delete_file_cache'; [/html] Now, to see the output enter the below URL in the browser. [html]http://localhost/Codeigniter/index.php/cache[/html] Output: Once the URL is visited, a cache file with some random name is created in the path application/cache like below. In order to delete the cache file, enter the below URL. [html]http://localhost/Codeigniter/index.php/cache/delete[/html] Then the output page will be empty without any result.

Summary

shape Key Points

  • CodeIgniter Page Caching improves application working speed.
  • During caching process, the time has to be set so that the file remains in cached folder and after that given duration, the file is deleted automatically.