Home  >  Article  >  Backend Development  >  Using PEAR to buffer PHP programs 1_PHP tutorial

Using PEAR to buffer PHP programs 1_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:28:58696browse

Buffering is a hot topic in the PHP world, because the dynamic pages generated by PHP need to be recalculated every time the user requests it, regardless of whether the result of the request is the same. At the same time, PHP compiles the script every time. Such overload would certainly be unbearable for a website with high traffic. Fortunately, the results of the Web can be cached without the need to rerun and compile the script. Commercial products like ZendCache or the open source Alternate PHP Cache provide a way to compile PHP scripts into bytecode and cache them. PEAR's buffering package provides a framework for buffering dynamic content, database queries, and PHP function calls. Just like Perl has CPAN and TeX has CTAN, PHP also has its own central resource library to store 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. PEAR's buffer package contains an overall buffer class and several specialized subclasses. Buffer classes use container classes to store and manage buffered data. The following are the containers currently included in the PEAR buffer, and their respective parameters: file -- The file container stores buffered data in the file system and is the fastest container. cache_dir – This is the directory where the container stores files. filename_prefix -- the prefix of the cache file, for example: "cache_". shm -- The shm container puts buffered data into shared memory. Benchmarks show that with the current implementation, this container is slower than the file container. shm_key -- the key used by shared memory. shm_perm – Permission to use shared memory data segments. shm_size -- The size of allocated shared memory. sem_key – The key value of the semaphore. sem_perm – Permissions for semaphores. db – PEAR’s database abstraction layer. dsn – The DSN of the database connection. You can refer to PEAR’s DB documentation. cache_table – The name of the table. phplib – The phplib container uses a database abstraction layer to store buffers. db_class db_file db_path local_file local_path ext/dbx -- PHP's database abstraction layer extension. If you want to store the buffer in the database, you can use this container. module host db username password cache_table persistent The performance improvement obtained by using PEAR Cache depends on the buffer container you choose. For example, it is meaningless to store the database results again in the database cache. The function buffer module of PEAR Cache can buffer the results of any function or method, whether it is PHP's built-in function or a user-defined function. It uses a file container by default and puts the buffered data into a directory called function_cache. The constructor of 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) the buffer object expires. Buffering can be triggered when a normal function call uses the call() method of the Cache_Function class. Calling call() is easy, one parameter is the name of the function, then the parameters of the function, the second parameter is the first of the functions to be called, and so on, let's look at an example: Example 1: Buffer function and Method call // Call the function buffer of PEAR Cache.


"; } } class bar { function foobar($object) { echo '$'.$object.'->foobar('.$object.') '; } } $bar = new bar; function foobar( ) { echo 'foobar()'; } // Get the Cache_Function object $cache = new Cache_Function(); // Cache the static function bar() of class foo (foo::bar()). ('foo::bar', 'test'); // $bar->foobar() $cache->call('bar->foobar', 'bar'); $cache->call('foobar') ; ?> Below we use Cache_Output to buffer the output: Example 2: Buffering the output of the script // Load the output buffer of PEAR Cache '.') ); // Calculate the mark of the page to be buffered, we assume that the page Caching 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 - - // Store the page in the cache echo $cache->end(); ?> Using the Cache_Output class, it is easy to convert a dynamic database-driven website application into a static one, thus greatly improving the performance of the site. Many sites use GZIP to compress HTML content, which reduces the bandwidth consumption of the server and can also benefit a lot from users who use Modem to access the Internet. Cache_OutputCompression extends the functions of the Cache_Output class. It buffers GZIP compressed HTML content. This saves CPU compression time. To be continued...

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531732.htmlTechArticleBuffering is a hot topic in the PHP world, because the dynamic pages generated by PHP need to be refreshed every time the user requests Calculation, regardless of whether the result of the request is the same, at the same time, PHP will...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn