How to clear cache in Codeigniter via inbuilt functions

By | August 8, 2020

What is caching in Codeigniter?

In Codeigniter framework, caching is a technique that help developers to cache pages in order to acheive maximum performance from the cached web page. Although, Codeigniter is very fast in itself if we compare it with other frameworks like YII, Zend etc. because it is very less in size, less memory consumption and processing cycle which increases the page load speed. And, if you are enabling caching system in your project, then your pages will be served to the visitors like static pages.

How does caching works in Codeigniter?

Caching can be enabled on the basis of per page and a developer can set maximum number of time to expire cached page. When we enable cache for a page, then cache file will be written into 'application/cache' directory and when visitor visits the page then cached page will be served and if the cache file is expired then the cache file will be deleted from the cache direcctory and a new cache file will be created then the cached file will be served to the visitor.

How to enable caching in Codeigniter?

To enable caching in your codeigniter project, you will have to add below code into your controller file.

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

In above code, $n is maximum number of time to expire cached file.

How to clear cache in codeigniter?

To clear or delete cache from 'application/cache' directory, you have call below methods.

$this->output->delete_cache(); // will delete cache for current request URI
$this->output->delete_cache('/foo/bar'); // will delete cache for given path.

I hope this trick will help you.

Category: PHP

Leave a Reply

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