[pLog-svn] r2911 - in plog/trunk/class/cache: . Cache_Lite
mark at devel.lifetype.net
mark at devel.lifetype.net
Tue Feb 7 09:13:08 GMT 2006
Author: mark
Date: 2006-02-07 09:13:07 +0000 (Tue, 07 Feb 2006)
New Revision: 2911
Modified:
plog/trunk/class/cache/Cache_Lite/Lite.php
plog/trunk/class/cache/cache.class.php
plog/trunk/class/cache/cachemanager.class.php
Log:
Some cache bug fix, and also upgrade the cahe_lite to 1.6.0.
1. I move the option modification to cache.class.php. It is will be eaiser for us when we want to upgrade to cache_lite 1.7.0 next time.
2 the log command $this->log->log(blahblahblah) seems not working.
Modified: plog/trunk/class/cache/Cache_Lite/Lite.php
===================================================================
--- plog/trunk/class/cache/Cache_Lite/Lite.php 2006-02-06 17:02:51 UTC (rev 2910)
+++ plog/trunk/class/cache/Cache_Lite/Lite.php 2006-02-07 09:13:07 UTC (rev 2911)
@@ -10,19 +10,16 @@
* There are some examples in the 'docs/examples' file
* Technical choices are described in the 'docs/technical' file
*
-* A tutorial is available in english at this url :
-* http://www.pearfr.org/index.php/en/article/cache_lite
-* (big thanks to Pierre-Alain Joye for the translation)
-*
-* The same tutorial is also available in french at this url :
-* http://www.pearfr.org/index.php/fr/article/cache_lite
-*
* 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.23 2005/02/27 18:24:28 fab Exp $
+* @version $Id: Lite.php,v 1.37 2005/11/24 20:10:01 fab Exp $
* @author Fabien MARTY <fab at php.net>
*/
@@ -31,7 +28,7 @@
define('CACHE_LITE_ERROR_RETURN', 1);
define('CACHE_LITE_ERROR_DIE', 8);
-class Cache_Lite
+class Cache_Lite
{
// --- Private properties ---
@@ -56,9 +53,11 @@
/**
* Cache lifetime (in seconds)
*
+ * If null, the cache is valid forever.
+ *
* @var int $_lifeTime
*/
- var $_lifeTime = 604800;
+ var $_lifeTime = 3600;
/**
* Enable / disable fileLocking
@@ -108,7 +107,7 @@
*
* @var boolean $_writeControl
*/
- var $_readControl = false;
+ var $_readControl = true;
/**
* Type of read control (only if read control is enabled)
@@ -123,6 +122,16 @@
var $_readControlType = 'crc32';
/**
+ * Pear error mode (when raiseError is called)
+ *
+ * (see PEAR doc)
+ *
+ * @see setToDebug()
+ * @var int $_pearErrorMode
+ */
+ var $_pearErrorMode = CACHE_LITE_ERROR_RETURN;
+
+ /**
* Current cache id
*
* @var string $_id
@@ -194,7 +203,7 @@
*
* @var boolean $_serialize
*/
- var $_automaticSerialization = true;
+ var $_automaticSerialization = false;
/**
* Disable / Tune the automatic cleaning process
@@ -212,11 +221,23 @@
/**
* Nested directory level
*
+ * Set the hashed directory structure level. 0 means "no hashed directory
+ * structure", 1 means "one level of directory", 2 means "two levels"...
+ * This option can speed up Cache_Lite only when you have many thousands of
+ * cache file. Only specific benchs can help you to choose the perfect value
+ * for you. Maybe, 1 or 2 is a good start.
*
* @var int $_hashedDirectoryLevel
*/
- var $_hashedDirectoryLevel = 2;
+ var $_hashedDirectoryLevel = 0;
+ /**
+ * Umask for hashed directory structure
+ *
+ * @var int $_hashedDirectoryUmask
+ */
+ var $_hashedDirectoryUmask = 0700;
+
// --- Public methods ---
/**
@@ -239,6 +260,7 @@
* '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)
* );
*
* @param array $options options
@@ -246,24 +268,30 @@
*/
function Cache_Lite($options = array(NULL))
{
- // original options
- // $availableOptions = array('hashedDirectoryLevel', 'automaticCleaningFactor', 'automaticSerialization', 'fileNameProtection', 'memoryCaching', 'onlyMemoryCaching', 'memoryCachingLimit', 'cacheDir', 'caching', 'lifeTime', 'fileLocking', 'writeControl', 'readControl', 'readControlType', 'pearErrorMode');
- // options really needed by pLog
- $availableOptions = array('cacheDir');
foreach($options as $key => $value) {
- if(in_array($key, $availableOptions)) {
- $property = '_'.$key;
- $this->$property = $value;
- }
+ $this->setOption($key, $value);
}
- $this->_refreshTime = time() - $this->_lifeTime;
-
- // activate memory caching if available memory is 24M or above
- if( $this->byteValue(ini_get('memory_limit')) >= 25165824 )
- $this->_memoryCaching = true;
+ $this->_setRefreshTime();
}
/**
+ * Generic way to set a Cache_Lite option
+ *
+ * see Cache_Lite constructor for available options
+ *
+ * @var string $name name of the option
+ * @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');
+ if (in_array($name, $availableOptions)) {
+ $property = '_'.$name;
+ $this->$property = $value;
+ }
+ }
+
+ /**
* Test if a cache is available and (if yes) return it
*
* @param string $id cache id
@@ -292,7 +320,7 @@
}
}
}
- if ($doNotTestCacheValidity) {
+ if (($doNotTestCacheValidity) || (is_null($this->_refreshTime))) {
if (file_exists($this->_file)) {
$data = $this->_read();
}
@@ -302,7 +330,7 @@
}
}
if (($data) and ($this->_memoryCaching)) {
- $this->_memoryCacheAdd($this->_file, $data);
+ $this->_memoryCacheAdd($data);
}
if (($this->_automaticSerialization) and (is_string($data))) {
$data = unserialize($data);
@@ -331,16 +359,16 @@
$this->_setFileName($id, $group);
}
if ($this->_memoryCaching) {
- $this->_memoryCacheAdd($this->_file, $data);
+ $this->_memoryCacheAdd($data);
if ($this->_onlyMemoryCaching) {
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)) {
@@ -350,8 +378,8 @@
return true;
}
} else {
- return $this->_write($data);
- }
+ return $this->_write($data);
+ }
}
return false;
}
@@ -386,7 +414,8 @@
* else only cache files of the specified group will be destroyed
*
* @param string $group name of the cache group
- * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup'
+ * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup',
+ * 'callback_myFunction'
* @return boolean true if no problem
* @access public
*/
@@ -417,7 +446,7 @@
function setLifeTime($newLifeTime)
{
$this->_lifeTime = $newLifeTime;
- $this->_refreshTime = time() - $newLifeTime;
+ $this->_setRefreshTime();
}
/**
@@ -466,12 +495,25 @@
* @return int last modification time
*/
function lastModified() {
- return filemtime($this->_file);
+ return @filemtime($this->_file);
}
// --- Private methods ---
-
+
/**
+ * Compute & set the refresh time
+ *
+ * @access private
+ */
+ function _setRefreshTime() {
+ if (is_null($this->_lifeTime)) {
+ $this->_refreshTime = null;
+ } else {
+ $this->_refreshTime = time() - $this->_lifeTime;
+ }
+ }
+
+ /**
* Remove a file
*
* @param string $file complete file path and name
@@ -480,7 +522,7 @@
*/
function _unlink($file)
{
- if (file_exists($file) && !@unlink($file)) {
+ if (!@unlink($file)) {
$this->log->log('Unable to remove cache !', LOGGER_PRIO_ERROR );
return false;
} else {
@@ -493,7 +535,8 @@
*
* @param string $dir directory complete path (with a trailing slash)
* @param string $group name of the cache group
- * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup'
+ * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup',
+ 'callback_myFunction'
* @return boolean true if no problem
* @access private
*/
@@ -505,7 +548,7 @@
$motif = ($group) ? 'cache_'.$group.'_' : 'cache_';
}
if ($this->_memoryCaching) {
- while (list($key, $value) = each($this->_memoryCachingArray)) {
+ while (list($key, ) = each($this->_memoryCachingArray)) {
if (strpos($key, $motif, 0)) {
unset($this->_memoryCachingArray[$key]);
$this->_memoryCachingCounter = $this->_memoryCachingCounter - 1;
@@ -525,18 +568,26 @@
if (substr($file, 0, 6)=='cache_') {
$file2 = $dir . $file;
if (is_file($file2)) {
- switch ($mode) {
+ switch (substr($mode, 0, 9)) {
case 'old':
// files older than lifeTime get deleted from cache
- if ((mktime() - filemtime($file2)) > $this->_lifeTime) {
- $result = ($result and ($this->_unlink($file2)));
+ if (!is_null($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)));
}
break;
+ case 'callback_':
+ $func = substr($mode, 9, strlen($mode) - 9);
+ if ($func($file2, $group)) {
+ $result = ($result and ($this->_unlink($file2)));
+ }
+ break;
case 'ingroup':
default:
if (strpos($file2, $motif, 0)) {
@@ -557,15 +608,14 @@
/**
* Add some date in the memory caching array
*
- * @param string $id cache id
* @param string $data data to cache
* @access private
*/
- function _memoryCacheAdd($id, $data)
+ function _memoryCacheAdd($data)
{
$this->_memoryCachingArray[$this->_file] = $data;
if ($this->_memoryCachingCounter >= $this->_memoryCachingLimit) {
- list($key, $value) = each($this->_memoryCachingArray);
+ list($key, ) = each($this->_memoryCachingArray);
unset($this->_memoryCachingArray[$key]);
} else {
$this->_memoryCachingCounter = $this->_memoryCachingCounter + 1;
@@ -617,23 +667,28 @@
$hashControl = @fread($fp, 32);
$length = $length - 32;
}
- $data = @fread($fp, $length);
+ if ($length) {
+ $data = @fread($fp, $length);
+ } else {
+ $data = '';
+ }
set_magic_quotes_runtime($mqr);
if ($this->_fileLocking) @flock($fp, LOCK_UN);
@fclose($fp);
if ($this->_readControl) {
$hashData = $this->_hash($data, $this->_readControlType);
- if ($hashData == false) {
- return false;
- }
if ($hashData != $hashControl) {
- @touch($this->_file, time() - 2*abs($this->_lifeTime));
+ if (is_null($this->_lifeTime)) {
+ @touch($this->_file, time() - 2*abs($this->_lifeTime));
+ } else {
+ @unlink($this->_file);
+ }
return false;
}
}
return $data;
}
- $this->log->log( 'Unable to read cache !', LOGGER_PRIO_ERROR );
+ $this->log->log( 'Unable to read cache !', LOGGER_PRIO_ERROR );
return false;
}
@@ -646,41 +701,37 @@
*/
function _write($data)
{
- $this->_createCacheDir();
- $fp = @fopen($this->_file, 'w');
- if ($fp) {
- if ($this->_fileLocking) @flock($fp, LOCK_EX);
- if ($this->_readControl) {
- $hashData = $this->_hash($data, $this->_readControlType);
- if ($hasData)
- @fwrite($fp, $hasData, 32);
- else
- return false;
+ $try = 1;
+ while ($try<=2) {
+ $fp = @fopen($this->_file, "wb");
+ if ($fp) {
+ if ($this->_fileLocking) @flock($fp, LOCK_EX);
+ if ($this->_readControl) {
+ @fwrite($fp, $this->_hash($data, $this->_readControlType), 32);
+ }
+ $len = strlen($data);
+ @fwrite($fp, $data, $len);
+ if ($this->_fileLocking) @flock($fp, LOCK_UN);
+ @fclose($fp);
+ return true;
+ } else {
+ if (($try==1) and ($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) . '/';
+ @mkdir($root, $this->_hashedDirectoryUmask);
+ }
+ $try = 2;
+ } else {
+ $try = 999;
+ }
}
- $len = strlen($data);
- @fwrite($fp, $data, $len);
- if ($this->_fileLocking) @flock($fp, LOCK_UN);
- @fclose($fp);
- return true;
}
$this->log->log('Unable to write cache file : '.$this->_file, LOGGER_PRIO_ERROR );
return false;
}
-
- function _createCacheDir()
- {
- 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, 0700);
- }
- }
- }
- }
-
+
/**
* Write the given data in the cache file and control it just after to avoir corrupted cache entries
*
@@ -718,22 +769,8 @@
LOGGER_PRIO_ERROR );
return false;
}
+ return false;
}
-
- function byteValue($val) {
- $val = trim($val);
- $last = strtolower($val{strlen($val)-1});
- switch($last) {
- case 'g':
- $val *= 1024;
- case 'm':
- $val *= 1024;
- case 'k':
- $val *= 1024;
- }
-
- return $val;
- }
}
Modified: plog/trunk/class/cache/cache.class.php
===================================================================
--- plog/trunk/class/cache/cache.class.php 2006-02-06 17:02:51 UTC (rev 2910)
+++ plog/trunk/class/cache/cache.class.php 2006-02-07 09:13:07 UTC (rev 2911)
@@ -67,10 +67,10 @@
$__cache_queries++;
- $inCache = $this->cache->get( $id, $group );
+ $data = $this->cache->get( $id, $group );
if( DEBUG_ENABLED && DEBUG_CHANNELS & DEBUG_CHANNEL_CACHE )
- if ($inCache) {
+ if ($data) {
$this->debug->log("Cache hit for $id ($group): $data", LOGGER_PRIO_INFO );
$__cache_hits++;
}
@@ -79,7 +79,7 @@
$__cache_misses++;
}
- return $inCache;
+ return $data;
} else {
return false;
}
Modified: plog/trunk/class/cache/cachemanager.class.php
===================================================================
--- plog/trunk/class/cache/cachemanager.class.php 2006-02-06 17:02:51 UTC (rev 2910)
+++ plog/trunk/class/cache/cachemanager.class.php 2006-02-07 09:13:07 UTC (rev 2911)
@@ -10,8 +10,6 @@
define( "CACHE_USERALBUMS", "useralbums" );
define( "CACHE_USERINFO", "userinfo" );
define( "CACHE_USERIDBYNAME", "userinfo_idbyname" );
- define( "CACHE_TRACKBACKS", "trackbacks" );
- define( "CACHE_NUMTRACKBACKS", "trackbacks_size" );
define( "CACHE_CONFIGDBSTORAGE", "config_db_storage" );
define( "CACHE_SITEADMINS", "permissions_siteadmin" );
define( "CACHE_BLOGLINKS", "blogLinks" );
@@ -56,7 +54,11 @@
// define defaults
$cacheParameter = array(
'cacheDir' => "./tmp/",
- );
+ 'lifeTime' => 604800,
+ 'readControl' => false,
+ 'automaticSerialization' => true,
+ 'hashedDirectoryLevel' => 2
+ );
// build a new cache object
$cache =& new Cache( $cacheParameter);
More information about the pLog-svn
mailing list