Simple Data Cache Technology - 1

zhaozj2021-02-12  135

Knighte is sent in □ -discuz! I feel very useful, turn out to see you.

Recently, I have made an optimization of the performance of the program, there is a more interesting idea, I want to mention it to communicate with you.

Cache is a typical application model of "Space-Discharge Time" strategy is an important way to improve system performance. The use of cache uses the number of times the large number of visits can be greatly reduced, and the system load improves system performance. Compared to the cache of the page, the result set is a "raw data" does not contain format information, the amount of data is relatively small, and it can be formatted again, so it seems flexible. Since PHP is a scripting language that is "while performing", a fairly convenient result set buffer usage method is also provided - the cache is used by dynamic incrude corresponding data definition code segments. If you build a cache in "Ramdisk", the efficiency should also be further improved. The following 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 expire

INCLUDE ($ Cache_FileName);

} else {

// ** the cache is expected

// 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. = ";); / r / n";

}

$ DATA_CACHE = "?> / r / n";

// save the data to the cache file

IF ($ fd = fopen ($ cache_filename, 'w ')) {

FPUTS ($ FD, $ DATA_CACHE);

Fclose ($ fd);

}

}

Return $ DATA;

}

?>

Application:

1. The data is relatively stable, mainly to read operations.

2. File operations are faster than the database.

3. Complex data access, big data volume access, intensive data access, system database load extreme.

4.Web / dB separation structure or multi-web single DB structure.

Unconfirmed issues:

1. Whether the reading and writing of the file will cause lockup issues when concurrent visit. 2. The data file involved is too much, how is the performance.

Expanded ideas:

1. Generate a JavaScript data definition code to call on the client.

2. I haven't thought of ...

I hope to discuss together.

Cache

If you want your huge PHP app, it is a good way to use a cache. There are already many cache schemes available, including: Zend Cache, APC, and Afterburner Cache.

All of these products are "cache module". When a request for the .php file appears, they save the intermediate code of the PHP in the web server memory, which will then respond to successive requests with the "compiled version". This method is indeed possible to improve the performance of the application, as it makes the disk accesses to minimize the minimum level (code has read and parsed), and the code runs directly in memory so that the speed of the server response request is greatly improved. Of course, the cache module also monitors changes in the PHP source file, and then reaches the page if necessary, preventing the user from being generated by the PHP code when the user gets. Since the cache module can significantly reduce the server's load, improve the response efficiency of the PHP application, so they are ideal for websites that are loaded with larger.

How to choose these cache products

Zend Cache is the commercial software of Zend Technologies, and Zend Technologies is the one mentioned by our company with PHP engine and free Zend Optimizer. Zend Cache is indeed a name! 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 it is still a value for money in some cases.

Afterburner cache is a free cache module from BWARE Technologies, current product or beta. The Afterburner Cache is similar to Zend Cache, but it is not comparable to Zend Cache, but it can't work with Zend Optimizer.

APC is an abbreviation from Alternative PHP Cache, which is another free caching module from Community Connect. This product has sufficient stability for formal occasions, and it seems to greatly increase the speed of response requests.

Compression

Free Apache module MOD_GZIP from Remote Communications has the ability to compress static web content for browsers that support this type of content. MOD_GZIP is very effective for most static web contents. MOD_GZIP can be easily compiled into Apache or as DSO. According to Remote Communications, MOD_GZIP can also compress dynamic content from mod_php, mod_perl, etc. I tried it again, but it seems still not. I have read many forums and articles about MOD_GZIP, it seems that this issue is expected to be resolved by the next version of Mod_gzip (may be 1.3.14.6f). Prior to this, we can use mod_gzip at the static part of the website.

However, sometimes we do to compress dynamic content, so you 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 page content. The specific method is to call certain functions of such a class at the beginning of the PHP script and the end of the end. If you want to implement this solution at the site level, you can call these functions from the auto_prepend and auto_append directives of the php.ini file. Although this method is effective, it undoubtedly brought more overhead to high-load websites. For a detailed description of how to use this class, see its source code. Its source code is quite perfect, the author told you all your you have to know. PHP 4.0.4 has a new output buffer handle OB_GZHandler, which is similar to the previous class, but the use is different. The content to join in php.ini when using ob_gzhandler as follows:

OUTPUT_HANDLER = OB_GZHANDAL;

This line of code makes the PHP activates the output cache and compresses all the content it sent out. If you don't want to add this line code in php.ini, you can also change the default server behavior (not compressed) through the PHP source file. Htaccess file, the syntax is as follows:

PHP_VALUE OUTPUT_HANDLER OB_GZHANDLER

Or call from the PHP code, as shown below:

Ob_start ("ob_gzhandler");

The method of using the output cache handle is indeed very effective, and it will not give the server what special load is brought. However, it must be noted that Netscape Communicator is not good for compressed graphics, so you should disable compression JPEG and GIF graphics unless you can guarantee that all users use IE browser. Generally, this compression is effective for all other files, but it is recommended that you test for various browsers, especially when you use a special plugin or data viewer. This is especially important.

转载请注明原文地址:https://www.9cbs.com/read-6711.html

New Post(0)