& Nbsp; due to its powerful and scalable nature, PHP has developed considerably in recent years, PHP has an absolute speed advantage over traditional ASP websites. 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 amount of data on websites, we are eager to make PHP more powerful and scalable. in recent years, PHP has developed considerably. 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.
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.
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 developers generate such objects yet. it is indeed unnecessary. ASP. the NET page cache technology uses viewstate, while the cache is 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:
Class cache {
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 cache 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. = (! Emptyempty ($ _ SERVER [QUERY_STRING])? ? . $ _ SERVER [QUERY_STRING]:;
}
Return $ url;
}
Function warn ($ errorstring ){
Echo"
Error:".$errorstring."
";
}
Function cache_page ($ pageurl, $ pagedata ){
If (! $ Fso = fopen ($ pageurl, w )){
$ This-> warns (the cached file cannot be opened.); // trigger_error
Return false;
}
If (! Flock ($ fso, LOCK_EX) {// LOCK_NB, locking
$ This-> warns (the cache file cannot be locked.); // trigger_error
Return false;
}
If (! Fwrite ($ fso, $ pagedata) {// write byte stream, serialize writes to other formats
$ This-> warns (the cache file cannot be written.); // trigger_error
Return false;
}
Flock ($ fso, LOCK_UN); // release lock
Fclose ($ fso );
Return true;
}
Function display_cache ($ cacheFile ){
If (! File_exists ($ cacheFile )){
$ This-> warn (the cache file cannot be read.); // trigger_error
Return false;
}
Echo:. $ cacheFile;
// Return unserialize (file_get_contents ($ cacheFile ));
$ Fso = fopen ($ cacheFile, r );
$ Data = fread ($ fso, filesize ($ cacheFile ));
Fclose ($ fso );
Return $ data;
}