cvs: php4 /pear/Cache/Container db.php

From: Date: Thu, 01 Mar 2001 17:22:59 +0000
Subject: cvs: php4 /pear/Cache/Container db.php
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-4316@lists.php.net to get a copy of this message
chagenbu Thu Mar 1 09:22:59 2001 EDT Added files: /php4/pear/Cache/Container db.php Log: almost working version. Index: php4/pear/Cache/Container/db.php +++ php4/pear/Cache/Container/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: Ulf Wendel <ulf.wendel@phpdoc.de> | // | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // // $Id: db.php,v 1.1 2001/03/01 17:22:59 chagenbu Exp $ require_once 'DB.php'; require_once 'Cache/Container.php'; /** * PEAR/DB Cache Container. * * CREATE TABLE cache ( * id varchar(32) NOT NULL DEFAULT '', * data text NOT NULL DEFAULT '', * expires int(9) NOT NULL DEFAULT 0, * changed timestamp(14), * INDEX(expires), * PRIMARY KEY(id) * ) * * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @version $Id: db.php,v 1.1 2001/03/01 17:22:59 chagenbu Exp $ * @package Cache */ class Cache_Container_db extends Cache_Container { /** * Name of the DB table to store caching data * * @see Cache_Container_file::$filename_prefix */ var $cache_table = ''; /** * PEAR DB dsn to use. * * @var string */ var $dsn = ''; /** * PEAR DB object * * @var object PEAR_DB */ var $db; /** * * @param mixed */ function Cache_Container_db($options) { if (!is_array($options) || !isset($options['dsn'])) { return new CacheError('No dsn specified!', __FILE__, __LINE__); } $this->setOptions($options, array('dsn', 'cache_table')); if (!$this->dsn) return new CacheError('No dsn specified!', __FILE__, __LINE__); $this->db = DB::connect($this->dsn, true); if (DB::isError($this->db)) { return new CacheError('DB::connect failed: ' . DB::errorMessage($this->db)); } else { $this->db->setFetchMode(DB_FETCHMODE_ASSOC); } } function fetch($id) { $query = sprintf('SELECT data, expires FROM %s WHERE id = %s', $this->cache_table, "'" . $this->db->quoteString($id) . "'"); $res = $this->db->query($query); if (DB::isError($res)) return new CacheError('DB::query failed: ' . DB::errorMessage($res)); $row = $res->fetchRow(); if (is_array($row)) return array($row['expires'], $row['data']); } function save($id, $data, $expires = 0) { $query = sprintf('REPLACE INTO %s (data, expires, id) VALUES (%s, %d, %s)', $this->cache_table, "'" . $this->db->quoteString(serialize($data)) . "'", $expires, "'" . $this->db->quoteString($id) . "'"); $res = $this->db->query($query); if (DB::isError($res)) { return new CacheError('DB::query failed: ' . DB::errorMessage($res)); } } function delete($id) { $query = sprintf('DELETE FROM %s WHERE id = %s', $this->cache_table, "'" . $this->db->quoteString($id) . "'"); $res = $this->db->query($query); if (DB::isError($res)) return new CacheError('DB::query failed: ' . DB::errorMessage($res)); } function flush() { $query = sprintf('DELETE FROM %s', $this->cache_table); $res = $this->db->query($query); if (DB::isError($res)) return new CacheError('DB::query failed: ' . DB::errorMessage($res)); } function idExists($id) { $query = sprintf('SELECT id FROM %s WHERE ID = %s', $this->cache_table, "'" . $this->db->quoteString($id) . "'"); $res = $this->db->query($query); if (DB::isError($res)) return new CacheError('DB::query failed: ' . DB::errorMessage($res)); $row = $res->fetchRow(); if (is_array($row)) { return $row['id']; } else { return false; } } function garbageCollection() { $query = sprintf('DELETE FROM %s WHERE expires <= %d AND expires > 0', $this->cache_table, time()); $res = $this->db->query($query); if (DB::isError($res)) { return new CacheError('DB::query failed: ' . DB::errorMessage($res)); } } } ?>

« previous php.cvs (#4316) next »