Simple Data Cache Technology - 2

zhaozj2021-02-12  157

Pread buffer PHP program

The buffer in the PHP world is a hot topic because of the dynamic page generated by PHP, each user request needs to be recalculated, regardless of whether the result is the same, and PHP will compile a script every time. Such overload operations are certainly unbearable for a website that is very traffic. Fortunately, the result of the web can buffer without the need to re-run and compile scripts, and the commercial product is like Zendcache or open source Alternate PHP Cache provides a way to compile PHP scripts as byte code and buffering.

PEAR's buffer package provides a framework for buffering dynamics, database queries, and PHP functions.

Just like Perl has CPAN, TEX has CTAN, PHP also has its own central repository, storage class, library and module. This library is called PEAR (PHP EXTENSION AND Add-on Repository).

This article assumes that you have installed a PEAR environment. If not, you can go to the PHP website to download.

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

Below is the container included in the PEAR buffer, and its 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 - Buffer file prefix, for example: "cache_".

SHM - SHM container puts buffer data into shared memory, benchmark display, under the current implementation, this container is slower than the file container.

SHM_KEY - Share the key value used by memory.

SHM_PERM - Use the permissions of the shared memory data segment.

SHM_SIZE - Assign the size of the shared memory.

SEM_KEY - The key value of the signal light.

SEM_PERM - the permissions of the signal light.

DB - PEAR database abstraction layer.

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

Cache_table - the name of the table.

PHPLIB - PHPLIB container uses database abstraction layer storage buffers.

DB_CLASS

DB_FILE

DB_PATH

Local_file

Local_path

Ext / DBX - PHP database abstract layer extension, if you are like a buffer into a database, this container can be used.

Module

Host

DB

Username

Password

Cache_table

Persistent

The performance improvements obtained using PEAR Cache depends on the buffer container you choose, for example, it is meaningless to store the result of the database into the database buffer.

PEAR Cache's function buffer module can make any function or method's result buffer, whether it is a built-in function of PHP or a user-defined function, he defaults to file containers, put buffer data into one called

Function_Cache's directory.

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 (second number of seconds) of the buffer object is expired.

The normal function calls the buffer when the CACHE_Function class is used. Calling Call () is easy, one parameter is the name of the function, then the parameter of the function, the second parameter is the first one in the function, according to such push, let's see example:

Example 1: Calling function and method

// Call the function buffer of Pear Cache.

Require_once 'cache / function.php';

// Define 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 a 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 make the output buffer:

Example 2: Buffer script output

/ / Load the output buffer of PEAR Cache

Require_once 'cache / output.php';

$ cache = new cache_output ('file', array ('cache_dir' => '.'));

/ / Calculate the mark to buffer the page, we assume that the buffer of the page depends on

// URL, HTTP GET and POST variables and cookies.

$ cache_id = $ cache-> generateid (Array ('url' => $ request_uri, 'pos' => $ http_post_vars, 'cookies' => $ http_cookie_vars);

// query buffer

IF ($ Content = $ Cache-> Start ($ Cache_ID)) {

// Cushion

Echo $ Content;

DIE ();

}

// buffer loss

// - Insert content generate code here -

// Put the page into the buffer

Echo $ cache-> end ();

?>

Using the Cache_output class, it is easy to convert a dynamic database-driven website to static, thereby greatly enhances the performance of the site.

More and more sites are using Gzip to compress HTML content, which reduces the bandwidth consumption of the server, which can benefit a lot for users using the MODEM Internet.

Cache_outputCompression extends the function of the Cache_Output class, and he buffers Gzip compressed HTML content to save the CPU compression time.

转载请注明原文地址:https://www.9cbs.com/read-6703.html

New Post(0)