最后,我们来定制一个应用,综合的来解释 PEAR 缓冲机制的整体框架。
我们定义一个叫做 MySQL_Query_Cache 的类,缓冲 SELECT 的查询结果。
我们首先定义类的变量:
<?php
require_once ’Cache.php’;
class MySQL_Query_Cache extends Cache {
var $connection = null;
var $expires = 3600;
var $cursor = 0;
var $result = array();
function MySQL_Query_Cache($container = ’file’,
$container_options = array(’cache_dir’=> ’.’,
’filename_prefix’ => ’cache_’), $expires = 3600)
{
$this->Cache($container, $container_options);
$this->expires = $expires;
}
function _MySQL_Query_Cache() {
if (is_resource($this->connection)) {
mysql_close($this->connection);
}
$this->_Cache();
}
}
?>
在正式开始之前,我们需要一些辅助函数。
function connect($hostname, $username, $password, $database) {
$this->connection = mysql_connect($hostname, $username, $password) or trigger_error(’数据库连接失败!’, E_USER_ERROR);
mysql_select_db($database, $this->connection) or trigger_error(’数据库选择失败!’, E_USER_ERROR);
}
function fetch_row() {
if ($this->cursor < sizeof($this->result)) {
return $this->result[$this->cursor++];
} else {
return false;
}
}
function num_rows() {
return sizeof($this-&g
| 对此文章发表了评论 |
