Simple Data caching technology
Recently did a while to optimize the performance of the program, there is a more interesting idea, want to put forward to communicate with you.
Cache is a typical application mode of "space-time" strategy, which is an important method to improve the performance of the system. The use of the cache can greatly reduce the number of database operations and significantly reduce the system load and improve the performance of the system under the condition of large traffic. Compared to the cache of the page, the result set is a "raw data" that does not contain formatting information, the amount of data is relatively small, and can be formatted again, so it seems quite flexible. Because PHP is a scripting language that "executes on one side", it also provides a fairly handy way of using the result set cache-using caching in a way that dynamically includes the corresponding data definition snippet. If the cache is built on "RamDisk", the efficiency should be improved further. 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 = " 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;
}
?>
Applicable situation:
1. The data is relatively stable, mainly read operation.
2. File operations are faster than database operations.
3. Complex data access, large data volume access, dense data access, system database load is very heavy.
4.WEB/DB separate structure or multi-Web single-DB structure.
Unconfirmed questions:
1. Whether read and write to the file during concurrent access can cause locking problems.
2. The data files involved are too long, how much performance.
Expansion ideas:
1. Generate JavaScript Data definition code that is called on the client.
2. Not yet thought ...
Hope to explore together.
Cache
Caching is also a good way to make your own large PHP application more performance-based. There are now a number of caching options available, including: Zend CACHE,APC, and Afterburner cache.
All of these products belong to the "cache module". When requests for. php files appear for the first time, they store the PHP intermediate code in the Web server memory, and thereafter respond to subsequent requests with a "compiled" version. This approach does improve the performance of your application because it minimizes the amount of disk traffic (code has been read and parsed), and the code runs directly in memory to make the server respond to requests much more quickly. Of course, the cache module also monitors changes to the PHP source file and re-caches the page if necessary, thus preventing the user from getting a page that is still generated by outdated PHP code. Because cache modules can significantly reduce server load and improve the responsiveness of PHP applications, they are ideal for use in heavily loaded web sites.
How to select these cache products
Zend Cache is the commercial software of Zend Technologies, and Zend Technologies is the company that provided us with the PHP engine and free Zend optimizer as mentioned earlier. Zend Cache is truly a worthy reputation! For large PHP pages, you can feel that the speed will improve after the first run, and the server will have more resources available. Unfortunately this product is not free, but in some cases it is still worth the value of the goods.
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 it does not improve performance (yet) compared to Zend cache, and it cannot work with Zend Optimizer.
APC is the abbreviation for alternative PHP cache, which is another free cache module from community Connect. This product is already stable enough for formal use, and it seems to be able to improve the speed of responding to requests to a large extent.
Current 1/3 page 123 next page
The above describes how to measure the acceleration of the slide on the air cushion guide How to improve the PHP speed 1th/3 page, including to determine the slide on the air Cushion guide the acceleration of the content, I hope to be interested in PHP tutorial friends helpful.