采用PEAR来缓冲PHP程序_PHP
PHP 世界中缓冲是一个热门的话题,因为 PHP 产生的动态页面,每次用户请求都需要重新计算,不论请求的结果是否一样,同时,PHP 每次都会编译一次脚本。这样的超负荷运转对一个流量很高的网站来说肯定难以忍受。幸运的是, Web 的结果可以缓冲,而不需要重新运行和编译脚本,商品化的产品像 ZendCache 或者开源的 Alternate PHP Cache都提供了把 PHP 脚本编译为字节代码并缓冲的办法。
PEAR 的缓冲包提供了缓冲动态内容,数据库查询和 PHP 函数调用的框架。
就像 Perl 有 CPAN, TeX 有 CTAN,PHP 也有自己的中心资源库,存放类,库和模块。这个库称为 PEAR(PHP Extension and Add-On Repository)。
本文假设你已经安装了 PEAR 环境,如果没有的话,可以去 PHP 网站下载。
PEAR 的缓冲包包含一个总体的缓冲类和几个特别的子类。缓冲类使用容器类来存贮和管理缓冲数据。
下面是 PEAR 缓冲当前所包含的容器,以及各自的参数:
file -- file 容器在文件系统存储了缓冲的数据,是最快的容器。
cache_dir -- 这是容器存储文件的目录。
filename_prefix -- 缓冲文件的前缀,例如:"cache_"。
shm -- shm 容器把缓冲数据放入共享内存,基准测试显示,目前的实现下,这个容器的速度要比文件容器慢。
shm_key -- 共享内存使用的键值。
shm_perm -- 使用共享内存数据段的权限。
shm_size -- 分配共享内存的大小。
sem_key -- 信号灯的键值。
sem_perm -- 信号灯的权限。
db -- PEAR 的数据库抽象层。
dsn -- 数据库连接的 DSN 。可以参考 PEAR 的 DB 文档。
cache_table -- 表的名字。
phplib -- phplib 容器使用数据库抽象层存储缓冲。
db_class
db_file
db_path
local_file
local_path
ext/dbx -- PHP 的数据库抽象层扩展,如果像把缓冲存入数据库,可以采用这个容器。
module
host
db
username
password
cache_table
persistent
使用 PEAR Cache 所得到的性能提升取决于你所选择的缓冲容器,例如,把数据库的结果再次存入数据库缓冲中就显得毫无意义。
PEAR Cache 的函数缓冲模块能把任何函数或者方法的结果缓冲,不论是 PHP 的内置函数还是用户自定义函数,他缺省采用文件容器,把缓冲数据放入到一个叫做
function_cache 的目录。
Cache_Function 类的构造器可以有三个可选的参数:
$container :缓冲容器的名字。
$container_options :缓冲容器的数组参数。
$expires:缓冲对象过期的时间(秒数)。
普通的函数调用采用 Cache_Function 类的 call() 方法时,就能触发缓冲。调用 call() 很容易,的一个参数是函数的名字,然后是函数的参数,第二个参数是要调用函数中的第一个,依此类推,我们来看例子:
例 1: 缓冲函数和方法的调用
// 调用 PEAR Cache 的函数缓冲。
require_once ’Cache/Function.php’;
// 定义一些类和函数。
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()’;
}
// 取得 Cache_Function 对象
$cache = new Cache_Function();
// 对 foo 类的静态函数 bar() 作缓冲(foo::bar())。
$cache->call(’foo::bar’, ’test’);
// $bar->foobar()
$cache->call(’bar->foobar’, ’bar’);
$cache->call(’foobar’);
?>
下面我们采用 Cache_Output 来把输出作缓冲:
例子 2: 缓冲脚本的输出
// 加载 PEAR Cache 的输出缓冲
require_once ’Cache/Output.php’;
$cache = new Cache_Output(’file’, array(’cache_dir’ => ’.’) );
// 计算要缓冲页面的标记,我们假定页面的缓冲取决于
// URL, HTTP GET 和 POST 变量以及 cookies。
$cache_id = $cache->generateID(array(’url’ => $REQUEST_URI, ’post’ => $HTTP_POST_VARS, ’cookies’ => $HTTP_COOKIE_VARS) );
// 查询缓冲
if ($content = $cache->start($cache_id)) {
// 缓冲命中
echo $content;
die();
}
// 缓冲丢失
// -- 在这里插入内容产生代码 --
// 把页面存入缓冲
echo $cache->end();
?>
利用 Cache_Output 类,很容易把一个动态的数据库驱动的网站应用转化为静态,从而极大的提升站点的性能。
越来越多的站点在采用 GZIP 压缩 HTML 内容,这样减少了服务器的带宽消耗,对于使用 Modem 上网的用户来说也能受益不少。
Cache_OutputCompression 扩展了 Cache_Output 类的功能,他把 GZIP 压缩的 HTML 内容进行缓冲,从而节省了 CPU 压缩的时间。
待续...

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.
