cvs: php4 /pear/Cache Container.php /pear/Cache/Container db.php dbx.php file.php phplib.php
| From: | Ulf Wendel | Date: | Wed, 02 May 2001 18:49:34 +0000 |
| Subject: | cvs: php4 /pear/Cache Container.php /pear/Cache/Container db.php dbx.php file.php phplib.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-107@lists.php.net to get a copy of this message | ||
uw Wed May 2 11:49:34 2001 EDT
Modified files:
/php4/pear/Cache Container.php
/php4/pear/Cache/Container db.php dbx.php file.php phplib.php
Log:
- added a high- and lowwater mark for the file container
... need some help with the other containers
- added default options
Index: php4/pear/Cache/Container.php diff -u php4/pear/Cache/Container.php:1.9 php4/pear/Cache/Container.php:1.10 --- php4/pear/Cache/Container.php:1.9 Thu Mar 8 12:41:39 2001 +++ php4/pear/Cache/Container.php Wed May 2 11:49:33 2001 @@ -17,7 +17,7 @@ // | Christian Stocker <chregu@nomad.ch> | // +----------------------------------------------------------------------+ // -// $Id: Container.php,v 1.9 2001/03/08 20:41:39 uw Exp $ +// $Id: Container.php,v 1.10 2001/05/02 18:49:33 uw Exp $ require_once "Cache/Error.php"; @@ -39,7 +39,7 @@ * not recommended! * * @author Ulf Wendel <ulf.wendel@phpdoc.de> -* @version $Id: Container.php,v 1.9 2001/03/08 20:41:39 uw Exp $ +* @version $Id: Container.php,v 1.10 2001/05/02 18:49:33 uw Exp $ * @package Cache * @access public * @abstract @@ -103,7 +103,38 @@ * @var string base64 or slash */ var $encoding_mode = "base64"; - + + /** + * Highwater mark - maximum space required by all cache entries. + * + * Whenever the garbage collection runs it checks the amount of space + * required by all cache entries. If it's more than n (highwater) bytes + * the garbage collection deletes as many entries as necessary to reach the + * lowwater mark. + * + * @var int + * @see lowwater + */ + var $highwater = 2048000; + + + /** + * Lowwater mark + * + * @var int + * @see highwater + */ + var $lowwater = 1536000; + + + /** + * Options that can be set in every derived class using it's constructor. + * + * @var array + */ + var $allowed_options = array("encoding_mode", "highwater", "lowwater"); + + /** * Loads a dataset from the cache. * Index: php4/pear/Cache/Container/db.php diff -u php4/pear/Cache/Container/db.php:1.10 php4/pear/Cache/Container/db.php:1.11 --- php4/pear/Cache/Container/db.php:1.10 Thu Apr 19 04:04:02 2001 +++ php4/pear/Cache/Container/db.php Wed May 2 11:49:33 2001 @@ -17,7 +17,7 @@ // | Chuck Hagenbuch <chuck@horde.org> | // +----------------------------------------------------------------------+ // -// $Id: db.php,v 1.10 2001/04/19 11:04:02 uw Exp $ +// $Id: db.php,v 1.11 2001/05/02 18:49:33 uw Exp $ require_once 'DB.php'; require_once 'Cache/Container.php'; @@ -52,7 +52,7 @@ * ) * * @author Sebastian Bergmann <sb@sebastian-bergmann.de> -* @version $Id: db.php,v 1.10 2001/04/19 11:04:02 uw Exp $ +* @version $Id: db.php,v 1.11 2001/05/02 18:49:33 uw Exp $ * @package Cache */ class Cache_Container_db extends Cache_Container { @@ -84,7 +84,7 @@ return new Cache_Error('No dsn specified!', __FILE__, __LINE__); } - $this->setOptions($options, array('dsn', 'cache_table')); + $this->setOptions($options, array_merge($this->allowed_options), array('dsn', 'cache_table'))); if (!$this->dsn) return new Cache_Error('No dsn specified!', __FILE__, __LINE__); Index: php4/pear/Cache/Container/dbx.php diff -u php4/pear/Cache/Container/dbx.php:1.4 php4/pear/Cache/Container/dbx.php:1.5 --- php4/pear/Cache/Container/dbx.php:1.4 Thu Apr 19 04:04:02 2001 +++ php4/pear/Cache/Container/dbx.php Wed May 2 11:49:33 2001 @@ -15,7 +15,7 @@ // | Authors: Christian Stocker <chregu@nomad.ch> | // +----------------------------------------------------------------------+ // -// $Id: dbx.php,v 1.4 2001/04/19 11:04:02 uw Exp $ +// $Id: dbx.php,v 1.5 2001/05/02 18:49:33 uw Exp $ require_once 'Cache/Container.php'; @@ -50,7 +50,7 @@ * ) * * @author Christian Stocker <chregu@nomad.ch> -* @version $Id: dbx.php,v 1.4 2001/04/19 11:04:02 uw Exp $ +* @version $Id: dbx.php,v 1.5 2001/05/02 18:49:33 uw Exp $ * @package Cache */ class Cache_Container_dbx extends Cache_Container { @@ -121,7 +121,7 @@ return new Cache_Error('No options specified!', __FILE__, __LINE__); } - $this->setOptions($options, array('module','host','db','username','password', 'cache_table', 'persistent')); + $this->setOptions($options, array_merge($this->allowed_options), array('module','host','db','username','password', 'cache_table', 'persistent'))); if (!$this->module) return new Cache_Error('No module specified!', __FILE__, __LINE__); Index: php4/pear/Cache/Container/file.php diff -u php4/pear/Cache/Container/file.php:1.11 php4/pear/Cache/Container/file.php:1.12 --- php4/pear/Cache/Container/file.php:1.11 Thu Apr 19 04:04:02 2001 +++ php4/pear/Cache/Container/file.php Wed May 2 11:49:33 2001 @@ -16,7 +16,7 @@ // | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: file.php,v 1.11 2001/04/19 11:04:02 uw Exp $ +// $Id: file.php,v 1.12 2001/05/02 18:49:33 uw Exp $ require_once 'Cache/Container.php'; @@ -24,7 +24,7 @@ * Stores cache contents in a file. * * @author Ulf Wendel <ulf.wendel@phpdoc.de> -* @version $Id: file.php,v 1.11 2001/04/19 11:04:02 uw Exp $ +* @version $Id: file.php,v 1.12 2001/05/02 18:49:33 uw Exp $ */ class Cache_Container_file extends Cache_Container { @@ -53,6 +53,21 @@ * @var string */ var $filename_prefix = ""; + + + /** + * List of cache entries, used within a gc run + * + * @var array + */ + var $entries; + + /** + * Total number of bytes required by all cache entries, used within a gc run. + * + * @var int + */ + var $total_size = 0; /** * Creates the cache directory if neccessary @@ -61,8 +76,8 @@ */ function Cache_Container_file($options = "") { if (is_array($options)) - $this->setOptions($options, array("cache_dir", "filename_prefix")); - + $this->setOptions($options, array_merge($this->allowed_options), array("cache_dir", "filename_prefix"))); + clearstatcache(); // make relative paths absolute for use in deconstructor. @@ -72,6 +87,9 @@ if (!file_exists($this->cache_dir) || !is_dir($this->cache_dir)) mkdir($this->cache_dir, 0755); + + $this->entries = array(); + } // end func contructor function fetch($id, $group) { @@ -176,8 +194,28 @@ $this->flushPreload(); clearstatcache(); + + $ok = $this->doGarbageCollection($maxlifetime, $this->cache_dir); + + // check the space used by the cache entries + if ($this->total_size > $this->highwater) { + + krsort($this->entries); + reset($this->entries); + + while ($this->total_size > $this->lowwater && list($lastmod, $entry) = each($this->entries)) { + if (@unlink($entry["file"])) + $this->total_size -= $entry["size"]); + else + new CacheError("Can't delete {$entry["file"]}. Check the permissions."); + } + + } + + $this->entries = array(); + $this->total_size = 0; - return $this->doGarbageCollection($maxlifetime, $this->cache_dir); + return $ok; } // end func garbageCollection /** @@ -189,7 +227,7 @@ * @throws Cache_Error */ function doGarbageCollection($maxlifetime, $dir) { - + if (!($dh = opendir($dir))) return new Cache_Error("Can't access cache directory '$dir'. Check permissions and path.", __FILE__, __LINE__); @@ -213,6 +251,9 @@ fclose($fh); $lastused = filemtime($file); + $this->entries[$lastused] = array("file" => $file, "size" => filesize($file)); + $this->total_size += filesize($file); + // remove if expired if (( ($expire && $expire <= time()) || ($lastused <= (time() - $maxlifetime)) ) && !unlink($file)) new Cache_Error("Can't unlink cache file '$file', skipping. Check permissions and path.", __FILE__, __LINE__); @@ -288,5 +329,6 @@ return $num_removed; } // end func deleteDir + } // end class file ?> Index: php4/pear/Cache/Container/phplib.php diff -u php4/pear/Cache/Container/phplib.php:1.11 php4/pear/Cache/Container/phplib.php:1.12 --- php4/pear/Cache/Container/phplib.php:1.11 Thu Apr 19 04:04:02 2001 +++ php4/pear/Cache/Container/phplib.php Wed May 2 11:49:33 2001 @@ -16,7 +16,7 @@ // | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: phplib.php,v 1.11 2001/04/19 11:04:02 uw Exp $ +// $Id: phplib.php,v 1.12 2001/05/02 18:49:33 uw Exp $ require_once 'Cache/Container.php'; @@ -50,7 +50,7 @@ * * * @author Ulf Wendel <ulf.wendel@phpdoc.de>, Sebastian Bergmann <sb@sebastian-bergmann.de> -* @version $Id: phplib.php,v 1.11 2001/04/19 11:04:02 uw Exp $ +* @version $Id: phplib.php,v 1.12 2001/05/02 18:49:33 uw Exp $ * @package Cache * @see save() */ @@ -118,7 +118,7 @@ */ function Cache_Container_phplib($options = "") { if (is_array($options)) - $this->setOptions($options, array("db_class", "db_file", "db_path", "local_file", "local_path")); + $this->setOptions($options, array_merge($this->allowed_options), array("db_class", "db_file", "db_path", "local_file", "local_path"))); if (!$this->db_class) return new Cache_Error("No database class specified.", __FILE__, __LINE__);
Index: php4/pear/Cache/Container.php diff -u php4/pear/Cache/Container.php:1.9 php4/pear/Cache/Container.php:1.10 --- php4/pear/Cache/Container.php:1.9 Thu Mar 8 12:41:39 2001 +++ php4/pear/Cache/Container.php Wed May 2 11:49:33 2001 @@ -17,7 +17,7 @@ // | Christian Stocker <chregu@nomad.ch> | // +----------------------------------------------------------------------+ // -// $Id: Container.php,v 1.9 2001/03/08 20:41:39 uw Exp $ +// $Id: Container.php,v 1.10 2001/05/02 18:49:33 uw Exp $ require_once "Cache/Error.php"; @@ -39,7 +39,7 @@ * not recommended! * * @author Ulf Wendel <ulf.wendel@phpdoc.de> -* @version $Id: Container.php,v 1.9 2001/03/08 20:41:39 uw Exp $ +* @version $Id: Container.php,v 1.10 2001/05/02 18:49:33 uw Exp $ * @package Cache * @access public * @abstract @@ -103,7 +103,38 @@ * @var string base64 or slash */ var $encoding_mode = "base64"; - + + /** + * Highwater mark - maximum space required by all cache entries. + * + * Whenever the garbage collection runs it checks the amount of space + * required by all cache entries. If it's more than n (highwater) bytes + * the garbage collection deletes as many entries as necessary to reach the + * lowwater mark. + * + * @var int + * @see lowwater + */ + var $highwater = 2048000; + + + /** + * Lowwater mark + * + * @var int + * @see highwater + */ + var $lowwater = 1536000; + + + /** + * Options that can be set in every derived class using it's constructor. + * + * @var array + */ + var $allowed_options = array("encoding_mode", "highwater", "lowwater"); + + /** * Loads a dataset from the cache. * Index: php4/pear/Cache/Container/db.php diff -u php4/pear/Cache/Container/db.php:1.10 php4/pear/Cache/Container/db.php:1.11 --- php4/pear/Cache/Container/db.php:1.10 Thu Apr 19 04:04:02 2001 +++ php4/pear/Cache/Container/db.php Wed May 2 11:49:33 2001 @@ -17,7 +17,7 @@ // | Chuck Hagenbuch <chuck@horde.org> | // +----------------------------------------------------------------------+ // -// $Id: db.php,v 1.10 2001/04/19 11:04:02 uw Exp $ +// $Id: db.php,v 1.11 2001/05/02 18:49:33 uw Exp $ require_once 'DB.php'; require_once 'Cache/Container.php'; @@ -52,7 +52,7 @@ * ) * * @author Sebastian Bergmann <sb@sebastian-bergmann.de> -* @version $Id: db.php,v 1.10 2001/04/19 11:04:02 uw Exp $ +* @version $Id: db.php,v 1.11 2001/05/02 18:49:33 uw Exp $ * @package Cache */ class Cache_Container_db extends Cache_Container { @@ -84,7 +84,7 @@ return new Cache_Error('No dsn specified!', __FILE__, __LINE__); } - $this->setOptions($options, array('dsn', 'cache_table')); + $this->setOptions($options, array_merge($this->allowed_options), array('dsn', 'cache_table'))); if (!$this->dsn) return new Cache_Error('No dsn specified!', __FILE__, __LINE__); Index: php4/pear/Cache/Container/dbx.php diff -u php4/pear/Cache/Container/dbx.php:1.4 php4/pear/Cache/Container/dbx.php:1.5 --- php4/pear/Cache/Container/dbx.php:1.4 Thu Apr 19 04:04:02 2001 +++ php4/pear/Cache/Container/dbx.php Wed May 2 11:49:33 2001 @@ -15,7 +15,7 @@ // | Authors: Christian Stocker <chregu@nomad.ch> | // +----------------------------------------------------------------------+ // -// $Id: dbx.php,v 1.4 2001/04/19 11:04:02 uw Exp $ +// $Id: dbx.php,v 1.5 2001/05/02 18:49:33 uw Exp $ require_once 'Cache/Container.php'; @@ -50,7 +50,7 @@ * ) * * @author Christian Stocker <chregu@nomad.ch> -* @version $Id: dbx.php,v 1.4 2001/04/19 11:04:02 uw Exp $ +* @version $Id: dbx.php,v 1.5 2001/05/02 18:49:33 uw Exp $ * @package Cache */ class Cache_Container_dbx extends Cache_Container { @@ -121,7 +121,7 @@ return new Cache_Error('No options specified!', __FILE__, __LINE__); } - $this->setOptions($options, array('module','host','db','username','password', 'cache_table', 'persistent')); + $this->setOptions($options, array_merge($this->allowed_options), array('module','host','db','username','password', 'cache_table', 'persistent'))); if (!$this->module) return new Cache_Error('No module specified!', __FILE__, __LINE__); Index: php4/pear/Cache/Container/file.php diff -u php4/pear/Cache/Container/file.php:1.11 php4/pear/Cache/Container/file.php:1.12 --- php4/pear/Cache/Container/file.php:1.11 Thu Apr 19 04:04:02 2001 +++ php4/pear/Cache/Container/file.php Wed May 2 11:49:33 2001 @@ -16,7 +16,7 @@ // | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: file.php,v 1.11 2001/04/19 11:04:02 uw Exp $ +// $Id: file.php,v 1.12 2001/05/02 18:49:33 uw Exp $ require_once 'Cache/Container.php'; @@ -24,7 +24,7 @@ * Stores cache contents in a file. * * @author Ulf Wendel <ulf.wendel@phpdoc.de> -* @version $Id: file.php,v 1.11 2001/04/19 11:04:02 uw Exp $ +* @version $Id: file.php,v 1.12 2001/05/02 18:49:33 uw Exp $ */ class Cache_Container_file extends Cache_Container { @@ -53,6 +53,21 @@ * @var string */ var $filename_prefix = ""; + + + /** + * List of cache entries, used within a gc run + * + * @var array + */ + var $entries; + + /** + * Total number of bytes required by all cache entries, used within a gc run. + * + * @var int + */ + var $total_size = 0; /** * Creates the cache directory if neccessary @@ -61,8 +76,8 @@ */ function Cache_Container_file($options = "") { if (is_array($options)) - $this->setOptions($options, array("cache_dir", "filename_prefix")); - + $this->setOptions($options, array_merge($this->allowed_options), array("cache_dir", "filename_prefix"))); + clearstatcache(); // make relative paths absolute for use in deconstructor. @@ -72,6 +87,9 @@ if (!file_exists($this->cache_dir) || !is_dir($this->cache_dir)) mkdir($this->cache_dir, 0755); + + $this->entries = array(); + } // end func contructor function fetch($id, $group) { @@ -176,8 +194,28 @@ $this->flushPreload(); clearstatcache(); + + $ok = $this->doGarbageCollection($maxlifetime, $this->cache_dir); + + // check the space used by the cache entries + if ($this->total_size > $this->highwater) { + + krsort($this->entries); + reset($this->entries); + + while ($this->total_size > $this->lowwater && list($lastmod, $entry) = each($this->entries)) { + if (@unlink($entry["file"])) + $this->total_size -= $entry["size"]); + else + new CacheError("Can't delete {$entry["file"]}. Check the permissions."); + } + + } + + $this->entries = array(); + $this->total_size = 0; - return $this->doGarbageCollection($maxlifetime, $this->cache_dir); + return $ok; } // end func garbageCollection /** @@ -189,7 +227,7 @@ * @throws Cache_Error */ function doGarbageCollection($maxlifetime, $dir) { - + if (!($dh = opendir($dir))) return new Cache_Error("Can't access cache directory '$dir'. Check permissions and path.", __FILE__, __LINE__); @@ -213,6 +251,9 @@ fclose($fh); $lastused = filemtime($file); + $this->entries[$lastused] = array("file" => $file, "size" => filesize($file)); + $this->total_size += filesize($file); + // remove if expired if (( ($expire && $expire <= time()) || ($lastused <= (time() - $maxlifetime)) ) && !unlink($file)) new Cache_Error("Can't unlink cache file '$file', skipping. Check permissions and path.", __FILE__, __LINE__); @@ -288,5 +329,6 @@ return $num_removed; } // end func deleteDir + } // end class file ?> Index: php4/pear/Cache/Container/phplib.php diff -u php4/pear/Cache/Container/phplib.php:1.11 php4/pear/Cache/Container/phplib.php:1.12 --- php4/pear/Cache/Container/phplib.php:1.11 Thu Apr 19 04:04:02 2001 +++ php4/pear/Cache/Container/phplib.php Wed May 2 11:49:33 2001 @@ -16,7 +16,7 @@ // | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: phplib.php,v 1.11 2001/04/19 11:04:02 uw Exp $ +// $Id: phplib.php,v 1.12 2001/05/02 18:49:33 uw Exp $ require_once 'Cache/Container.php'; @@ -50,7 +50,7 @@ * * * @author Ulf Wendel <ulf.wendel@phpdoc.de>, Sebastian Bergmann <sb@sebastian-bergmann.de> -* @version $Id: phplib.php,v 1.11 2001/04/19 11:04:02 uw Exp $ +* @version $Id: phplib.php,v 1.12 2001/05/02 18:49:33 uw Exp $ * @package Cache * @see save() */ @@ -118,7 +118,7 @@ */ function Cache_Container_phplib($options = "") { if (is_array($options)) - $this->setOptions($options, array("db_class", "db_file", "db_path", "local_file", "local_path")); + $this->setOptions($options, array_merge($this->allowed_options), array("db_class", "db_file", "db_path", "local_file", "local_path"))); if (!$this->db_class) return new Cache_Error("No database class specified.", __FILE__, __LINE__);