Home >Backend Development >PHP Tutorial >Using PEAR to buffer PHP programs_PHP tutorial
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 request result 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 special 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. Benchmark tests show that under the current implementation, this container is slower than the file container.
shm_key -- the key value used by shared memory.
shm_perm -- Permission to use the shared memory data segment.
shm_size -- The size of allocated shared memory.
sem_key -- The key value of the semaphore.
sem_perm -- semaphore permissions.
db -- PEAR's database abstraction layer.
dsn -- DSN of 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 buffers in the database, you can use this container.
module
host
db
username
password
cache_table
persistent
The performance improvement you get from using PEAR Cache depends on the buffer container you choose, for example, It is meaningless to store the database results in the database buffer again.
PEAR Cache's function buffer module can cache 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 file called
function_cache directory.
The constructor of the Cache_Function class can have three optional parameters: