Using pear to buffer PHP programs _php Tutorial

Source: Internet
Author: User
Tags dsn pear php database php website
Buffering in the PHP World is a hot topic, because PHP generates dynamic pages that need to be recalculated every time a user requests, regardless of whether the requested result is the same, and PHP compiles the script every time. This overload will certainly be unbearable for a high-traffic site. Fortunately, the results of the WEB can be buffered without having to rerun and compile scripts, and commercialized products like Zendcache or open source Alternate PHP cache provide a way to compile PHP scripts into byte code and buffer them. The PEAR buffer package provides a framework for buffering dynamic content, database queries, and PHP function calls. Just like Perl has CPAN, TeX has ctan,php also has its own central repository, storage classes, libraries and modules. This library is called PEAR (PHP Extension and add-on Repository). This article assumes you have installed the PEAR environment, if not, you can go to the PHP website to download. The PEAR buffer package contains a general buffer class and several special subclasses. Buffer classes use container classes to store and manage buffered data. The following are the containers currently contained by the PEAR buffer, as well as the respective parameters: file-The Files container stores buffered data in the filesystem and is the fastest container. Cache_dir-This is the directory where the container stores the files. Filename_prefix-The prefix of the buffered file, for example: "Cache_". SHM-The SHM container puts the buffered data into shared memory, and benchmarks show that the container is slower than the file container at the current implementation. Shm_key--The key value used for shared memory. Shm_perm--Permission to use shared memory data segments. Shm_size--allocates the size of shared memory. Sem_key--The key value of the semaphore. Sem_perm--the permission of the semaphore. DB--PEAR's database abstraction layer. DSN--The DSN for the database connection. You can refer to the DB documentation for PEAR. Cache_table--The name of the table. Phplib--The Phplib container uses the database abstraction layer to store the buffer. Db_class db_file db_path local_file local_path ext/dbx--PHP Database Abstraction layer extension, if the buffer is stored as a database, you can use this container. Module host DB username password cache_table persistent use PEAR The performance gain of the cache depends on the buffer container you choose, for example, it makes no sense to store the results of the database in the database buffer again. The function buffer module of the PEAR cache can buffer the results of any function or method, whether it is a PHP built-in function or a user-defined function, by default using a file container and putting the buffered data into a directory called Function_cache. The constructor for the Cache_function class can have three optional parameters: $container: The name of the buffer container. $container _options: The array parameter of the buffer container. $expires: The time (in seconds) that the buffer object expires. Normal function calls can trigger buffering when the call () method of the Cache_function class is used. Calling call () is easy, one parameter is the name of the function, then the argument of the function, the second argument is the first one to call the function, and so on, let's look at an example: Example 1: Buffer function and method invocation//Call PEAR Cache function buffer.
"; } } Class Bar {function Foobar ($object) {echo ' $ '. $object. '->foobar ('. $object. ') ’; }} $bar = new bar; function Foobar () {echo ' foobar () ';}//Get Cache_function object $cache = new Cache_function (); The static function bar () of the Foo class is buffered (Foo::bar ()). $cache->call (' Foo::bar ', ' test '); $bar->foobar () $cache->call (' Bar->foobar ', ' Bar '); $cache->call (' Foobar ');?> below we use the Cache_output to buffer the output: Example 2: Buffer script output//load PEAR cache output Buffer ’.’) ); To calculate the markup to buffer the page, we assume that the page's buffering 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 lost//--insert content generated code here--//Save page into the buffer echo $cache->end ();?> using the Cache_output class, it is easy to convert a dynamic database-driven Web application to static, which greatly improves the performance of the site. More and more sites in the use of GZIP compressed HTML content, so as to reduce the bandwidth consumption of the server, for the user using Modem Internet can also benefit a lot. Cache_outputcompression extends the functionality of the Cache_output class by buffering the GZIP-compressed HTML content, saving CPU time. Cond...

http://www.bkjia.com/PHPjc/531732.html www.bkjia.com true http://www.bkjia.com/PHPjc/531732.html techarticle Buffering in the PHP World is a hot topic, because PHP generates dynamic pages, and each user request needs to be recalculated, regardless of whether the requested result is the same, and PHP every time ...

  • 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.