uw Sat Mar 3 11:02:54 2001 EDT Modified files: /php4/pear/Cache/Container file.php Log: Changes for the new cache structure / features. - added delDirectory() as a helper function for flush - new file format
Index: php4/pear/Cache/Container/file.php diff -u php4/pear/Cache/Container/file.php:1.3 php4/pear/Cache/Container/file.php:1.4 --- php4/pear/Cache/Container/file.php:1.3 Fri Mar 2 06:00:43 2001 +++ php4/pear/Cache/Container/file.php Sat Mar 3 11:02:54 2001 @@ -16,7 +16,7 @@ // | Sebastian Bergmann <[EMAIL PROTECTED]> | // +----------------------------------------------------------------------+ // -// $Id: file.php,v 1.3 2001/03/02 14:00:43 uw Exp $ +// $Id: file.php,v 1.4 2001/03/03 19:02:54 uw Exp $ require_once 'Cache/Container.php'; @@ -24,7 +24,7 @@ * Stores cache contents in a file. * * @author Ulf Wendel <[EMAIL PROTECTED]> -* @version $Id: file.php,v 1.3 2001/03/02 14:00:43 uw Exp $ +* @version $Id: file.php,v 1.4 2001/03/03 19:02:54 uw Exp $ */ class Cache_Container_file extends Cache_Container { @@ -64,17 +64,19 @@ if (is_array($options)) $this->setOptions($options, array("cache_dir", "filename_prefix")); + clearstatcache(); + if (!file_exists($this->cache_dir) || !is_dir($this->cache_dir)) mkdir($this->cache_dir, 0755); } // end func contructor - function fetch($id) { + function fetch($id, $group) { - $file = $this->getFilename($id); + $file = $this->getFilename($id, $group); if (!file_exists($file)) - return array(NULL, NULL); + return array(NULL, NULL, NULL); // retrive the content if (!($fh = @fopen($file, "rb"))) @@ -82,30 +84,39 @@ // file format: // 1st line: expiration date - // 2nd+ lines: cache data + // 2nd line: user data + // 3rd+ lines: cache data $expire = trim(fgets($fh, 11)); - $data = $this->decode(fread($fh, filesize($file))); - + $userdata = trim(fgets($fh, 257)); + $cachedata = $this->decode(fread($fh, filesize($file))); fclose($fh); - return array($expire, $data); + return array($expire, $cachedata, $userdata); } // end func fetch - function save($id, $data, $expire = 0) { + /** + * Stores a dataset. + * + * WARNING: If you supply userdata it must not contain any linebreaks, + * otherwise it will break the filestructure. + */ + function save($id, $cachedata, $expires, $group, $userdata) { - $this->flushPreload($id); + $this->flushPreload($id, $group); - $file = $this->getFilename($id); + $file = $this->getFilename($id, $group); if (!($fh = @fopen($file, "wb"))) return new CacheError("Can't access '$file' to store cache data. Check access rights and path.", __FILE__, __LINE__); // file format: // 1st line: expiration date - // 2nd+ lines: cache data - $expire = ($expire) ? $expire + time() : 0; - fwrite($fh, $expire . "\r\n"); - fwrite($fh, $this->encode($data)); + // 2nd line: user data + // 3rd+ lines: cache data + $expires = ($expires) ? $expires + time() : 0; + fwrite($fh, $expires . "\n"); + fwrite($fh, $userdata . "\n"); + fwrite($fh, $this->encode($cachedata)); fclose($fh); @@ -114,50 +125,39 @@ return true; } // end func save - - function delete($id) { + function delete($id, $group) { - $this->flushPreload($id); + $this->flushPreload($id, $group); - $file = $this->getFilename($id); + $file = $this->getFilename($id, $group); if (file_exists($file)) { - - unlink($file); + + $ok = unlink($file); clearstatcache(); - return true; + return $ok; } return false; } // end func delete - function flush() { + function flush($group) { $this->flushPreload(); - - if (!($dh = opendir($this->cache_dir))) - return new CacheError("Can't access the cache directory '$this->cache_dir'. Check access rights and path", __FILE__, __LINE__); - - $num_removed = 0; - while ($file = readdir($dh)) { - if ("." == $file || ".." == $file) - continue; - - unlink($this->cache_dir . $file); - $num_removed++; - } + $dir = ($group) ? $this->cache_dir . $group . "/" : $this->cache_dir; + $num_removed = $this->deleteDir($dir); clearstatcache(); - + return $num_removed; } // end func flush - function idExists($id) { + function idExists($id, $group) { - return file_exists($this->cache_dir . $this->filename_prefix . $id); + return file_exists($this->getFilename($id, $group)); } // end func idExists @@ -171,23 +171,31 @@ * read from them and if neccessary they have to be unlinked (removed). * If you have a user comment for a good default gc probability please add it to * to the inline docs. + * + * @param string directory to examine - don't sets this parameter, it's used +for a + * recursive function call! */ - function garbageCollection() { + function garbageCollection($dir = "") { $this->flushPreload(); - - if (!($dh = opendir($this->cache_dir))) - return new CacheError("Can't access cache directory.", __FILE__, __LINE__); + + if (!$dir) + $dir = $this->cache_dir; + + if (!($dh = opendir($dir))) + return new CacheError("Can't access cache directory '$dir'. Check +permissions and path.", __FILE__, __LINE__); while ($file = readdir($dh)) { if ("." == $file || ".." == $file) continue; - $file = $this->cache_dir . $file; + $file = $dir . $file; + if (is_dir($file)) + $this->garbageCollection($file . "/"); // skip trouble makers but inform the user if (!($fh = @fopen($file, "rb"))) { - new CacheError("Can't access cache file '$file' skipping for garbage collection. Check access rights and path.", __FILE__, __LINE__); + new CacheError("Can't access cache file '$file', skipping it. Check +permissions and path.", __FILE__, __LINE__); continue; } @@ -195,8 +203,8 @@ fclose($fh); // remove if expired - if ($expire && $expire <= time()) - unlink($file); + if ($expire && $expire <= time() && !unlink($file)) + new CacheError("Can't unlink cache file '$file', skipping. Check +permissions and path.", __FILE__, __LINE__); } closedir($dh); @@ -211,12 +219,68 @@ * Returns the filename for the specified id. * * @param string dataset ID + * @param string cache group * @return string full filename with the path * @access public */ - function getFilename($id) { - return $this->cache_dir . $this->filename_prefix . $id; + function getFilename($id, $group) { + static $group_dirs = array(); + + if (isset($group_dirs[$group])) + return $group_dirs[$group] . $this->filename_prefix . $id; + + + $dir = $this->cache_dir . $group . "/"; + if (!file_exists($dir)) { + mkdir($dir, 0755); + clearstatcache(); + } + + $group_dirs[$group] = $dir; + + return $dir . $this->filename_prefix . $id; } // end func getFilename + + + /** + * Deletes a directory and all files in it. + * + * @param string directory + * @return integer number of removed files + * @throws CacheError + */ + function deleteDir($dir) { + + if (!($dh = opendir($dir))) + return new CacheError("Can't remove directory '$dir'. Check permissions +and path.", __FILE__, __LINE__); + + $num_removed = 0; + + while ($file = readdir($dh)) { + if ("." == $file || ".." == $file) + continue; -} + $file = $dir . $file; + if (is_dir($file)) { + + $file .= "/"; + $num = $this->deleteDir($file . "/"); + if (is_int($num)) + $num_removed += $num; + + } else { + + if (unlink($file)) + $num_removed++; + + } + + + } + + return $num_removed; + } // end func deleteDir + + +} // end class file ?>
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]