cvs: php4 /pear/Cache DB.php

From: Date: Tue, 14 Aug 2001 18:37:21 +0000
Subject: cvs: php4 /pear/Cache DB.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-515@lists.php.net to get a copy of this message
sebastian Tue Aug 14 14:37:21 2001 EDT Modified files: /php4/pear/Cache DB.php Log: Don't even connect to the database if there's a cached result set for the current query. Just use setConnection() instead of connect() to register a database connection and let Cache_DB connect to the database on demand. Index: php4/pear/Cache/DB.php diff -u php4/pear/Cache/DB.php:1.2 php4/pear/Cache/DB.php:1.3 --- php4/pear/Cache/DB.php:1.2 Tue Aug 14 14:13:53 2001 +++ php4/pear/Cache/DB.php Tue Aug 14 14:37:21 2001 @@ -15,7 +15,7 @@ // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: DB.php,v 1.2 2001/08/14 18:13:53 sebastian Exp $ +// $Id: DB.php,v 1.3 2001/08/14 18:37:21 sebastian Exp $ require_once 'Cache.php'; require_once 'DB.php'; @@ -31,13 +31,16 @@ * @module Cache_DB * @modulegroup Cache_DB * @package Cache -* @version $Revision: 1.2 $ +* @version $Revision: 1.3 $ * @access public */ class Cache_DB extends Cache { var $db; var $expires = 3600; + var $dsn = ''; + var $options = false; + function Cache_DB($container = 'file', $container_options = array('cache_dir' => '.', 'filename_prefix' => 'query_' @@ -58,37 +61,69 @@ } function connect($dsn, $options = false) { - $this->db = DB::connect($dsn, $options); - - if (DB::isError($this->db)) { + if (!isset($this->db)) { + $this->db = DB::connect($dsn, $options); + + if (DB::isError($this->db)) { + } } } + function setConnection($dsn, $options = false) { + $this->dsn = $dsn; + $this->options = $options; + } + 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)) { - $rows = array(); - - while ($row = $_result->fetchRow(DB_FETCHMODE_ASSOC)) { - $rows[] = $row; - } + $cache_id = md5($query); - $result = new Cache_DB_Result($rows); + $result = $this->get($cache_id, 'db_cache'); - $this->save($cache_id, $result, $this->expires, 'db_cache'); - } else { - return $_result; - } - } + if ($result == NULL) { + if (!isset($this->db)) { + if (!empty($this->dsn)) { + $this->connect($this->dsn, $this->options); + } else { + return new Cache_Error('No database connection. Either open a connection + using connect() or register a connection with + setConnection($dsn, $options)', + __FILE__, + __LINE__ + ); + } + } + + $_result = $this->db->query($query); + + if (!DB::isError($_result)) { + $rows = array(); + + while ($row = $_result->fetchRow(DB_FETCHMODE_ASSOC)) { + $rows[] = $row; + } + + $result = new Cache_DB_Result($rows); + + $this->save($cache_id, $result, $this->expires, 'db_cache'); + } else { + return $_result; + } + } } else { - $result = $this->db->query($query); + if (!isset($this->db)) { + if (!empty($this->dsn)) { + $this->connect($this->dsn, $this->options); + $result = $this->db->query($query); + } else { + return new Cache_Error('No database connection. Either open a connection + using connect() or register a connection with + setConnection($dsn, $options)', + __FILE__, + __LINE__ + ); + } + } } return $result; @@ -102,7 +137,7 @@ * @module Cache_DB * @modulegroup Cache_DB * @package Cache -* @version $Revision: 1.2 $ +* @version $Revision: 1.3 $ * @access public */ class Cache_DB_Result {

« previous php.pear.cvs (#515) next »