Use PEAR to buffer PHP program 1

Source: Internet
Author: User
Tags pear php database

Buffer is a hot topic in the PHP world, because every user request generated by PHP needs to be re-computed, regardless of whether the request results are the same, PHP compiles the script once every time. This overload is certainly intolerable for a website with high traffic. Fortunately, Web results can be buffered without re-running or compiling scripts, commercial products such as ZendCache or open-source Alternate PHP Cache provide methods to compile PHP scripts into byte code and buffer them. The PEAR buffer package provides a framework for caching dynamic content, database queries, and PHP function calls. Just like Perl has CPAN, TeX has CTAN, and PHP also has its own central resource library, which stores classes, libraries, and modules. This library is called PEAR (PHP Extension and Add-On Repository ). This article assumes that you have installed the PEAR environment. If not, you can download it from the PHP website. The PEAR buffer package contains an overall buffer class and several special sub-classes. The buffer class uses the container class to store and manage the buffered data. Below are the containers currently included in the PEAR buffer and their respective parameters: the file-file container stores buffered data in the file system, which is the fastest container. Cache_dir -- this is the directory of the container storage file. Filename_prefix -- prefix of the buffer file, for example, "cache _". The shm -- shm container puts the buffered data into the shared memory. The benchmark test shows that the current implementation of this container is slower than the file container. Shm_key -- the key value used by the shared memory. Shm_perm -- permission to use the data segment in the shared memory. Shm_size -- size of shared memory allocated. Sem_key -- the key value of the traffic signal. Sem_perm -- specifies the traffic signal permission. Db-PEAR database abstraction layer. Dsn: The DSN of the database connection. See the pear db document. Cache_table -- Name of the table. Phplib -- phplib container uses database abstraction layer storage buffer. Db_class db_file db_path local_file local_path ext/dbx -- PHP database abstraction layer extension. This container can be used if the buffer is saved to the database. The performance improvement of module host db username password cache_table persistent using PEAR Cache depends on the selected buffer container. For example, it is meaningless to store the database results into the database buffer again. The Function Buffer module of PEAR Cache can buffer the results of any function or method. Whether it is a PHP built-in function or a user-defined function, it uses a file container by default, put the buffered data in a directory named function_cache. The Cache_Function constructor can have three optional parameters: $ container: the name of the buffer container. $ Container_options: array parameter of the buffer container. $ Expires: The expiration time (in seconds) of the buffer object ). When a common function call uses the call () method of the Cache_Function class, the buffer can be triggered. It is easy to call (). One parameter is the name of the function, followed by the function parameter. The second parameter is the first parameter in the function to be called. For example: example 1: buffer function and method call // call the Function Buffer of the PEAR Cache.
";}} Class bar {function foobar ($ object) {echo '$ '. $ object. '-> foobar ('. $ object. ')' ;}}$ bar = new bar; function foobar () {echo 'foobar () ';} // obtain the Cache_Function object $ cache = new Cache_Function (); // buffer the static function bar () of the foo class (foo: bar ()). $ Cache-> call ('foo: bar', 'test'); // $ bar-> foobar () $ cache-> call ('bar-> foobar ', 'bar'); $ cache-> call ('foobar');?> The following uses Cache_Output to buffer the output: Example 2: Buffer Script output // load the PEAR Cache output buffer '.'); // Calculate the tag of the page to be buffered. We assume that the page buffer depends on // URL, http get and POST variables, and cookies. $ Cache_id = $ cache-> generateID (array ('url' => $ REQUEST_URI, 'post' => $ HTTP_POST_VARS, 'cookies '=> $ HTTP_COOKIE_VARS )); // Query Buffer if ($ content = $ cache-> start ($ cache_id) {// buffer hit echo $ content; die ();} // buffer loss // -- insert content here to generate code -- // Save the page to buffer echo $ cache-> end ();?> With the Cache_Output class, it is easy to convert a dynamic database-driven website application into a static state, which greatly improves the performance of the site. More and more sites are using GZIP to compress HTML content, which reduces the bandwidth consumption of servers and will benefit a lot for users who use Modem to access the Internet. Cache_OutputCompression extends the Cache_Output class function. It caches the HTML content compressed by GZIP, thus saving the CPU compression time. To be continued...

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.