Use PEAR to buffer PHP programs (i)

Source: Internet
Author: User
Tags array contains dsn functions pear php website semaphore
The program PHP World buffer is a hot topic, because the dynamic page generated by PHP, each user request needs to be recalculated, regardless of the request results are the same, and PHP will compile a script every time. This overload is certainly unbearable for a site with high traffic. Fortunately, Web results can be buffered without the need 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.





PEAR's buffer package provides a framework for buffering dynamic content, database queries, and PHP function calls.





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 that you have installed the PEAR environment, if not, you can go to the PHP website to download.








PEAR's buffer package contains an overall buffer class and several special subclasses. Buffer classes use container classes to store and manage buffered data.





below is the current container that the PEAR buffer contains, along with its respective parameters:





file-The file container stores buffered data in the filesystem and is the fastest container.





Cache_dir-This is the directory where the containers store files.





Filename_prefix-The prefix of the buffered file, for example: "Cache_".





SHM-The SHM container puts buffered data into shared memory, and benchmark tests show that the container is slower than the file container at the current implementation.





Shm_key--the key value used by shared memory.





shm_perm-Permission to use the shared memory data segment.





Shm_size-Allocates the size of shared memory.





Sem_key--The key value of the semaphore.





Sem_perm--the right of the semaphore.





DB--PEAR's database abstraction layer.





DSN-the DSN for the database connection. You can refer to PEAR's DB documentation.





cache_table--The name of the table.





Phplib--The Phplib container uses the database abstraction layer to store buffers.





Db_class





Db_file





Db_path





Local_file





Local_path





ext/dbx-PHP's database abstraction layer expands to use this container if the buffer is stored in a database.





Module





Host





DB





username





Password





cache_table





Persistent




The performance boost that
uses with PEAR Cache depends on the buffer container you choose, for example, it makes no sense to put the results of a database back into the database buffer.





PEAR Cache's function buffer module can buffer the results of any function or method, whether PHP's built-in function or user-defined function, he defaults to the file container, the buffer data into a called


Function_cache's directory.







The constructor for the
Cache_function class can have three optional parameters:





$container: The name of the buffer container.





$container _options: Array parameters of the buffer container.





$expires: The time (in seconds) that the buffered object expires.





a normal function call can trigger a buffer when called by the Cache_function class. Calling call () is easy, one argument is the name of the function, then the parameter of the function, the second parameter is the first in the function to invoke, and so on, let's look at the example:





Example 1: Invocation of buffering functions and methods





//Call function buffering for PEAR Cache.





<?php


require_once ' cache/function.php ';





//defines some classes and functions.





class Foo {


function Bar ($test) {


echo "Foo::bar ($test) <br>";


}


}





class Bar {


function Foobar ($object) {


Echo ' $ '. $object. '->foobar ('. $object. ')


’;


}


}





$bar = new Bar;





function Foobar () {


Echo ' foobar () ';


}





//Get Cache_function object





$cache = new Cache_function ();





//buffers 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 ');


?>














below we use the Cache_output to buffer output:





Example 2: Output of the buffered script





//Load the output buffer of PEAR cache





<?php


require_once ' cache/output.php ';





$cache = new Cache_output (' file ', array (' Cache_dir ' => '));





//calculation to buffer the page's tag, we assume that the page's buffering depends on the


//URLs, HTTP get and POST variables, and cookies.





$cache _id = $cache->generateid (array (' URL ' => $REQUEST _uri, ' post ' => $HTTP _post_vars, ' Cookies ' => $HTTP _ Cookie_vars));





//Query buffering





if ($content = $cache->start ($cache _id)) {





//Buffer hit


Echo $content;


die ();


}





//buffering loss





//-Insert content Generation code here-





//The page is cached


echo $cache->end ();


?>








using the Cache_output class, it is easy to convert a dynamic database-driven website application to static, thereby greatly improving 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 use of Modem Internet users can also benefit a lot.





Cache_outputcompression extends the functionality of the Cache_output class by buffering GZIP-compressed HTML content, which saves 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.