On the second of PHP caching technology

Source: Internet
Author: User
Keywords On the second of PHP caching technology
Tags dsn pear php database php website

Use PEAR to buffer PHP programs

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.

Here are the containers currently contained in the PEAR buffer, along with their respective parameters:

The File-file container stores buffered data in the file system 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_".

The SHM-SHM container puts the buffered data into shared memory, and the benchmark tests 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.

The key value of the sem_key-semaphore.

sem_perm-the permission of the semaphore.

Db-pear the database abstraction layer.

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

The name of the cache_table-table.

The Phplib-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

The performance gains from using the PEAR Cache depend 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 the example:

Example 1 invocation of buffered functions and methods

Call the function buffer of the PEAR Cache.

Require_once ' cache/function.php ';

Defines some classes and functions.

class Foo {

function Bar ($test) {

echo "Foo::bar ($test)

";

}

}

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 Cache_output to buffer the output:

Example 2 output of a buffered script

Load the output buffer of the PEAR Cache

Require_once ' cache/output.php ';

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

To calculate the markup to buffer the page, we assume that the page's buffering depends on

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 ();

}

Buffer loss

--Insert the content generated code here--

Buffer the page

echo $cache->end ();

?>

With the Cache_output class, it is easy to convert a dynamic database-driven Web application into 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.

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