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