I. Introduction
PHP, a web design scripting language that has emerged in recent years. Due to its powerful and scalability, php has developed considerably in recent years. Compared with traditional asp websites, it has an absolute advantage in speed. If you want mssql to convert 60 thousand pieces of data to php, it will take 40 seconds, and asp will take less than 2 minutes. however, due to the increasing number of website data, we are eager to call data more quickly. We do not need to drop data from the database every time. We can choose from other places, such as a file, or a memory address, which is the Cache Technology of php.
Ii. In-depth Analysis
In general, the purpose of caching is to put data in one place to make access faster. There is no doubt that the memory is the fastest, but can hundreds of MB of data be stored in the memory? This is not realistic. Of course, sometimes it is temporarily stored as a server cache, for example, if the ob_start () cache page is enabled, the page content will be cached in the memory before the file header is sent, knowing that the page output will be clear automatically or wait for the ob_get_contents to return, or be cleared by the ob_end_clean display, which can be used well in the generation of static pages and can be well reflected in the template, I have discussed this article in depth: it is a temporary way to generate static pages in PHP, but it is not a good solution to our problem.
In addition, there is an object application in asp that can save common parameters. This is also a bit of cache. However, in php, I have not seen such an object produced by developers so far. Indeed, there is no need for .asp.net's page cache technology to use viewstate, while cache is a File Association (not necessarily accurate), the file is modified, and the cache is updated, if the file is not modified and does not Time Out (note 1), read the cache and return the result. This is the idea. Let's look at the source code:
<? Php
Class cache {
/*
Class Name: cache
Description: control to cache data, $ cache_out_time is a array to save cache date time out.
Version: 1.0
Author: Lao Nong cjjer
Last modify: 2006-2-26
Author URL: http://www.cjjer.com
*/
Private $ cache_dir;
Private $ expireTime = 180; // The cache time is 60 seconds.
Function _ construct ($ cache_dirname ){
If (! @ Is_dir ($ cache_dirname )){
If (! @ Mkdir ($ cache _ dirname, 0777 )){
$ This-> warn ('the cached file does not exist and cannot be created. You need to create it manually .');
Return false;
}
}
$ This-> cache_dir = $ cache_dirname;
}
Function _ destruct (){
Echo 'cache class bye .';
}
Function get_url (){
If (! Isset ($ _ SERVER ['request _ URI ']) {
$ Url = $ _ SERVER ['request _ URI '];
} Else {
$ Url = $ _ SERVER ['script _ name'];
$ Url. = (! Empty ($ _ SERVER ['query _ string'])? '? '. $ _ SERVER ['query _ string']: '';
}
Return $ url;
}
Function warn ($ errorstring ){
Echo "<B> <font color = 'red'> error: <pre>". $ errorstring. "</pre> </font> </B> ";
}
Function cache_page ($ pageurl, $ pagedata ){
If (! $ Fso = fopen ($ pageurl, 'w ')){
$ This-> warns ('cache file cannot be opened. '); // trigger_error
Return false;
}
If (! Flock ($ fso, LOCK_EX) {// LOCK_NB, locking
$ This-> warns ('unable to lock the cache file. '); // trigger_error
Return false;
}
If (! Fwrite ($ fso, $ pagedata) {// write byte stream, serialize writes to other formats
$ This-> warns ('unable to write the cache file. '); // trigger_error
Return false;
}
Flock ($ fso, LOCK_UN); // release lock
Fclose ($ fso );
Return true;
}
Function display_cache ($ cacheFile ){
If (! File_exists ($ cacheFile )){