[pLog-svn] r4301 -
plog/branches/lifetype-1.1.3/class/cache/Cache_Lite
jondaley at devel.lifetype.net
jondaley at devel.lifetype.net
Thu Nov 16 01:53:35 GMT 2006
Author: jondaley
Date: 2006-11-16 01:53:34 +0000 (Thu, 16 Nov 2006)
New Revision: 4301
Modified:
plog/branches/lifetype-1.1.3/class/cache/Cache_Lite/Lite.php
Log:
updated to version 1.7.2
Modified: plog/branches/lifetype-1.1.3/class/cache/Cache_Lite/Lite.php
===================================================================
--- plog/branches/lifetype-1.1.3/class/cache/Cache_Lite/Lite.php 2006-11-13 19:28:35 UTC (rev 4300)
+++ plog/branches/lifetype-1.1.3/class/cache/Cache_Lite/Lite.php 2006-11-16 01:53:34 UTC (rev 4301)
@@ -1,35 +1,32 @@
<?php
-include_once( PLOG_CLASS_PATH."class/object/loggable.class.php" );
+/**
+* Fast, light and safe Cache Class
+*
+* Cache_Lite is a fast, light and safe cache system. It's optimized
+* for file containers. It is fast and safe (because it uses file
+* locking and/or anti-corruption tests).
+*
+* There are some examples in the 'docs/examples' file
+* Technical choices are described in the 'docs/technical' file
+*
+* Memory Caching is from an original idea of
+* Mike BENOIT <ipso at snappymail.ca>
+*
+* Nota : A chinese documentation (thanks to RainX <china_1982 at 163.com>) is
+* available at :
+* http://rainx.phpmore.com/manual/cache_lite.html
+*
+* @package Cache_Lite
+* @category Caching
+* @version $Id: Lite.php,v 1.45 2006/06/03 08:10:33 fab Exp $
+* @author Fabien MARTY <fab at php.net>
+*/
define('CACHE_LITE_ERROR_RETURN', 1);
define('CACHE_LITE_ERROR_DIE', 8);
-/**
- * \ingroup Cache
- *
- * Fast, light and safe Cache Class
- *
- * Cache_Lite is a fast, light and safe cache system. It's optimized
- * for file containers. It is fast and safe (because it uses file
- * locking and/or anti-corruption tests).
- *
- * There are some examples in the 'docs/examples' file
- * Technical choices are described in the 'docs/technical' file
- *
- * Memory Caching is from an original idea of
- * Mike BENOIT <ipso at snappymail.ca>
- *
- * Nota : A chinese documentation (thanks to RainX <china_1982 at 163.com>) is
- * available at :
- * http://rainx.phpmore.com/manual/cache_lite.html
- *
- * @package Cache_Lite
- * @category Caching
- * @version $Id: Lite.php,v 1.37 2005/11/24 20:10:01 fab Exp $
- * @author Fabien MARTY <fab at php.net>
- */
-class Cache_Lite extends Loggable
+class Cache_Lite
{
// --- Private properties ---
@@ -239,6 +236,18 @@
*/
var $_hashedDirectoryUmask = 0700;
+ /**
+ * API break for error handling in CACHE_LITE_ERROR_RETURN mode
+ *
+ * In CACHE_LITE_ERROR_RETURN mode, error handling was not good because
+ * for example save() method always returned a boolean (a PEAR_Error object
+ * would be better in CACHE_LITE_ERROR_RETURN mode). To correct this without
+ * breaking the API, this option (false by default) can change this handling.
+ *
+ * @var boolean
+ */
+ var $_errorHandlingAPIBreak = false;
+
// --- Public methods ---
/**
@@ -258,10 +267,11 @@
* 'onlyMemoryCaching' => enable / disable only memory caching (boolean),
* 'memoryCachingLimit' => max nbr of records to store into memory caching (int),
* 'fileNameProtection' => enable / disable automatic file name protection (boolean),
- * 'automaticSerialization' => enable / disable automatic serialization (boolean)
- * 'automaticCleaningFactor' => distable / tune automatic cleaning process (int)
- * 'hashedDirectoryLevel' => level of the hashed directory system (int)
- * 'hashedDirectoryUmask' => umask for hashed directory structure (int)
+ * 'automaticSerialization' => enable / disable automatic serialization (boolean),
+ * 'automaticCleaningFactor' => distable / tune automatic cleaning process (int),
+ * 'hashedDirectoryLevel' => level of the hashed directory system (int),
+ * 'hashedDirectoryUmask' => umask for hashed directory structure (int),
+ * 'errorHandlingAPIBreak' => API break for better error handling ? (boolean)
* );
*
* @param array $options options
@@ -269,12 +279,9 @@
*/
function Cache_Lite($options = array(NULL))
{
- $this->Loggable();
-
foreach($options as $key => $value) {
$this->setOption($key, $value);
}
- $this->_setRefreshTime();
}
/**
@@ -286,8 +293,9 @@
* @var mixed $value value of the option
* @access public
*/
- function setOption($name, $value) {
- $availableOptions = array('hashedDirectoryUmask', 'hashedDirectoryLevel', 'automaticCleaningFactor', 'automaticSerialization', 'fileNameProtection', 'memoryCaching', 'onlyMemoryCaching', 'memoryCachingLimit', 'cacheDir', 'caching', 'lifeTime', 'fileLocking', 'writeControl', 'readControl', 'readControlType', 'pearErrorMode');
+ function setOption($name, $value)
+ {
+ $availableOptions = array('errorHandlingAPIBreak', 'hashedDirectoryUmask', 'hashedDirectoryLevel', 'automaticCleaningFactor', 'automaticSerialization', 'fileNameProtection', 'memoryCaching', 'onlyMemoryCaching', 'memoryCachingLimit', 'cacheDir', 'caching', 'lifeTime', 'fileLocking', 'writeControl', 'readControl', 'readControlType', 'pearErrorMode');
if (in_array($name, $availableOptions)) {
$property = '_'.$name;
$this->$property = $value;
@@ -300,7 +308,7 @@
* @param string $id cache id
* @param string $group name of the cache group
* @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
- * @return string data of the cache (or false if no cache available)
+ * @return string data of the cache (else : false)
* @access public
*/
function get($id, $group = 'default', $doNotTestCacheValidity = false)
@@ -309,19 +317,19 @@
$this->_group = $group;
$data = false;
if ($this->_caching) {
+ $this->_setRefreshTime();
$this->_setFileName($id, $group);
+ clearstatcache();
if ($this->_memoryCaching) {
if (isset($this->_memoryCachingArray[$this->_file])) {
if ($this->_automaticSerialization) {
return unserialize($this->_memoryCachingArray[$this->_file]);
- } else {
- return $this->_memoryCachingArray[$this->_file];
}
- } else {
- if ($this->_onlyMemoryCaching) {
- return false;
- }
+ return $this->_memoryCachingArray[$this->_file];
}
+ if ($this->_onlyMemoryCaching) {
+ return false;
+ }
}
if (($doNotTestCacheValidity) || (is_null($this->_refreshTime))) {
if (file_exists($this->_file)) {
@@ -349,7 +357,7 @@
* @param string $data data to put in cache (can be another type than strings if automaticSerialization is on)
* @param string $id cache id
* @param string $group name of the cache group
- * @return boolean true if no problem
+ * @return boolean true if no problem (else : false or a PEAR_Error object)
* @access public
*/
function save($data, $id = NULL, $group = 'default')
@@ -367,22 +375,32 @@
return true;
}
}
- if ($this->_automaticCleaningFactor>0) {
+ if ($this->_automaticCleaningFactor>0) {
$rand = rand(1, $this->_automaticCleaningFactor);
- if ($rand==1) {
- $this->clean(false, 'old');
- }
+ if ($rand==1) {
+ $this->clean(false, 'old');
+ }
}
if ($this->_writeControl) {
- if (!$this->_writeAndControl($data)) {
+ $res = $this->_writeAndControl($data);
+ if (is_bool($res)) {
+ if ($res) {
+ return true;
+ }
+ // if $res if false, we need to invalidate the cache
@touch($this->_file, time() - 2*abs($this->_lifeTime));
return false;
- } else {
- return true;
- }
+ }
} else {
- return $this->_write($data);
- }
+ $res = $this->_write($data);
+ }
+ if (is_object($res)) {
+ // $res is a PEAR_Error object
+ if (!($this->_errorHandlingAPIBreak)) {
+ return false; // we return false (old API)
+ }
+ }
+ return $res;
}
return false;
}
@@ -431,13 +449,13 @@
* Set to debug mode
*
* When an error is found, the script will stop and the message will be displayed
- * (in debug mode only).
+ * (in debug mode only).
*
* @access public
*/
function setToDebug()
{
- $this->_pearErrorMode = CACHE_LITE_ERROR_DIE;
+ $this->setOption('pearErrorMode', CACHE_LITE_ERROR_DIE);
}
/**
@@ -497,10 +515,40 @@
*
* @return int last modification time
*/
- function lastModified() {
+ function lastModified()
+ {
return @filemtime($this->_file);
}
+ /**
+ * Trigger a PEAR error
+ *
+ * To improve performances, the PEAR.php file is included dynamically.
+ * The file is so included only when an error is triggered. So, in most
+ * cases, the file isn't included and perfs are much better.
+ *
+ * @param string $msg error message
+ * @param int $code error code
+ * @access public
+ */
+ function raiseError($msg, $code)
+ {
+ include_once('PEAR.php');
+ return PEAR::raiseError($msg, $code, $this->_pearErrorMode);
+ }
+
+ /**
+ * Extend the life of a valid cache file
+ *
+ * see http://pear.php.net/bugs/bug.php?id=6681
+ *
+ * @access public
+ */
+ function extendLife()
+ {
+ @touch($this->_file);
+ }
+
// --- Private methods ---
/**
@@ -508,7 +556,8 @@
*
* @access private
*/
- function _setRefreshTime() {
+ function _setRefreshTime()
+ {
if (is_null($this->_lifeTime)) {
$this->_refreshTime = null;
} else {
@@ -525,12 +574,10 @@
*/
function _unlink($file)
{
- if (file_exists($file) && !@unlink($file)) {
- $this->log->error('Unable to remove cache !', LOGGER_PRIO_ERROR );
- return false;
- } else {
- return true;
+ if (!@unlink($file)) {
+ return $this->raiseError('Cache_Lite : Unable to remove cache !', -3);
}
+ return true;
}
/**
@@ -562,8 +609,7 @@
}
}
if (!($dh = opendir($dir))) {
- $this->log->error('Unable to open cache directory!', LOGGER_PRIO_ERROR );
- return false;
+ return $this->raiseError('Cache_Lite : Unable to open cache directory !', -4);
}
$result = true;
while ($file = readdir($dh)) {
@@ -575,12 +621,12 @@
case 'old':
// files older than lifeTime get deleted from cache
if (!is_null($this->_lifeTime)) {
- if ((mktime() - filemtime($file2)) > $this->_lifeTime) {
+ if ((mktime() - @filemtime($file2)) > $this->_lifeTime) {
$result = ($result and ($this->_unlink($file2)));
}
}
break;
- case 'notingroup':
+ case 'notingrou':
if (!strpos($file2, $motif, 0)) {
$result = ($result and ($this->_unlink($file2)));
}
@@ -654,7 +700,7 @@
/**
* Read the cache file and return the content
*
- * @return string content of the cache file
+ * @return string content of the cache file (else : false or a PEAR_Error object)
* @access private
*/
function _read()
@@ -662,7 +708,7 @@
$fp = @fopen($this->_file, "rb");
if ($this->_fileLocking) @flock($fp, LOCK_SH);
if ($fp) {
- clearstatcache(); // because the filesize can be cached by PHP itself...
+ clearstatcache();
$length = @filesize($this->_file);
$mqr = get_magic_quotes_runtime();
set_magic_quotes_runtime(0);
@@ -681,7 +727,7 @@
if ($this->_readControl) {
$hashData = $this->_hash($data, $this->_readControlType);
if ($hashData != $hashControl) {
- if (is_null($this->_lifeTime)) {
+ if (!(is_null($this->_lifeTime))) {
@touch($this->_file, time() - 2*abs($this->_lifeTime));
} else {
@unlink($this->_file);
@@ -691,28 +737,28 @@
}
return $data;
}
- $this->log->error( 'Unable to read cache !', LOGGER_PRIO_ERROR );
- return false;
+ return $this->raiseError('Cache_Lite : Unable to read cache !', -2);
}
/**
* Write the given data in the cache file
*
* @param string $data data to put in cache
- * @return boolean true if ok
+ * @return boolean true if ok (a PEAR_Error object else)
* @access private
*/
function _write($data)
{
- if(!is_dir(dirname($this->_file)) && ($this->_hashedDirectoryLevel>0)) {
+ if ($this->_hashedDirectoryLevel > 0) {
$hash = md5($this->_fileName);
$root = $this->_cacheDir;
for ($i=0 ; $i<$this->_hashedDirectoryLevel ; $i++) {
$root = $root . 'cache_' . substr($hash, 0, $i + 1) . '/';
- if(!is_dir($root)) @mkdir($root, $this->_hashedDirectoryUmask);
- }
+ if (!(@is_dir($root))) {
+ @mkdir($root, $this->_hashedDirectoryUmask);
+ }
+ }
}
-
$fp = @fopen($this->_file, "wb");
if ($fp) {
if ($this->_fileLocking) @flock($fp, LOCK_EX);
@@ -724,22 +770,30 @@
if ($this->_fileLocking) @flock($fp, LOCK_UN);
@fclose($fp);
return true;
- }
- $this->log->error('Unable to write cache file : '.$this->_file, LOGGER_PRIO_ERROR );
- return false;
+ }
+ return $this->raiseError('Cache_Lite : Unable to write cache file : '.$this->_file, -1);
}
/**
* Write the given data in the cache file and control it just after to avoir corrupted cache entries
*
* @param string $data data to put in cache
- * @return boolean true if the test is ok
+ * @return boolean true if the test is ok (else : false or a PEAR_Error object)
* @access private
*/
function _writeAndControl($data)
{
- $this->_write($data);
- $dataRead = $this->_read($data);
+ $result = $this->_write($data);
+ if (is_object($result)) {
+ return $result; # We return the PEAR_Error object
+ }
+ $dataRead = $this->_read();
+ if (is_object($dataRead)) {
+ return $result; # We return the PEAR_Error object
+ }
+ if ((is_bool($dataRead)) && (!$dataRead)) {
+ return false;
+ }
return ($dataRead==$data);
}
@@ -761,12 +815,8 @@
case 'strlen':
return sprintf('% 32d', strlen($data));
default:
- $this->log->error('Unknown cache controlType! Caching will be disabled
- (available values are only \'md5\', \'crc32\', \'strlen\')',
- LOGGER_PRIO_ERROR );
- return false;
+ return $this->raiseError('Unknown controlType ! (available values are only \'md5\', \'crc32\', \'strlen\')', -5);
}
- return false;
}
}
More information about the pLog-svn
mailing list