Simple Usage Analysis of Web Page Caching in CI Framework

  • 2021-11-13 06:53:22
  • OfStack

This article illustrates the simple usage of CI framework web page caching. Share it for your reference, as follows:

CodeIgniter allows you to cache pages for better performance.

Although CodeIgniter is quite efficient, the dynamic content in the web page, the memory CPU of the host and the reading speed of the database directly affect the loading speed of the web page. With web caching, your web pages can be loaded at nearly static speeds, because the output of the program has been saved.

How does caching work?

It can be cached for each individual page, and you can set the update time of each page cache. When the page loads for the first time, the cache is written to a file in the application/cache directory. When you request this page later, you can read the content directly from the cache file and output it to the user's browser. If the cache expires, it will be deleted and refreshed before output.

Turn on the cache

Put the following code into any 1 controller method, and you can turn on the cache:


$this->output->cache($n); // Among them  $n  Is the cache update time (in minutes) 

The above code can be placed anywhere in the method, and the order in which it appears has no effect on the cache, so you can put it wherever you think it is reasonable. 1 Once the code is placed in the method, your page begins to be cached.

Because of the way CodeIgniter stores the cache, only pages output through view can be cached.

If you modify a configuration that may affect page output, you need to manually delete your cache file.

Before writing to the cache file, you need to set the permissions of the application/cache/directory to writable.

Delete cache

If you no longer need to cache a page, you can delete the cache code on the page so that it will not be refreshed after it expires.

Note:

Deleting the cached code does not take effect immediately, and must wait until the cache expires.

If you need to delete the cache manually, you can use the delete_cache() Methods:


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

More readers interested in CodeIgniter can check the topics on this site: "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "php Excellent Development Framework Summary", "ThinkPHP Introduction Tutorial", "Zend FrameWork Framework Introduction Tutorial", "php Object-Oriented Programming Introduction Tutorial", "php + mysql Database Operation Introduction Tutorial" and "php Common Database Operation Skills Summary"

I hope this article is helpful to the PHP programming based on CodeIgniter framework.


Related articles: