近来做了一阵子程序性能的优化工作,有个比较有意思的想法,想提出来和大家交流一下。
Cache是“以空间换时间”策略的典型应用模式,是提高系统性能的一种重要方法。缓存的使用在大访问量的情况下能够极大的减少对数据库操作的次数,明显降低系统负荷提高系统性能。相比页面的缓存,结果集是一种“原始数据”不包含格式信息,数据量相对较小,而且可以再进行格式化,所以显得相当灵活。由于PHP是“一边编译一边执行”的脚本语言,某种程度上也提供了一种相当方便的结果集缓存使用方法——通过动态include相应的数据定义代码段的方式使用缓存。如果在“RamDisk”上建缓存的话,效率应该还可以得到进一步的提升。以下是一小段示例代码,供参考。
<br>// load data with cache <br>function load_data($id,$cache_lifetime) { <br>// the return data <br>$data = array(); <br>// make cache filename <br>$cache_filename = ‘cache_‘.$id.‘.php‘; <br>// check cache file‘s last modify time <br>$cache_filetime = filemtime($cache_filename); <br>if (time() - $cache_filetime //** the cache is not expire <br>include($cache_filename); <br>} else { <br>//** the cache is expired <br>// load data from database <br>// ... <br>while ($dbo->nextRecord()) { <br>// $data[] = ... <br>} <br>// format the data as a php file <br>$data_cache = "<?rn "; <br>while (list($key, $val) = each($data)) { <br>$data_cache .= "$data[‘$key‘]=array(‘"; <br>$data_cache .= "‘NAME‘=>"".qoute($val[‘NAME‘]).""," <br>$data_cache .= "‘VALUE‘=>"".qoute($val[‘VALUE‘]).""" <br>$data_cache .= ";);rn"; <br>} <br>$data_cache = "?>rn"; <br>// save the data to the cache file <br>if ($fd = fopen($cache_filename,‘w+‘)) { <br>fputs($fd,$data_cache); <br>fclose($fd); <br>} <br>} <br>return $data; <br>} <br>?> |
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 issues:
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?
Expansion ideas:
1. Generate JavaScript data definition code and call it on the client.
2. Haven’t thought of it yet...
Hope to discuss it together.
CachingIf you want to improve the performance of your huge PHP application, caching is also a good method. 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 in Beta version. Afterburner Cache looks similar to Zend Cache, but it doesn't improve performance as much as Zend Cache (yet), 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.
About compressionThe free Apache module mod_gzip from Remote Communications has the ability to compress static web content for browsers that support this type of content encoding. For the vast majority of static web content, mod_gzip works very well. mod_gzip can be easily compiled into Apache and can also be used as a DSO. According to Remote Communications, mod_gzip can also compress dynamic content from mod_php, mod_perl, etc. I tried again and again, but it didn't seem to work. I have read many forums and articles about mod_gzip, and it seems that this problem is expected to be solved in the next version of mod_gzip (probably 1.3.14.6f). Until then, we can use mod_gzip in the static parts of the website.
However, sometimes we really don’t want to compress dynamic content, so we have to find other ways. One way is to use class.gzip_encode.php, which is a PHP class that can be used to compress the content of the page by calling certain functions of the class at the beginning and end of the PHP script. If you want to implement this solution at the website level, you can call these functions from the auto_prepend and auto_append directives in the php.ini file. Although this method is effective, it undoubtedly brings more overhead to high-load websites. For detailed instructions on how to use this class, see its source code. Its source code description is quite complete, and the author tells you everything you need to know.
PHP 4.0.4 has a new output cache handler ob_gzhandler, which is similar to the previous class, but its usage is different. When using ob_gzhandler, add the following to php.ini:
output_handler = ob_gzhandler ;
This line of code causes PHP to activate output caching and compress everything it sends out. If for some reason you don't want to add this line of code to php.ini, you can also change the default server behavior (no compression) through the .htaccess file in the directory where the PHP source file is located. The syntax is as follows:
php_value output_handler ob_gzhandler
Or call it from PHP code, as shown below: ob_start("ob_gzhandler");
The method of using the output cache handle is indeed very effective and will not bring any special load to the server. But it must be noted that Netscape Communicator has poor support for compressed graphics, so unless you can ensure that all users use IE browser, you should disable compressed JPEG and GIF graphics. In general, this compression works for all other files, but it is recommended that you test it separately for each browser, especially if you use special plug-ins or data viewers. This is especially important. Editor’s recommendation:
http://www.bkjia.com/PHPjc/446856.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446856.htmlTechArticleI have been doing optimization work on program performance for a while recently, and I have an interesting idea that I would like to share with everyone. Let’s talk. Cache is a typical application mode of the "space for time" strategy,...