cvs: php4 /pear/Cache DB.php
| From: | Sebastian Bergmann | Date: | Tue, 14 Aug 2001 16:05:16 +0000 |
| Subject: | cvs: php4 /pear/Cache DB.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-512@lists.php.net to get a copy of this message | ||
sebastian Tue Aug 14 12:05:16 2001 EDT
Added files:
/php4/pear/Cache DB.php
Log:
Initial version of the PEAR DB Cache.
Index: php4/pear/Cache/DB.php
+++ php4/pear/Cache/DB.php
<?php
// +----------------------------------------------------------------------+
// | PHP version 4.0 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt.
|
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> |
// +----------------------------------------------------------------------+
//
// $Id: DB.php,v 1.1 2001/08/14 16:05:16 sebastian Exp $
require_once 'Cache.php';
require_once 'DB.php';
/**
* Cache_DB
*
* Purpose:
*
* Caching the result of database queries.
*
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @module Cache_DB
* @modulegroup Cache_DB
* @package Cache
* @version $Revision: 1.1 $
* @access public
*/
class Cache_DB extends Cache {
var $db;
var $expires = 3600;
function Cache_DB($container = 'file',
$container_options = array('cache_dir' => '.',
'filename_prefix' =>
'query_'
),
$expires = 3600
)
{
$this->Cache($container, $container_options);
$this->expires = $expires;
}
function _Cache_DB() {
$this->_Cache();
if (is_object($this->db)) {
$this->db->disconnect();
}
}
function connect($dsn, $options = false) {
$this->db = DB::connect($dsn, $options);
if (DB::isError($this->db)) {
}
}
function &query($query) {
if (stristr($query, 'SELECT')) {
$cache_id = md5($query);
$result = $this->get($cache_id, 'db_cache');
if ($result == NULL) {
$_result = $this->db->query($query);
if (!DB::isError($_result)) {
$result = new Cache_DB_Result;
while ($row = $_result->fetchRow(DB_FETCHMODE_ASSOC)) {
$result->rows[] = $row;
}
$this->save($cache_id, $result, $this->expires, 'db_cache');
} else {
return $_result;
}
}
} else {
$result = $this->db->query($query);
}
return $result;
}
}
/**
* Cache_DB_Result
*
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @module Cache_DB
* @modulegroup Cache_DB
* @package Cache
* @version $Revision: 1.1 $
* @access public
*/
class Cache_DB_Result {
var $rows = array();
var $cursor = 0;
function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) {
if ($this->cursor < sizeof($this->rows)) {
if ($fetchmode == DB_FETCHMODE_ASSOC) {
return $this->rows[$this->cursor++];
}
elseif ($fetchmode == DB_FETCHMODE_ORDERED) {
$_row = $this->rows[$this->cursor++];
$columns = array_keys($_row);
$row = array();
foreach ($columns as $column) {
$row[] = $_row[$column];
}
return $row;
}
} else {
return false;
}
}
function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) {
}
function numCols() {
if (sizeof($this->rows) > 0) {
return sizeof($this->rows[0]);
} else {
return 0;
}
}
function numRows() {
return sizeof($this->rows);
}
function free() {
}
function tableInfo($mode = null) {
}
}
?>