cvs: php4 /pear Cache.php /pear/Cache/Container db.php dbx.php file.php phplib.php
| From: | Ulf Wendel | Date: | Thu, 19 Apr 2001 11:04:02 +0000 |
| Subject: | cvs: php4 /pear Cache.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-48@lists.php.net to get a copy of this message | ||
uw Thu Apr 19 04:04:02 2001 EDT
Modified files:
/php4/pear Cache.php
/php4/pear/Cache/Container db.php dbx.php file.php phplib.php
Log:
- fixed the garbage collection
Added some kind of LRU to delete all entries older than n seconds.
This fixes the bug that cache entries with lifetime 0 (endless) never
got removed although if they are no longer used. What's still missing
is some space limit for cache data.
Index: php4/pear/Cache.php diff -u php4/pear/Cache.php:1.9 php4/pear/Cache.php:1.10 --- php4/pear/Cache.php:1.9 Wed Mar 28 10:32:20 2001 +++ php4/pear/Cache.php Thu Apr 19 04:04:01 2001 @@ -16,7 +16,7 @@ // | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Cache.php,v 1.9 2001/03/28 18:32:20 uw Exp $ +// $Id: Cache.php,v 1.10 2001/04/19 11:04:01 uw Exp $ require_once "Cache/Error.php"; @@ -58,7 +58,7 @@ * bad circumstances (especially with the file container) * * @author Ulf Wendel <ulf.wendel@phpdoc.de> -* @version $Id: Cache.php,v 1.9 2001/03/28 18:32:20 uw Exp $ +* @version $Id: Cache.php,v 1.10 2001/04/19 11:04:01 uw Exp $ * @package Cache * @access public */ @@ -82,7 +82,7 @@ * of seconds. * * @var integer - * @see $gc_probability + * @see $gc_probability, $gc_maxlifetime * @access public */ var $gc_time = 1; @@ -93,10 +93,20 @@ * TODO: Add an explanation. * * @var integer 0 => never - * @see $gc_time + * @see $gc_time, $gc_maxlifetime * @access public */ var $gc_probability = 1; + + /** + * Garbage collection: delete all entries not use for n seconds. + * + * Default is one day, 60 * 60 * 24 = 86400 seconds. + * + * @var integer + * @see $gc_probability, $gc_time + */ + var $gc_maxlifetime = 86400; /** * Storage container object. @@ -314,7 +324,7 @@ // time and probability based if (($force) || ($last_run && $last_run < time() + $this->gc_time) || (rand(1, 100) < $this->gc_probability)) { - $this->container->garbageCollection(); + $this->container->garbageCollection($this->gc_maxlifetime); $last_run = time(); } } // end func garbageCollection Index: php4/pear/Cache/Container/db.php diff -u php4/pear/Cache/Container/db.php:1.9 php4/pear/Cache/Container/db.php:1.10 --- php4/pear/Cache/Container/db.php:1.9 Thu Mar 8 12:39:16 2001 +++ php4/pear/Cache/Container/db.php Thu Apr 19 04:04:02 2001 @@ -17,7 +17,7 @@ // | Chuck Hagenbuch <chuck@horde.org> | // +----------------------------------------------------------------------+ // -// $Id: db.php,v 1.9 2001/03/08 20:39:16 uw Exp $ +// $Id: db.php,v 1.10 2001/04/19 11:04:02 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.9 2001/03/08 20:39:16 uw Exp $ +* @version $Id: db.php,v 1.10 2001/04/19 11:04:02 uw Exp $ * @package Cache */ class Cache_Container_db extends Cache_Container { @@ -111,9 +111,25 @@ return new Cache_Error('DB::query failed: ' . DB::errorMessage($res), __FILE__, __LINE__); $row = $res->fetchRow(); - if (is_array($row)) - return array($row['expires'], $this->decode($row['cachedata']), $row['userdata']); + $data = array($row['expires'], $this->decode($row['cachedata']), $row['userdata']); + else + $data = 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->cache_table, + addslashes($id), + addslashes($group) + ); + + $res = $this->db->query($query); + + if (DB::isError($res)) + return new Cache_Error('DB::query failed: ' . DB::errorMessage($res), __FILE__, __LINE__); + + return $data; } /** @@ -197,11 +213,15 @@ } } - function garbageCollection() + function garbageCollection($maxlifetime) { - $query = sprintf('DELETE FROM %s WHERE expires <= %d AND expires > 0', + $this->flushPreload(); + + $query = sprintf('DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)', $this->cache_table, - time()); + time(), + $maxlifetime + ); $res = $this->db->query($query); @@ -209,5 +229,6 @@ return new Cache_Error('DB::query failed: ' . DB::errorMessage($res), __FILE__, __LINE__); } } + } -?> +?> \ No newline at end of file Index: php4/pear/Cache/Container/dbx.php diff -u php4/pear/Cache/Container/dbx.php:1.3 php4/pear/Cache/Container/dbx.php:1.4 --- php4/pear/Cache/Container/dbx.php:1.3 Thu Mar 22 07:09:21 2001 +++ php4/pear/Cache/Container/dbx.php Thu Apr 19 04:04:02 2001 @@ -15,7 +15,7 @@ // | Authors: Christian Stocker <chregu@nomad.ch> | // +----------------------------------------------------------------------+ // -// $Id: dbx.php,v 1.3 2001/03/22 15:09:21 uw Exp $ +// $Id: dbx.php,v 1.4 2001/04/19 11:04:02 uw Exp $ require_once 'Cache/Container.php'; @@ -50,7 +50,7 @@ * ) * * @author Christian Stocker <chregu@nomad.ch> -* @version $Id: dbx.php,v 1.3 2001/03/22 15:09:21 uw Exp $ +* @version $Id: dbx.php,v 1.4 2001/04/19 11:04:02 uw Exp $ * @package Cache */ class Cache_Container_dbx extends Cache_Container { @@ -144,15 +144,30 @@ addslashes($group) ); - $res = dbx_query($this->db,$query); - + $res = dbx_query($this->db, $query); if (dbx_error($this->db)) return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__); $row = $res->data[0]; if (is_array($row)) - return array($row['expires'], $this->decode($row['cachedata']), $row['userdata']); + $data = array($row['expires'], $this->decode($row['cachedata']), $row['userdata']); + else + $data = 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->cache_table, + addslashes($id), + addslashes($group) + ); + + $res = dbx_query($this->db, $query); + if (dbx_error($this->db)) + return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__); + + return $data; } /** @@ -237,11 +252,15 @@ } } - function garbageCollection() + function garbageCollection($maxlifetime) { - $query = sprintf('DELETE FROM %s WHERE expires <= %d AND expires > 0', + $this->flushPreload(); + + $query = sprintf('DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)', $this->cache_table, - time()); + time(), + $maxlifetime + ); $res = dbx_query($this->db, $query); @@ -250,5 +269,6 @@ return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__); } + } -?> +?> \ No newline at end of file Index: php4/pear/Cache/Container/file.php diff -u php4/pear/Cache/Container/file.php:1.10 php4/pear/Cache/Container/file.php:1.11 --- php4/pear/Cache/Container/file.php:1.10 Wed Mar 28 04:51:33 2001 +++ php4/pear/Cache/Container/file.php Thu Apr 19 04:04:02 2001 @@ -16,7 +16,7 @@ // | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: file.php,v 1.10 2001/03/28 12:51:33 uw Exp $ +// $Id: file.php,v 1.11 2001/04/19 11:04:02 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.10 2001/03/28 12:51:33 uw Exp $ +* @version $Id: file.php,v 1.11 2001/04/19 11:04:02 uw Exp $ */ class Cache_Container_file extends Cache_Container { @@ -92,6 +92,10 @@ $cachedata = $this->decode(fread($fh, filesize($file))); fclose($fh); + // last usage date used by the gc - maxlifetime + touch($file); + clearstatcache(); + return array($expire, $cachedata, $userdata); } // end func fetch @@ -165,15 +169,27 @@ * If you have a user comment for a good default gc probability please add it to * to the inline docs. * + * @param integer Maximum lifetime in seconds of an no longer used/touched entry + * @throws Cache_Error + */ + function garbageCollection($maxlifetime) { + + $this->flushPreload(); + clearstatcache(); + + return $this->doGarbageCollection($maxlifetime, $this->cache_dir); + } // end func garbageCollection + + /** + * Does the recursive gc procedure, protected. + * + * @param integer Maximum lifetime in seconds of an no longer used/touched entry * @param string directory to examine - don't sets this parameter, it's used for a * recursive function call! + * @throws Cache_Error */ - function garbageCollection($dir = "") { - $this->flushPreload(); - - if (!$dir) - $dir = $this->cache_dir; - + function doGarbageCollection($maxlifetime, $dir) { + if (!($dh = opendir($dir))) return new Cache_Error("Can't access cache directory '$dir'. Check permissions and path.", __FILE__, __LINE__); @@ -183,7 +199,7 @@ $file = $dir . $file; if (is_dir($file)) { - $this->garbageCollection($file . "/"); + $this->doGarbageCollection($file . "/"); continue; } @@ -195,9 +211,10 @@ $expire = fgets($fh, 11); fclose($fh); - + $lastused = filemtime($file); + // remove if expired - if ($expire && $expire <= time() && !unlink($file)) + 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__); } @@ -205,7 +222,8 @@ // flush the disk state cache clearstatcache(); - } // end func garbageCollection + + } // end func doGarbageCollection /** * Returns the filename for the specified id. Index: php4/pear/Cache/Container/phplib.php diff -u php4/pear/Cache/Container/phplib.php:1.10 php4/pear/Cache/Container/phplib.php:1.11 --- php4/pear/Cache/Container/phplib.php:1.10 Thu Mar 8 12:39:16 2001 +++ php4/pear/Cache/Container/phplib.php Thu Apr 19 04:04:02 2001 @@ -16,7 +16,7 @@ // | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: phplib.php,v 1.10 2001/03/08 20:39:16 uw Exp $ +// $Id: phplib.php,v 1.11 2001/04/19 11:04:02 uw Exp $ require_once 'Cache/Container.php'; @@ -30,9 +30,8 @@ * about other databases. Please add sugestions for other databases to * the inline docs. * -* The field 'changed' has no meaning for the Cache itself. It's just there -* because it's a good idea to have an automatically updated timestamp -* field for debugging in all of your tables. +* 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: * @@ -51,7 +50,7 @@ * * * @author Ulf Wendel <ulf.wendel@phpdoc.de>, Sebastian Bergmann <sb@sebastian-bergmann.de> -* @version $Id: phplib.php,v 1.10 2001/03/08 20:39:16 uw Exp $ +* @version $Id: phplib.php,v 1.11 2001/04/19 11:04:02 uw Exp $ * @package Cache * @see save() */ @@ -145,6 +144,14 @@ 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->cache_table, + $id, + $group + ); + return array($this->db->f("expires"), $this->decode($this->db->f("cachedata")), $this->db->f("userdata")); } // end func fetch @@ -207,16 +214,17 @@ return (boolean)$this->db->nf(); } // end func isExists - function garbageCollection() { + function garbageCollection($maxlifetime) { $this->flushPreload(); $this->db->query( - sprintf("DELETE FORM %s WHERE expires <= %d AND expires > 0", + sprintf("DELETE FORM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)", $this->cache_table, - time() + time(), + $maxlifetime ) ); } // end func garbageCollection } -?> +?> \ No newline at end of file
Index: php4/pear/Cache.php diff -u php4/pear/Cache.php:1.9 php4/pear/Cache.php:1.10 --- php4/pear/Cache.php:1.9 Wed Mar 28 10:32:20 2001 +++ php4/pear/Cache.php Thu Apr 19 04:04:01 2001 @@ -16,7 +16,7 @@ // | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Cache.php,v 1.9 2001/03/28 18:32:20 uw Exp $ +// $Id: Cache.php,v 1.10 2001/04/19 11:04:01 uw Exp $ require_once "Cache/Error.php"; @@ -58,7 +58,7 @@ * bad circumstances (especially with the file container) * * @author Ulf Wendel <ulf.wendel@phpdoc.de> -* @version $Id: Cache.php,v 1.9 2001/03/28 18:32:20 uw Exp $ +* @version $Id: Cache.php,v 1.10 2001/04/19 11:04:01 uw Exp $ * @package Cache * @access public */ @@ -82,7 +82,7 @@ * of seconds. * * @var integer - * @see $gc_probability + * @see $gc_probability, $gc_maxlifetime * @access public */ var $gc_time = 1; @@ -93,10 +93,20 @@ * TODO: Add an explanation. * * @var integer 0 => never - * @see $gc_time + * @see $gc_time, $gc_maxlifetime * @access public */ var $gc_probability = 1; + + /** + * Garbage collection: delete all entries not use for n seconds. + * + * Default is one day, 60 * 60 * 24 = 86400 seconds. + * + * @var integer + * @see $gc_probability, $gc_time + */ + var $gc_maxlifetime = 86400; /** * Storage container object. @@ -314,7 +324,7 @@ // time and probability based if (($force) || ($last_run && $last_run < time() + $this->gc_time) || (rand(1, 100) < $this->gc_probability)) { - $this->container->garbageCollection(); + $this->container->garbageCollection($this->gc_maxlifetime); $last_run = time(); } } // end func garbageCollection Index: php4/pear/Cache/Container/db.php diff -u php4/pear/Cache/Container/db.php:1.9 php4/pear/Cache/Container/db.php:1.10 --- php4/pear/Cache/Container/db.php:1.9 Thu Mar 8 12:39:16 2001 +++ php4/pear/Cache/Container/db.php Thu Apr 19 04:04:02 2001 @@ -17,7 +17,7 @@ // | Chuck Hagenbuch <chuck@horde.org> | // +----------------------------------------------------------------------+ // -// $Id: db.php,v 1.9 2001/03/08 20:39:16 uw Exp $ +// $Id: db.php,v 1.10 2001/04/19 11:04:02 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.9 2001/03/08 20:39:16 uw Exp $ +* @version $Id: db.php,v 1.10 2001/04/19 11:04:02 uw Exp $ * @package Cache */ class Cache_Container_db extends Cache_Container { @@ -111,9 +111,25 @@ return new Cache_Error('DB::query failed: ' . DB::errorMessage($res), __FILE__, __LINE__); $row = $res->fetchRow(); - if (is_array($row)) - return array($row['expires'], $this->decode($row['cachedata']), $row['userdata']); + $data = array($row['expires'], $this->decode($row['cachedata']), $row['userdata']); + else + $data = 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->cache_table, + addslashes($id), + addslashes($group) + ); + + $res = $this->db->query($query); + + if (DB::isError($res)) + return new Cache_Error('DB::query failed: ' . DB::errorMessage($res), __FILE__, __LINE__); + + return $data; } /** @@ -197,11 +213,15 @@ } } - function garbageCollection() + function garbageCollection($maxlifetime) { - $query = sprintf('DELETE FROM %s WHERE expires <= %d AND expires > 0', + $this->flushPreload(); + + $query = sprintf('DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)', $this->cache_table, - time()); + time(), + $maxlifetime + ); $res = $this->db->query($query); @@ -209,5 +229,6 @@ return new Cache_Error('DB::query failed: ' . DB::errorMessage($res), __FILE__, __LINE__); } } + } -?> +?> \ No newline at end of file Index: php4/pear/Cache/Container/dbx.php diff -u php4/pear/Cache/Container/dbx.php:1.3 php4/pear/Cache/Container/dbx.php:1.4 --- php4/pear/Cache/Container/dbx.php:1.3 Thu Mar 22 07:09:21 2001 +++ php4/pear/Cache/Container/dbx.php Thu Apr 19 04:04:02 2001 @@ -15,7 +15,7 @@ // | Authors: Christian Stocker <chregu@nomad.ch> | // +----------------------------------------------------------------------+ // -// $Id: dbx.php,v 1.3 2001/03/22 15:09:21 uw Exp $ +// $Id: dbx.php,v 1.4 2001/04/19 11:04:02 uw Exp $ require_once 'Cache/Container.php'; @@ -50,7 +50,7 @@ * ) * * @author Christian Stocker <chregu@nomad.ch> -* @version $Id: dbx.php,v 1.3 2001/03/22 15:09:21 uw Exp $ +* @version $Id: dbx.php,v 1.4 2001/04/19 11:04:02 uw Exp $ * @package Cache */ class Cache_Container_dbx extends Cache_Container { @@ -144,15 +144,30 @@ addslashes($group) ); - $res = dbx_query($this->db,$query); - + $res = dbx_query($this->db, $query); if (dbx_error($this->db)) return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__); $row = $res->data[0]; if (is_array($row)) - return array($row['expires'], $this->decode($row['cachedata']), $row['userdata']); + $data = array($row['expires'], $this->decode($row['cachedata']), $row['userdata']); + else + $data = 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->cache_table, + addslashes($id), + addslashes($group) + ); + + $res = dbx_query($this->db, $query); + if (dbx_error($this->db)) + return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__); + + return $data; } /** @@ -237,11 +252,15 @@ } } - function garbageCollection() + function garbageCollection($maxlifetime) { - $query = sprintf('DELETE FROM %s WHERE expires <= %d AND expires > 0', + $this->flushPreload(); + + $query = sprintf('DELETE FROM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)', $this->cache_table, - time()); + time(), + $maxlifetime + ); $res = dbx_query($this->db, $query); @@ -250,5 +269,6 @@ return new Cache_Error('DBx query failed: ' . dbx_error($this->db), __FILE__, __LINE__); } + } -?> +?> \ No newline at end of file Index: php4/pear/Cache/Container/file.php diff -u php4/pear/Cache/Container/file.php:1.10 php4/pear/Cache/Container/file.php:1.11 --- php4/pear/Cache/Container/file.php:1.10 Wed Mar 28 04:51:33 2001 +++ php4/pear/Cache/Container/file.php Thu Apr 19 04:04:02 2001 @@ -16,7 +16,7 @@ // | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: file.php,v 1.10 2001/03/28 12:51:33 uw Exp $ +// $Id: file.php,v 1.11 2001/04/19 11:04:02 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.10 2001/03/28 12:51:33 uw Exp $ +* @version $Id: file.php,v 1.11 2001/04/19 11:04:02 uw Exp $ */ class Cache_Container_file extends Cache_Container { @@ -92,6 +92,10 @@ $cachedata = $this->decode(fread($fh, filesize($file))); fclose($fh); + // last usage date used by the gc - maxlifetime + touch($file); + clearstatcache(); + return array($expire, $cachedata, $userdata); } // end func fetch @@ -165,15 +169,27 @@ * If you have a user comment for a good default gc probability please add it to * to the inline docs. * + * @param integer Maximum lifetime in seconds of an no longer used/touched entry + * @throws Cache_Error + */ + function garbageCollection($maxlifetime) { + + $this->flushPreload(); + clearstatcache(); + + return $this->doGarbageCollection($maxlifetime, $this->cache_dir); + } // end func garbageCollection + + /** + * Does the recursive gc procedure, protected. + * + * @param integer Maximum lifetime in seconds of an no longer used/touched entry * @param string directory to examine - don't sets this parameter, it's used for a * recursive function call! + * @throws Cache_Error */ - function garbageCollection($dir = "") { - $this->flushPreload(); - - if (!$dir) - $dir = $this->cache_dir; - + function doGarbageCollection($maxlifetime, $dir) { + if (!($dh = opendir($dir))) return new Cache_Error("Can't access cache directory '$dir'. Check permissions and path.", __FILE__, __LINE__); @@ -183,7 +199,7 @@ $file = $dir . $file; if (is_dir($file)) { - $this->garbageCollection($file . "/"); + $this->doGarbageCollection($file . "/"); continue; } @@ -195,9 +211,10 @@ $expire = fgets($fh, 11); fclose($fh); - + $lastused = filemtime($file); + // remove if expired - if ($expire && $expire <= time() && !unlink($file)) + 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__); } @@ -205,7 +222,8 @@ // flush the disk state cache clearstatcache(); - } // end func garbageCollection + + } // end func doGarbageCollection /** * Returns the filename for the specified id. Index: php4/pear/Cache/Container/phplib.php diff -u php4/pear/Cache/Container/phplib.php:1.10 php4/pear/Cache/Container/phplib.php:1.11 --- php4/pear/Cache/Container/phplib.php:1.10 Thu Mar 8 12:39:16 2001 +++ php4/pear/Cache/Container/phplib.php Thu Apr 19 04:04:02 2001 @@ -16,7 +16,7 @@ // | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: phplib.php,v 1.10 2001/03/08 20:39:16 uw Exp $ +// $Id: phplib.php,v 1.11 2001/04/19 11:04:02 uw Exp $ require_once 'Cache/Container.php'; @@ -30,9 +30,8 @@ * about other databases. Please add sugestions for other databases to * the inline docs. * -* The field 'changed' has no meaning for the Cache itself. It's just there -* because it's a good idea to have an automatically updated timestamp -* field for debugging in all of your tables. +* 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: * @@ -51,7 +50,7 @@ * * * @author Ulf Wendel <ulf.wendel@phpdoc.de>, Sebastian Bergmann <sb@sebastian-bergmann.de> -* @version $Id: phplib.php,v 1.10 2001/03/08 20:39:16 uw Exp $ +* @version $Id: phplib.php,v 1.11 2001/04/19 11:04:02 uw Exp $ * @package Cache * @see save() */ @@ -145,6 +144,14 @@ 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->cache_table, + $id, + $group + ); + return array($this->db->f("expires"), $this->decode($this->db->f("cachedata")), $this->db->f("userdata")); } // end func fetch @@ -207,16 +214,17 @@ return (boolean)$this->db->nf(); } // end func isExists - function garbageCollection() { + function garbageCollection($maxlifetime) { $this->flushPreload(); $this->db->query( - sprintf("DELETE FORM %s WHERE expires <= %d AND expires > 0", + sprintf("DELETE FORM %s WHERE (expires <= %d AND expires > 0) OR changed <= (NOW() - %d)", $this->cache_table, - time() + time(), + $maxlifetime ) ); } // end func garbageCollection } -?> +?> \ No newline at end of file