近来做了一阵子程序性能的优化工作,有个比较有意思的想法,想提出来和大家交流一下。
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>?> |
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.
Caching
If 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 compression
The 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:

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

WebStorm Mac version
Useful JavaScript development tools