Home  >  Article  >  Backend Development  >  How to Improve PHP Speed ​​Page 1/3_PHP Tutorial

How to Improve PHP Speed ​​Page 1/3_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:57:171079browse

Simple data caching technology

I have been doing some program performance optimization work recently, and I have an interesting idea that I would like to share with you.
Cache is a typical application mode of the "space for time" strategy and an important method to improve system performance. The use of cache can greatly reduce the number of database operations in the case of large access volumes, significantly reduce system load and improve system performance. Compared with page caching, the result set is a kind of "raw data" that does not contain format information. The amount of data is relatively small and can be formatted again, so it is quite flexible. Since PHP is a scripting language that "compiles and executes at the same time", it also provides a very convenient way to use result set caching to a certain extent - using the cache by dynamically including the corresponding data definition code segment. If a cache is built on "RamDisk", the efficiency should be further improved. Below is a small 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 .= ");rn";
}
$data_cache = "?>rn";

// save the data to the cache file
if ($fd = fopen($cache_filename,'w+')) {
fputs($fd,$data_cache);
fclose($fd);
}
}
return $data;
}
?>

Applicable situations:
1. The data is relatively stable, mainly for reading operations.
2. File operations are faster than database operations.
3. Complex data access, large data volume access, intensive data access, the system database load is extremely heavy.
4.Web/DB separation structure or multi-Web single DB structure.

Unconfirmed questions:
1. Will reading and writing files during concurrent access cause locking problems.
2. How is the performance when there are too many data files involved?

Expanded ideas:
1. Generate JavaScript data definition code and call it on the client.
2. Haven’t thought of it yet...

We hope to discuss it together.
Caching
If you want to make your huge PHP application have better performance, using caching is also a good way. There are many caching solutions available, including: Zend Cache, APC, and Afterburner Cache.

All these products are "caching modules".When a request for a .php file first occurs, they save the PHP intermediate code in the web server's memory, and then respond to subsequent requests with the "compiled" version. This approach can really improve application performance because it reduces disk access to a minimum (the code has been read and parsed), and the code runs directly in memory, making the server respond to requests much faster. Of course, the caching module will also monitor changes in PHP source files and re-cache the page if necessary, thus preventing the user from receiving pages that are still generated by outdated PHP code. Because caching modules can significantly reduce the load on the server and improve the response efficiency of PHP applications, they are very suitable for websites with heavy loads.

How to choose these caching products
Zend Cache is a commercial software from Zend Technologies, and Zend Technologies is the company mentioned earlier that provides us with the PHP engine and free Zend Optimizer. Zend Cache is indeed well-deserved! For large PHP pages, you can feel that the speed will increase after the first run, and the server will have more available resources. Unfortunately this product is not free, but in some cases it can still be a great value.

Afterburner Cache is a free caching module from Bware Technologies. This product is currently still in Beta version. Afterburner Cache looks similar to Zend Cache, but it doesn't improve performance (yet) to the same degree as Zend Cache, and it doesn't work with Zend Optimizer.

APC is the abbreviation of Alternative PHP Cache, which is another free caching module from Community Connect. The product is already stable enough for formal use, and it seems to improve the speed of responding to requests to a great extent.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317892.htmlTechArticleSimple data caching technology has recently been used to optimize program performance for a while, and I have an interesting idea. I wanted to bring it up and share it with everyone. Cache is a "space for time" strategy...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn