One of the PHP Cache Technologies

Source: Internet
Author: User
Tags qoute
I have been optimizing program performance for a while recently. I have an interesting idea and want to discuss it with you. Cache is a typical application mode based on the "space-for-time" policy 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 traffic volumes, indicating SyntaxHighlighter. all ();

I have been optimizing program performance for a while recently. I have an interesting idea and want to discuss it with you.

Cache is a typical application mode based on the "space-for-time" policy 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 traffic volumes, significantly reducing the system load and improving system performance. Compared with the page cache, the result set is a kind of "raw data" that does not contain the format information. it has a relatively small amount of data and can be formatted again, so it looks quite clever. Because php is a script language for "compiling and executing, to some extent, it also provides a convenient way to use the result set cache-by dynamically include the corresponding data definition code segment. If you create a cache on RamDisk, the efficiency can be further improved. The following is a small sample code for your 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. data is relatively stable, mainly for reading operations.

2. file operations are faster than database operations.

3. complex data access questions, massive data access, intensive data access, and extremely heavy system database load.

4. Web/DB splitting structure or multiple Web single DB structures.

Unconfirmed problems:

1. whether the file read/write during concurrent access will cause the locking problem.

2. what 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. I have not thought ......

Hope to discuss it together.

Cache

If you want to improve the performance of your huge PHP application, using cache is also a good method. There are already many caching schemes available, including Zend Cache, APC, and Afterburner Cache.

All these products belong to the cache module ". When a request to the. PHP file appears for the first time, it will save the PHP intermediate code in the memory of the Web server, and then respond to the subsequent request with the "compiled" version. This method can indeed improve the performance of the application, because it minimizes the disk access volume (the code has been read and parsed ), the code runs directly in the memory, greatly improving the server's response speed. Of course, the cache module also monitors changes in PHP source files and caches pages again when necessary to prevent users from generating pages from outdated PHP code. Because the cache module can significantly reduce the server load and improve the response efficiency of PHP applications, it is very suitable for websites with large loads.

How to choose these Cache products

Zend Cache is the commercial software of Zend Technologies, and Zend Technologies is the one mentioned earlier that provides us with PHP engines and free Zend Optimizer. Zend Cache is indeed a good name! For large PHP pages, you can feel that the speed will increase after the first running, and more available resources will be available on the server. Unfortunately, this product is not free, but in some cases it is still worth the money.

Afterburner Cache is a free Cache module from Bware Technologies. Currently, this product is a Beta version. The practice of Afterburner Cache seems to be similar to that of Zend Cache, but its performance improvement (also) cannot be compared with that of Zend Cache, and it cannot work with Zend Optimizer.

APC is short for Alternative PHP Cache. it is another free Cache module from Community Connect. This product is already stable enough for official use, and it seems to be able to greatly speed up response to requests.

Enable compression

The free Apache module mod_gzip from Remote Communications is capable of compressing static Web content for browsers that support such content encoding. Mod_gzip is very effective for most static Web content. Mod_gzip can be easily compiled into Apache or used as a DSO. According to Remote communications, mod_gzip can also compress dynamic content from mod_php and mod_perl. I tried again and again, but it does not seem to work. I have read many forums and articles about mod_gzip. it seems that the next version of mod_gzip (may be 1.3.14.6f) is expected to be solved. Previously, we could use mod_gzip in the static part of the website.

However, sometimes we do want to compress dynamic content, so we have to look for other methods. One way is to use class.gzip _ encode. php, which is a PHP class that can be used to compress page content. the specific method is to call some functions of this class at the beginning and end of the PHP script. To implement this scheme at the website level, you can call these functions from the auto_PRepend and auto_append commands of the php. ini file. This method is effective, but it undoubtedly brings more overhead to websites with high loads. For more information about how to use this class, see its source code. The source code description is quite complete, and the author tells you everything you must know.

PHP 4.0.4 has a new output cache handle, ob_gzhandler, which is similar to the previous class but has different usage. When using ob_gzhandler, add the following content to php. ini:

Output_handler = ob_gzhandler;

This line of code enables PHP to activate the output cache and compress all the content it sends. If for some reason you don't want to go to php. add this line of code in ini. you can also use the directory where the PHP source file is located. the htaccess file changes the default server behavior (not compressed). The syntax is as follows:

Php_value output_handler ob_gzhandler

Or call the code from PHP, as shown below: ob_start ("ob_gzhandler ");

The method of using the output cache handle is indeed very effective and does not impose any special load on the server. However, it must be noted that Netscape Communicator does not provide good support for compressed images. Therefore, unless you can ensure that all users use IE browsers, you should disable JPEG and GIF image compression. Generally, this compression is effective for all other files, but we recommend that you perform tests on different browsers, this is especially important when you use a special plug-in or data viewer.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.