I have been working on optimizing program performance for a while 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 reducing system load and improving 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 method of using 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 expired
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 reading operations.
2. File operations are faster than database operations.
3. Complex data access, large data volume access, intensive data access, and extremely heavy load on the system database.
4. Web/DB separation structure or multi-Web single DB structure.
Unconfirmed question:
1. Whether reading and writing files during concurrent access will cause locking problems.
2. What is the performance when there are too many data files involved?
Extension ideas:
1. Generate JavaScript data definition code and call it on the client.
2. Haven’t thought of it yet…
Hope to discuss together.
cache
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 getting pages that are still generated by outdated PHP code. Since 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, 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 on the static parts of the website.
However, sometimes we really don't want to compress dynamic content, so we must 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 has different usage. The following content should be added to php.ini when using ob_gzhandler:
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 follows: ob_start("ob_gzhandler");
The method of using output cache handles is indeed very effective and does not place any special load on the server. However, 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.