Deleting Codeigniter Webpage Cache

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

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

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

$this->output->cache(50);
50 in above cache function is 50 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

http://www.example.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 example.com

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

    public function delete_cache_path($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', 'Unable to find cache path: '.$cache_path);
            return FALSE;
        }

        $cache_path .= md5($uri);

        if ( ! @unlink($cache_path))
        {
            log_message('error', 'Unable to delete cache file for '.$uri);
            return FALSE;
        }

        return TRUE;
    }




Hope this helps.

1 Comments

Previous Post Next Post