[pLog-svn] r7199 - in plog/branches/lifetype-1.2/class/cache: . Cache_Lite

jondaley at devel.lifetype.net jondaley at devel.lifetype.net
Thu Apr 25 04:43:42 EDT 2013


Author: jondaley
Date: 2013-04-25 04:43:42 -0400 (Thu, 25 Apr 2013)
New Revision: 7199

Modified:
   plog/branches/lifetype-1.2/class/cache/Cache_Lite/Lite.php
   plog/branches/lifetype-1.2/class/cache/cache.class.php
Log:
upgraded cache_lite to 1.7.15.  I didn't re-apply the is_dir() call, since I'm not sure if we really need it - maybe we need it if the tmp directory is erased?  I did apply our documentation and logging changes

Modified: plog/branches/lifetype-1.2/class/cache/Cache_Lite/Lite.php
===================================================================
--- plog/branches/lifetype-1.2/class/cache/Cache_Lite/Lite.php	2013-03-06 20:17:52 UTC (rev 7198)
+++ plog/branches/lifetype-1.2/class/cache/Cache_Lite/Lite.php	2013-04-25 08:43:42 UTC (rev 7199)
@@ -21,8 +21,8 @@
 *
 * @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>
+* @author Markus Tacker <tacker at php.net>
 */
 
 define('CACHE_LITE_ERROR_RETURN', 1);
@@ -275,6 +275,14 @@
     *     'hashedDirectoryUmask' => umask for hashed directory structure (int),
     *     'errorHandlingAPIBreak' => API break for better error handling ? (boolean)
     * );
+    * 
+    * If sys_get_temp_dir() is available and the 
+    * 'cacheDir' option is not provided in the 
+    * constructor options array its output is used 
+    * to determine the suitable temporary directory.
+    * 
+    * @see http://de.php.net/sys_get_temp_dir
+    * @see http://pear.php.net/bugs/bug.php?id=18328
     *
     * @param array $options options
     * @access public
@@ -284,6 +292,9 @@
         foreach($options as $key => $value) {
             $this->setOption($key, $value);
         }
+        if (!isset($options['cacheDir']) && function_exists('sys_get_temp_dir')) {
+        	$this->setOption('cacheDir', sys_get_temp_dir() . DIRECTORY_SEPARATOR);
+        }
     }
     
     /**
@@ -377,12 +388,9 @@
                     return true;
                 }
             }
-            if ($this->_automaticCleaningFactor>0) {
-                $rand = rand(1, $this->_automaticCleaningFactor);
-                if ($rand==1) {
-                    $this->clean(false, 'old');
-                }
-            }
+            if ($this->_automaticCleaningFactor>0 && ($this->_automaticCleaningFactor==1 || mt_rand(1, $this->_automaticCleaningFactor)==1)) {
+				$this->clean(false, 'old');			
+			}
             if ($this->_writeControl) {
                 $res = $this->_writeAndControl($data);
                 if (is_bool($res)) {
@@ -397,11 +405,11 @@
                 $res = $this->_write($data);
             }
             if (is_object($res)) {
-	        	// $res is a PEAR_Error object 
+                // $res is a PEAR_Error object 
                 if (!($this->_errorHandlingAPIBreak)) {   
-	                return false; // we return false (old API)
-	            }
-	        }
+                    return false; // we return false (old API)
+                }
+            }
             return $res;
         }
         return false;
@@ -412,10 +420,11 @@
     *
     * @param string $id cache id
     * @param string $group name of the cache group
+    * @param boolean $checkbeforeunlink check if file exists before removing it
     * @return boolean true if no problem
     * @access public
     */
-    function remove($id, $group = 'default')
+    function remove($id, $group = 'default', $checkbeforeunlink = false)
     {
         $this->_setFileName($id, $group);
         if ($this->_memoryCaching) {
@@ -427,6 +436,9 @@
                 return true;
             }
         }
+        if ( $checkbeforeunlink ) {
+            if (!file_exists($this->_file)) return true;
+        }
         return $this->_unlink($this->_file);
     }
 
