Source for file phplib.php
Documentation is available at phplib.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 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: phplib.php,v 1.5 2004/12/15 09:09:30 dufuz Exp $
require_once 'Cache/Container.php';
* Stores cache data into a database table using PHPLibs DB abstraction.
* WARNING: Other systems might or might not support certain datatypes of
* the tables shown. As far as I know there's no large binary
* type in SQL-92 or SQL-99. Postgres seems to lack any
* BLOB or TEXT type, for MS-SQL you could use IMAGE, don't know
* about other databases. Please add sugestions for other databases to
* The field 'changed' is used by the garbage collection. Depending on
* your databasesystem you might have to subclass fetch() and garbageCollection().
* For _MySQL_ you need this DB table:
* id CHAR(32) NOT null DEFAULT '',
* cachegroup VARCHAR(127) NOT null DEFAULT '',
* cachedata BLOB NOT null DEFAULT '',
* userdata VARCHAR(255) NOT null DEFAUL '',
* expires INT(9) NOT null DEFAULT 0,
* changed TIMESTAMP(14) NOT null,
* PRIMARY KEY (id, cachegroup)
* @author Ulf Wendel <ulf.wendel@phpdoc.de>, Sebastian Bergmann <sb@sebastian-bergmann.de>
* @version $Id: phplib.php,v 1.5 2004/12/15 09:09:30 dufuz Exp $
* Name of the DB table to store caching data
* @see Cache_Container_file::$filename_prefix
* Name of the PHPLib DB class to use
* @see $db_path, $local_path
* Filename of your local.inc
* If empty, 'local.inc' is assumed.
* Include path for you local.inc
* HINT: If your're not using PHPLib's prepend.php you must
* take care that all classes (files) references by you
* local.inc are included automatically. So you might
* want to write a new local2.inc that only referrs to
* the database class (file) you're using and includes all required files.
* @var string path to your local.inc - make sure to add a trailing slash
* Creates an instance of a phplib db class to use it for storage.
* @param mixed If empty the object tries to used the
* preconfigured class variables. If given it
* db_class => name of the DB class to use
* db_file => filename of the DB class
* db_path => path to the DB class
* local_file => kind of local.inc
* local_patk => path to the local.inc
* see $local_path for some hints.s
return new Cache_Error('No database class specified.', __FILE__ , __LINE__ );
// include the required files
include_once $this->db_path . $this->db_file;
function fetch($id, $group)
$query = sprintf("SELECT expires, cachedata, userdata FROM %s WHERE id = '%s' AND cachegroup = '%s'",
$this->db->query ($query);
if (!$this->db->Next_Record ()) {
return array (null , null , null );
// last used required by the garbage collection
// WARNING: might be MySQL specific
$query = sprintf("UPDATE %s SET changed = (NOW() + 0) WHERE id = '%s' AND cachegroup = '%s'",
$this->db->query ($query);
return array ($this->db->f ('expires'), $this->decode($this->db->f ('cachedata')), $this->db->f ('userdata'));
* WARNING: we use the SQL command REPLACE INTO this might be
* MySQL specific. As MySQL is very popular the method should
* work fine for 95% of you.
function save($id, $data, $expires, $group)
$query = sprintf("REPLACE INTO %s (cachedata, expires, id, cachegroup) VALUES ('%s', %d, '%s', '%s')",
$this->db->query ($query);
return (boolean) $this->db->affected_rows ();
sprintf("DELETE FROM %s WHERE id = '%s' AND cachegroup = '%s'",
return (boolean) $this->db->affected_rows ();
return $this->db->affected_rows ();
sprintf("SELECT id FROM %s WHERE ID = '%s' AND cachegroup = '%s'",
return (boolean) $this->db->nf ();
sprintf("DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)",
//check for total size of cache
$query = sprintf('select sum(length(cachedata)) as CacheSize from %s',
$this->db->query ($query);
$this->db->Next_Record ();
$cachesize = $this->db->f ('CacheSize');
//find the lowwater mark.
$query = sprintf('select length(cachedata) as size, changed from %s order by changed DESC',
$this->db->query ($query);
while ($keep_size < $this->lowwater && $this->db->Next_Record ()) {
$keep_size += $this->db->f ('size');
//delete all entries, which were changed before the "lowwwater mark"
$query = sprintf('delete from %s where changed <= '. $this->db->f ('changed'),
$this->db->query ($query);
} // end func garbageCollection
Documentation generated on Mon, 11 Mar 2019 15:25:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|