CodeIgniter cache mechanism and usage

Source: Internet
Author: User
Tags codeigniter

Database cache

The database cache class allows you to save database query results in text files to reduce database access.

Three steps are required to activate the cache:

  • Create a writable directory on the server to save the cached files.
  • Set the directory in the application/config/database. php file.
  • To activate the cache feature, you can set global options in the application/config/database. php file, or manually set them in the method below this page.

Once activated, the cache automatically occurs when a page containing database queries is loaded.

When the page is browsed, the query Cache System of CodeIgniter can be dynamically executed. If the cache feature is activated, the database query result object will be serialized and saved in the text file on your server when the page is loaded for the first time. When the page is loaded again, the cache file will replace the database query. In this way, your database usage will be reduced to 0 on the cached page.

Only read-type (SELECT) queries are cached, because only such queries generate result sets. Write-type (INSERT, UPDATE, and so on) queries are not cached because no result set is generated.

The cached file will not expire. Unless you delete it, any cached query will always exist. The cache system allows you to clear by page or clear all the caches. In general, you can use the following function to clear the cache when some events (such as adding data to the database) occur.

Whether the cache can obtain performance gains depends on many factors. If you have a database with low load and high optimization, you may not see performance improvement. If your database is in mass use, you may see performance improvements after caching, provided that your file system does not have much overhead. In some cluster server environments, this may occur because file system operations are too frequent and the cache cannot be correctly generated. In a single server in a shared environment, high-speed cache may be helpful. Performance improvement depends on your database. This depends on your situation.

CI places the results of each query in its own cache file. According to your controller function, the cache file set will be further organized into sub-directories. If it is accurate, the subdirectory name is determined by the first two sections of your URI (control class name and function name. For example, suppose you have a controller blog and a comments function, which includes three queries. The cache system will create a directory named blog + comments and create three cached files in this directory. When you dynamically query based on the information on the URI (for example, using pagination), each query instance will create its own cache file. Therefore, after many queries, the number of cached files may be more than the number of queries.

Because the cache file does not expire, you need to write the code for deleting the cache operation in your application. For example, assume that you have a blog that allows users to post comments. Whenever a new comment is submitted, you must delete the cache file and controller functions in a controller method. You will find that the following two deletion functions can help you clear data.

You can manually set the cache switch. This function is useful if you want to retain some queries that are not cached. For example:

// Enable the cache switch $ this-> db-> cache_on (); $ query = $ this-> db-> query ("SELECT * FROM mytable "); // make the following query not cached $ this-> db-> cache_off (); $ query = $ this-> db-> query ("SELECT * FROM members WHERE member_id = '$ current_user '"); // Turn caching back on $ this-> db-> cache_on (); $ query = $ this-> db-> query ("SELECT * FROM another_table ");

Delete cached files and specific webpages. If you need to clear the cache, update your database.

The cache system creates a subdirectory corresponding to the accessed URL in the cache directory, and stores the cached file in that subdirectory. the main directory of the cache is in application/config/database. cache directory set in php. for example, if you are browsing the page with the address example.com/index.php/blog/comments, the cache system will put all generated cache files in a folder named "blog + comments. if you want to delete the cache file corresponding to this example, You need to execute the following code:

$this->db->cache_delete('blog', 'comments');

$ This-> db-> cache_delete ('blog ', 'comments'), which does not take effect during actual testing. I don't know why, and I don't know if it's a small bug? However, the following $ this-> db-> cache_delete_all () is acceptable and there is no problem.

If you do not use any parameters, the current URI settings determine when to clear/update the cache.

Clear all cached files. Example:

$this->db->cache_delete_all();
Webpage Cache

Codeigniter supports the caching technology to achieve the fastest speed. Although CI has been quite efficient, factors such as the dynamic content in the webpage, the memory CPU of the host, and the read speed of the database directly affect the loading speed of the webpage. Depending on the Web cache, your web page can load almost static Web pages because they save the results output by the program to the hard disk.

CI supports separate cache for each page and allows you to set the cache Update time. When a webpage is loaded for the first time, the cached file is saved to the application/cache folder. During the next visit, the system will directly read the cached file and return it to the user's browser. If the cached file expires, it will be deleted and regenerated.

To enable the caching function, you only need to put the following code in the function of any controller:

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

N indicates the number of minutes you want to cache updates. You can use m/60 to get accurate to the second, for example, 1/60, Which is accurate to 1 second. The above code can be put in any function. The order of appearance does not affect the cache, so place it where you think it is most logical. Once the above Code is put into the Controller method, the page will be cached.

Because CI stores cached files, only the output of view files can be cached. Before a cached file is generated, make sure that the application/cache folder is writable.

If you no longer want to use the cache, you only need to delete the above Code from your controller. Note: This will not cause the cached file to disappear immediately. It will automatically expire and be deleted. If you want to delete those files immediately, you must do it yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.