How to improve PHP speed 1th/3 page _php Tips

Source: Internet
Author: User
Tags apc qoute server memory zend
Simple Data caching technology

Recently done a while to optimize the performance of the program, there is a more interesting idea, would like to propose to communicate with you.
Cache is a typical application mode of "Space Change Time" strategy, and it is an important method to improve system performance. The use of caching can greatly reduce the number of database operations and significantly reduce the system load and improve system performance with large traffic. Compared to the page cache, the result set is a "raw data" does not contain format information, the amount of data is relatively small, and can be formatted, so it appears quite flexible. Because PHP is a scripting language that executes on one side, it also provides a fairly convenient method of result set caching-using caching in a way that dynamically include the corresponding data definition snippet. If you build a cache on "RamDisk", the efficiency should be further improved. The following is a short sample code for 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 (the 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;
}
?>

Applicable situation:
1. The data is relatively stable, mainly for reading operations.
2. File operations are faster than database operations.
3. Complex data access, large amount of data access, intensive data access, system database load is very heavy.
4.WEB/DB separation structure or multiple WEB single DB architecture.

Unconfirmed questions:
1. Whether read and write to the file during concurrent access can cause a lock problem.
2. The data file involved is too long and the performance is very good.

Expanding ideas:
1. Generate JavaScript data definition code, called on the client.
2. It has not been thought that ...

Hope to discuss together.
Cache
Caching is also a good approach if you want to make your own large PHP applications perform better. There are already many caching options available, including: Zend CACHE,APC, and Afterburner cache.

All of these products are part of the "Caching module". When a request for a. php file appears for the first time, they save the Intermediate code of PHP in the Web server memory, and then respond to the subsequent request with a "compiled" version. This approach does improve the performance of the application because it minimizes the amount of disk traffic (code has been read and parsed), and the code runs directly in memory, making the server respond to requests much more quickly. Of course, the cache module will also monitor changes to the PHP source files and, if necessary, cache the pages, preventing users from getting pages that are still generated by outdated PHP code. Because caching modules can significantly reduce the load on the server and improve the responsiveness of PHP applications, they are ideal for Web sites with larger workloads.

How to select these caching products
Zend Cache is the commercial software of Zend Technologies company, and Zend Technologies is the aforementioned company that provides us with PHP engines and free Zend optimizer. Zend Cache is indeed a well-deserved reputation! For a large PHP page, you can feel that the speed will improve after the first run, and that the server will have more resources available. Unfortunately, this product is not free, but in some cases it is still a matter of value.

The afterburner cache is a free caching module from Bware Technologies, which is currently a beta version of the product. The afterburner cache approach looks similar to the Zend cache, but its performance improvement (also) cannot be compared to the Zend cache, and it cannot work with Zend Optimizer.

APC is the acronym for Alternative PHP Cache, which is another free cache module from community Connect. The product is already stable enough for formal use, and it also seems to be able to greatly increase the speed of response requests.
Current 1/3 page 123 Next read the full text

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.