@@ -484,7 +496,7 @@
         if ($this->_caching) {
             $array = array(
                 'counter' => $this->_memoryCachingCounter,
-                'array' => $this->_memoryCachingState
+                'array' => $this->_memoryCachingArray
             );
             $data = serialize($array);
             $this->save($data, $id, $group);
@@ -577,7 +589,7 @@
     */
     function _unlink($file)
     {
-        if (file_exists($file) && !@unlink($file)) {
+        if (!@unlink($file)) {
             return $this->raiseError('Cache_Lite : Unable to remove cache !', -3);
         }
         return true;        
@@ -601,8 +613,8 @@
             $motif = ($group) ? 'cache_'.$group.'_' : 'cache_';
         }
         if ($this->_memoryCaching) {
-            while (list($key, ) = each($this->_memoryCachingArray)) {
-                if (strpos($key, $motif, 0)) {
+	    foreach($this->_memoryCachingArray as $key => $v) {
+                if (strpos($key, $motif) !== false) {
                     unset($this->_memoryCachingArray[$key]);
                     $this->_memoryCachingCounter = $this->_memoryCachingCounter - 1;
                 }
@@ -615,7 +627,7 @@
             return $this->raiseError('Cache_Lite : Unable to open cache directory !', -4);
         }
         $result = true;
-        while ($file = readdir($dh)) {
+        while (($file = readdir($dh)) !== false) {
             if (($file != '.') && ($file != '..')) {
                 if (substr($file, 0, 6)=='cache_') {
                     $file2 = $dir . $file;
@@ -624,13 +636,13 @@
                             case 'old':
                                 // files older than lifeTime get deleted from cache
                                 if (!is_null($this->_lifeTime)) {
-                                    if ((mktime() - @filemtime($file2)) > $this->_lifeTime) {
+                                    if ((time() - @filemtime($file2)) > $this->_lifeTime) {
                                         $result = ($result and ($this->_unlink($file2)));
                                     }
                                 }
                                 break;
                             case 'notingrou':
-                                if (!strpos($file2, $motif, 0)) {
+                                if (strpos($file2, $motif) === false) {
                                     $result = ($result and ($this->_unlink($file2)));
                                 }
                                 break;
@@ -642,7 +654,7 @@
                                 break;
                             case 'ingroup':
                             default:
-                                if (strpos($file2, $motif, 0)) {
+                                if (strpos($file2, $motif) !== false) {
                                     $result = ($result and ($this->_unlink($file2)));
                                 }
                                 break;
@@ -709,22 +721,30 @@
     function _read()
     {
         $fp = @fopen($this->_file, "rb");
-        if ($this->_fileLocking) @flock($fp, LOCK_SH);
         if ($fp) {
+	    if ($this->_fileLocking) @flock($fp, LOCK_SH);
             clearstatcache();
             $length = @filesize($this->_file);
             $mqr = get_magic_quotes_runtime();
-            set_magic_quotes_runtime(0);
+            if ($mqr) {
+                set_magic_quotes_runtime(0);
+            }
             if ($this->_readControl) {
                 $hashControl = @fread($fp, 32);
                 $length = $length - 32;
-            } 
+            }
+
             if ($length) {
-                $data = @fread($fp, $length);
+                $data = '';
+                // See https://bugs.php.net/bug.php?id=30936
+                // The 8192 magic number is the chunk size used internally by PHP.
+                while(!feof($fp)) $data .= fread($fp, 8192);
             } else {
                 $data = '';
             }
-            set_magic_quotes_runtime($mqr);
+            if ($mqr) {
+                set_magic_quotes_runtime($mqr);
+            }
             if ($this->_fileLocking) @flock($fp, LOCK_UN);
             @fclose($fp);
             if ($this->_readControl) {
@@ -752,7 +772,7 @@
     */
     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++) {
@@ -768,8 +788,14 @@
             if ($this->_readControl) {
                 @fwrite($fp, $this->_hash($data, $this->_readControlType), 32);
             }
-            $len = strlen($data);
-            @fwrite($fp, $data, $len);
+            $mqr = get_magic_quotes_runtime();
+            if ($mqr) {
+                set_magic_quotes_runtime(0);
+            }
+            @fwrite($fp, $data);
+            if ($mqr) {
+                set_magic_quotes_runtime($mqr);
+            }
             if ($this->_fileLocking) @flock($fp, LOCK_UN);
             @fclose($fp);
             return true;
@@ -792,7 +818,7 @@
         }
         $dataRead = $this->_read();
         if (is_object($dataRead)) {
-            return $result; # We return the PEAR_Error object
+            return $dataRead; # We return the PEAR_Error object
         }
         if ((is_bool($dataRead)) && (!$dataRead)) {
             return false; 
@@ -823,5 +849,3 @@
     }
     
 } 
-
-?>

Modified: plog/branches/lifetype-1.2/class/cache/cache.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/cache/cache.class.php	2013-03-06 20:17:52 UTC (rev 7198)
+++ plog/branches/lifetype-1.2/class/cache/cache.class.php	2013-04-25 08:43:42 UTC (rev 7199)
@@ -139,7 +139,7 @@
 		 */
         function removeData( $id, $group )
         {
-			return $this->cache->remove( $id, $group );
+			return $this->cache->remove( $id, $group, true );
         }
 
 		/**



More information about the pLog-svn mailing list