PHP Cache technology application-PHP Tutorial

Source: Internet
Author: User
Describes in detail the application of the cache technology in PHP. 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, has an absolute advantage in speed.

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.Cache technologyThat is, the Cache technology.


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 static page generation and can be well reflected in the template, I have discussed this article in depth:

PHP static page generationThis is a temporary approach, 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 updated. the file is not modified and does not Time Out (Note 1), Read the cache, and return results. this is the idea. let's look at the source code:

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 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. = (! Empty ($ _ 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;
}

Function readData($cacheFile=default_cache.txt ){
$ CacheFile = $ this-> cache_dir. "/". $ cacheFile;
If (file_exists ($ cacheFile) & filemtime ($ cacheFile)> (time ()-$ this-> expireTime )){
$ Data = $ this-> display_cache ($ cacheFile );
} Else {
$ Data = "from here wo can get it from mysql database, update time is ". Date (l dS of f y h: I: s )."The expiration time is: ". date (l dS of f y h: I: s A, time () + $ this-> expireTime )."----------";
$ This-> cache_page ($ cacheFile, $ data );
}
Return $ data;
}



}
?>

Next I will interrupt this code and explain it line by line.
3. procedural dialysis

This cache class is named cache and has two attributes:

Private $ cache_dir;
Private $ expireTime = 180;

$ Cache_dir is the parent directory of the relative Website Directory of the cached file, $ expireTime ( Note 1) Is the cache data expiration time, mainly in this way:
When the data or file is loaded, the system first determines whether the cached file exists or does not, and returns false. The last modification time and cache time of the file are not greater than the current time, if the cache size is large, it indicates that the cache has not expired. if it is small, false is returned. when false is returned, raw data is read, written to the cache file, and data is returned.,

Next let's look at the program:

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;
}

When the class is started for the first time, construct the default function with the parameter cache file name. if the file does not exist, create a folder with the editing permission and throw an exception when the creation fails. set the $ cache_dir attribute of the cache class to the folder name.

...

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.