Deleting Codeigniter Webpage Cache

Deleting Codeigniter Webpage Cache

In this post we will show you Deleting Codeigniter Webpage Cache, hear for Deleting Codeigniter Webpage Cache we will give you demo and example for implement.

Codeigniter Output class provides a way to create HTML cache of the view files loaded through controller.

eg: If the url is yourdomin.com/CONTROLLER/FUNCTION23

To cache the view loaded by FUNCTION23, use following code inside FUNCTION23

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

60 in above cache function is 60 minute. Cached file is stored here application/cache.
How to delete this cache file

$this->output->delete_cache('controller/function22');

Make sure you don’t include any slash before or after, because the path is MD5 Encoded.

Delete cache when using custom routing
Consider a case, where you are using custom routing (config/routes.php) for a page, when a user requested URL is

yourdomin.com/CONTROLLER/FUNCTION100

and you are routing it to a different controller/function200.

To Delete this cached file you need to do as follows

$this->output->delete_cache('controller/function200');

How to delete the home page landing page url

If you are setting routes.php

$route['default_controller'] = 'MyController/MyHomePage' 

which will be shown to user when he enter yourdomin.com

Add the following code to system\core\Output.php of codeigniter

public function delete_cache_path($site_uri = '')
{
	$CI =& get_instance();
	$cache_path = $CI->config->item('cache_path');
	if ($cache_path === '')
	{
		$cache_path = APPPATH.'cache/';
	}

	if ( ! is_dir($cache_path))
	{
		log_message('error', 'we are unable to find cache path: '.$cache_path);
		return FALSE;
	}

	$cache_path .= md5($site_uri);

	if ( ! @unlink($cache_path))
	{
		log_message('error', 'we are unable to delete cache file for '.$site_uri);
		return FALSE;
	}

	return TRUE;
}

Hope this code and post will helped you for implement Deleting Codeigniter Webpage Cache. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org

Leave a Comment

Your email address will not be published. Required fields are marked *

1  +  7  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US