찾다
php教程php手册采用 PEAR 来缓冲 PHP 程序(二)

程序

最后,我们来定制一个应用,综合的来解释 PEAR 缓冲机制的整体框架。


我们定义一个叫做 MySQL_Query_Cache 的类,缓冲 SELECT 的查询结果。

我们首先定义类的变量:



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 result)) {
return $this->result[$this->cursor++];
} else {
return false;
}
}

function num_rows() {
return sizeof($this->result);
}
?>


下面我们来看怎样缓冲:

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 查询缓冲


$cache = new MySQL_Query_Cache();
$cache->connect(’hostname’, ’username’, ’password’, ’database’);
$cache->query(’select * from table’);

while ($row = $cache->fetch_row()) {
echo ’

’;
print_r($row);
echo ’

’;
}
?>



성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.