采用 PEAR 来缓冲 PHP 程序(二)

2016-01-29 13:40 9 1 收藏

采用 PEAR 来缓冲 PHP 程序(二),采用 PEAR 来缓冲 PHP 程序(二)

【 tulaoshi.com - PHP 】

最后,我们来定制一个应用,综合的来解释 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-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);
}
}
?

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)

例 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';
}
?

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)

来源:http://www.tulaoshi.com/n/20160129/1491064.html

延伸阅读
标签: PHP
  文/徐永久 我们知道 Zend 有免费的优化引擎针对 PHP 而作,但是 FreeLAMP 这次采用的是一个叫做 PHP Accelerator 的缓冲产品。 我们在 “LAMP 加速” 这篇文章中阐述过加速的几种办法,其中提到了 PHP Accelerator,它的安装方法十分简单,但是需要去他的网站获取一个激活键。    一、下载: www.php...
标签: Web开发
本教程是继续上一篇 搭建Windows下基于Eclipse的PHP开发环境,所以之前需要安装eclipse,eclipse是IBM公司主持开发的很棒的开发平台,现在版本已经是3.0了。 文章地址: http://www.openphp.cn/index.php?module=article&id=27 先下载php_dbg.dll,下载地址如下: http://dd.cron.ru/dbg/download.php?h=211.23-...
标签: PHP
PHP4.0 提供了一个输出缓冲函数集合。输出缓冲支持允许你写包裹函数功能压缩缓冲区。在 PHP4 的输出缓冲支持允许 HTML 头信息存放, 无论 HTML的正文是否输出。但在PHP中,头信息( (header(), content type, and cookies )不采用缓冲 。 在使用  PHP的过程中不免要使用到header和setcookie两个函数,这两个函数会发送一...
首先写一个处理URLs重写的类,并且这个类必须继承IHttpHandler接口,以博客园的程序为例: public class UrlReWriteModule : System.Web.IHttpModule { public void Init(HttpApplication context) { context.BeginRequest +=new EventHandler(context_BeginRequest); } public void Dispose() { } } UrlReWriteModule类就是处理URLs...
标签: ASP
  本文作者Brian Schaffner是富士通咨询公司的副主任。他为富士通的技术咨询公司提供架构、设计和开发支持。 在本篇文章里,我们会使用一个简单的Web表单,它会列出某个目录下的一些XML文件。然后,我们会从这个目录里选择一个文件,将它发送到另一个Web表单里,后者会使用被选中的XML文件来填充某些文本字段。 示例XML   我们的...

经验教程

122

收藏

97
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部