CodeIgniter Method of Enabling Cache and Clearing Cache

  • 2021-06-29 10:29:08
  • OfStack

Codeigniter supports caching technology for maximum speed.Although CI is already quite efficient, dynamic content in Web pages, host memory CPU, and database read speed directly affect the loading speed of web pages.With web caching, your pages can load almost static pages because they save the program output to your hard disk.

How does caching work?

CI supports separate caching of each page and allows you to set cache update times.When a page is first loaded, the cache file is saved to the application/cache folder.The next time you visit, the system reads the cache file directly and returns it to the user's browser.If the cache file expires, it will be deleted and regenerated.
Note: The Benchmark tag is still available for pages that use the cache.

Start Cache

To enable caching, simply put the following code into the method (function) of any of your controllers (controller):
$this->output->cache(n);

n is the number of minutes you want to cache updates.You can use m/60 to be precise to seconds, such as 1/60, to be precise to 1 second
The above code can be placed in any function.The order in which he appears has no effect on the cache, so place it where you think it's most logical.1Once the above code is placed in the controller's method, the page will be cached.
Warning: Due to the way CI stores cached files, only the output from the view file can be cached.
Note: Make sure the application/cache folder is writable before the cache file is generated.

Clear Cache

If you no longer want to use the cache, just delete the above code from your controller.Note: This does not immediately make the cache file disappear, it will automatically expire and be deleted.If you want to delete those files immediately, you must do it yourself.

Related articles: