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. = ";); 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 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.
about Compression
The free Apache module from remote communications Mod_gzip has the ability to compress static Web content for browsers that support such content encoding. For most static Web content, Mod_gzip is very effective. Mod_gzip can be easily compiled into Apache or used as DSO. According to the remote communications company, 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 read a lot of forums and articles about Mod_gzip, and it seems that the next version of Mod_gzip (probably 1.3.14.6f) is expected to be resolved. Prior to this, we could use Mod_gzip in the static section of the website.
But sometimes we do want to compress dynamic content, so we have to find other ways. One way to do this is to use class.gzip_encode.php, a PHP class that can be used to compress the contents of a page, by invoking some of the functions of the class at the beginning and end of the PHP script. If you are implementing this scenario at the site level, you can call these functions from the auto_prepend and auto_append directives of the php.ini file. While this approach works, it undoubtedly brings more overhead to high-load web sites. For a detailed description of how to use this class, see its source code. Its source code description is quite perfect, and the author tells you all the things you have to know.
PHP 4.0.4 has a new output cache handle Ob_gzhandler, similar to the previous class, but with a different usage. The content to be added to PHP.ini when using Ob_gzhandler is as follows:
Output_handler = Ob_gzhandler;
This line of code allows PHP to activate the output cache and compress everything it sends out. If for some reason you do not want to add this line of code to php.ini, you can also change the default server behavior (not compressed) by using the. htaccess file in the directory where the PHP source files are located, with the following syntax:
Php_value Output_handler Ob_gzhandler
Or it is called from the PHP code, as follows: Ob_start ("Ob_gzhandler");
The method of using output cache handles is really effective and does not give the server any special load. It is important to note, however, that Netscape Communicator has poor support for compressed graphics, so you should suppress JPEG and GIF graphics unless you can ensure that all users are using Internet Explorer. Generally, this compression works for all other files, but it is recommended that you test separately for various browsers, especially if you are using a special plug-in or data viewer.
Editor's recommendation:
http://www.bkjia.com/PHPjc/446856.html www.bkjia.com true http://www.bkjia.com/PHPjc/446856.html techarticle 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 the "space-for-time" strategy ...