Search

CodeIgniter Page Caching

The CodeIgniter lets you cache your pages in order to achieve maximum performance.
Caching a page will improve the page load speed. By caching your pages, since they are saved in their fully rendered state, you can achieve performance much closer to that of static web pages.

How Does Caching Work?

Caching can be enabled on a per-page basis, cached files are stored in application/cache folder. and you can set the length of time that a page should remain cached before being refreshed. When a page is loaded for the first time, the file will be cached using the currently configured cache engine. On subsequent page loads, the cache file will be retrieved and sent to the requesting user’s browser. If it has expired, it will be deleted automatically.

Enabling Caching

Caching can be enabled by executing the following line in any of the controller’s method.

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

Where $m is the number of minutes, you wish the page to remain cached between refresh. and Or delete after time

Deleting Caches

If you no longer wish to cache a file you can remove the caching tag and it will no longer be refreshed when it expires.

If you need to manually delete the caches, you can use the delete_cache() method:

// Deletes cache for the currently requested URI
$this->output->delete_cache();

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

Example

We have Create a controller called CacheController.php and save it in application/controller/CacheController.php

<?php 
   class CacheController extends CI_Controller { 
	
      public function index() { 
         $this->output->cache(1); 
         $this->load->view('cashe-test-page'); 
      }
		
      public function deleteCache() { 
         $this->output->delete_cache('cachecontroller'); 
      } 
   }

We have create a view file called cashe-test-page.php and save it in application/views/cashe-test-page.php

<!DOCTYPE html> 
<html lang="en">
   <head> 
      <meta charset = "utf-8"> 
      <title>CodeIgniter Cashe View Example</title> 
   </head>
	
   <body> 
      CodeIgniter Cashe View Example 
   </body>
</html>

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

$route['cachecontroller'] 		 = 'CacheController'; 
$route['cachecontroller/delete'] = 'CacheController/deleteCache';

You can type the following URL in the browser to execute the example.

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

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

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

 

Stack Overlode is optimized Tutorials and examples for learning and training. Examples might be simplified to improve reading and learning and understand. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using Stack Overlode, you agree to have read and accepted our terms of use, cookie and privacy policy.