The caching mechanism and usage of CodeIgniter. Database cache 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 database cache on the server
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.
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... on the server...