关 键 词:
程序 采用 缓冲 this-result this-connection // query
我们定义一个叫做 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->result);
}
?>
下面我们来看怎样缓冲:
<?php
function query($query) {
if (stristr($query, ’SELECT’)) {
// 计算查询的缓冲标记
$cache_id = md5($query);
// 查询缓冲
$this->result = $this->get($cache_id, ’mysql_query_cache’);
if ($this->result == NULL) {
// 缓冲丢失
$this->cursor = 0;
$this->result = array();
if (is_resource($this->connection)) {
// 尽可能采用 mysql_unbuffered_query()
if (function_exists(’mysql_unbuffered_query’)) {$result = mysql_unbuffered_query($query, $this->connection);
} else {$result = mysql_query($query, $this->connection);
}
// 取出所有查询结果
while ($row = mysql_fetch_assoc($result)) {$this->result[] = $row;
}
// 释放 MySQL 结果资源
mysql_free_result($result);
// 把结果缓冲
$this->save($cache_id, $this->result, $this->expires, ’mysql_query_cache’);
}
}
} else {
// 没有查询结果,不需要缓冲
return mysql_query($query, $this->connection);
}
}
?>
例 3: 使用 MySQL 查询缓冲
<?php require_once ’MySQL_Query_Cache.php’;
$cache = new MySQL_Query_Cache();
$cache->connect(’hostname’, ’username’, ’password’, ’database’);
$cache->query(’select * from table’);
while ($row = $cache->fetch_row()) {
echo ’<p>’;
print_r($row);
echo ’</p>’;
}
?>
<全文完>欢迎进入PHP开发资源论坛讨论。
相关文章
图文推荐
主 站 资 源
论 坛 资 源
·SQLite结合PHP的开发实践
·简介一种PHP设计模式:DPT
·在PHP中使用XML-RPC来构造Web Service简介
·PHP网站注入方法之深度分析
·教你使用Apache的rewrite技术来实现URL重写
·Wordpress 2.5 Tags 标签功能
·详细解析:Apache服务器实现的用户验证
·set_include_path在win和linux下的区别
·自定义SESSION(一)——文件
·PDO函数库使用入门
·简介一种PHP设计模式:DPT
·在PHP中使用XML-RPC来构造Web Service简介
·PHP网站注入方法之深度分析
·教你使用Apache的rewrite技术来实现URL重写
·Wordpress 2.5 Tags 标签功能
·详细解析:Apache服务器实现的用户验证
·set_include_path在win和linux下的区别
·自定义SESSION(一)——文件
·PDO函数库使用入门
热门技术文档
·SQLite结合PHP的开发实践
·简介一种PHP设计模式:DPT
·在PHP中使用XML-RPC来构造Web Service简介
·PHP网站注入方法之深度分析
·教你使用Apache的rewrite技术来实现URL重写
·Wordpress 2.5 Tags 标签功能
·关于Servlet及JSP中遇到的多线程同步问题
·详细解析:Apache服务器实现的用户验证
·set_include_path在win和linux下的区别
·自定义SESSION(一)——文件
·简介一种PHP设计模式:DPT
·在PHP中使用XML-RPC来构造Web Service简介
·PHP网站注入方法之深度分析
·教你使用Apache的rewrite技术来实现URL重写
·Wordpress 2.5 Tags 标签功能
·关于Servlet及JSP中遇到的多线程同步问题
·详细解析:Apache服务器实现的用户验证
·set_include_path在win和linux下的区别
·自定义SESSION(一)——文件
最新图文档
本站编辑推荐:(本站开通Delphi4PHP专区,欢迎进入论坛交流!)
- · 3分钟快速了解 Delphi for PHP 特色 (中文), PDF档
- · 购买Delphi for PHP的五大理由, PDF档
- · Delphi for PHP 使用规格介绍, PDF档
- · Delphi for PHP 問答集 (From CodeGear)
- · Delphi for PHP 产品价格表
编缉最近更新文章
网站赞助商
搜索您感兴趣的内容




