How to speed up PHP page 1/3

Source: Internet
Author: User
Tags apc qoute

Simple data cache technology

I have been optimizing program performance for a while recently. I have an interesting idea and want to discuss it with you.
Cache is a typical application mode based on the "space-for-time" policy and an important method to improve system performance. The use of cache can greatly reduce the number of database operations and significantly reduce the system load to improve system performance. Compared with the page cache, The result set is a type of "Raw data" that does not contain the format information. It has a relatively small amount of data and can be formatted, so it is quite flexible. Because PHP is a script language for "Compiling and executing, to some extent, it also provides a convenient way to use the result set cache-by dynamically include the corresponding data definition code segment. If you create a cache on RamDisk, the efficiency can be further improved. The following is a small sample code for your reference.

<?
// Load data with cache
Function load_data ($ id, $ cache_lifetime ){

// The return data
$ Data = array ();

// Make cache filename
$ Cache_filename = 'cache _ '. $ id.'. php ';

// Check cache file's last modify time
$ Cache_filetime = filemtime ($ cache_filename );

If (time ()-$ cache_filetime <= $ cache_lifetime ){
// ** The cache is not expire
Include ($ cache_filename );
} Else {
// ** The cache is expired

// Load data from database
//...
While ($ dbo-> nextRecord ()){
// $ Data [] =...
}

// Format the data as a php file
$ Data_cache = "<? \ R \ n ";
While (list ($ key, $ val) = each ($ data )){
$ Data_cache. = "\ $ data ['$ key'] = array ('";
$ Data_cache. = "'name' => \" ". qoute ($ val ['name'])." \ ","
$ Data_cache. = "'value' => \" ". qoute ($ val ['value'])." \ ""
$ Data_cache. = "); \ r \ n ";
}
$ Data_cache = "?> \ R \ n ";

// Save the data to the cache file
If ($ fd = fopen ($ cache_filename, 'W + ')){
Fputs ($ fd, $ data_cache );
Fclose ($ fd );
}
}
Return $ data;
}
?>

Applicability:
1. Data is relatively stable, mainly for reading operations.
2. File Operations are faster than database operations.
3. complex data access, large data access, intensive data access, and extremely heavy system database load.
4. Web/DB splitting structure or multiple Web single DB structures.

Unconfirmed problems:
1. Whether the file read/write during concurrent access will cause the locking problem.
2. What is the performance when there are too many data files involved.

Expansion ideas:
1. Generate JavaScript data definition code and call it on the client.
2. I have not thought ......

Hope to discuss it together.
Cache
If you want to improve the performance of your huge PHP application, using cache is also a good method. There are already many caching schemes available, including Zend Cache, APC, and Afterburner Cache.

All these products belong to the cache module ". When a request to the. PHP file appears for the first time, it will save the PHP intermediate code in the memory of the Web server, and then respond to the subsequent request with the "compiled" version. This method can indeed improve the performance of the application, because it minimizes the disk access volume (the code has been read and parsed ), the code runs directly in the memory, greatly improving the server's response speed. Of course, the cache module also monitors changes in PHP source files and caches pages again when necessary to prevent users from generating pages from outdated PHP code. Because the cache module can significantly reduce the server load and improve the response efficiency of PHP applications, it is very suitable for websites with large loads.

How to choose these cache Products
Zend Cache is the commercial software of Zend Technologies, and Zend Technologies is the one mentioned earlier that provides us with PHP engines and free Zend Optimizer. Zend Cache is indeed a good name! For large PHP pages, you can feel that the speed will increase after the first running, and more available resources will be available on the server. Unfortunately, this product is not free, but in some cases it is still worth the money.

Afterburner Cache is a free Cache module from Bware Technologies. Currently, this product is a Beta version. The practice of Afterburner Cache seems to be similar to that of Zend Cache, but its performance improvement (also) cannot be compared with that of Zend Cache, and it cannot work with Zend Optimizer.

APC is short for Alternative PHP Cache. It is another free Cache module from Community Connect. This product is already stable enough for official use, and it seems to be able to greatly speed up response to requests.

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.