[pLog-svn] r2209 - plog/trunk/class/cache/Cache_Lite

ork at devel.plogworld.net ork at devel.plogworld.net
Sun Jun 12 09:04:17 GMT 2005


Author: ork
Date: 2005-06-12 09:04:16 +0000 (Sun, 12 Jun 2005)
New Revision: 2209

Modified:
   plog/trunk/class/cache/Cache_Lite/Lite.php
Log:
some reformatting, removed some unneccessary stuff and enabled memory caching if more than 24M is assigned to php. 
furthermore rewrote the error throwing to just report to the log file and don't throw an error to the user.


Modified: plog/trunk/class/cache/Cache_Lite/Lite.php
===================================================================
--- plog/trunk/class/cache/Cache_Lite/Lite.php	2005-06-12 00:08:57 UTC (rev 2208)
+++ plog/trunk/class/cache/Cache_Lite/Lite.php	2005-06-12 09:04:16 UTC (rev 2209)
@@ -26,10 +26,12 @@
 * @author Fabien MARTY <fab at php.net>
 */
 
+require_once( PLOG_CLASS_PATH . 'class/object/object.class.php' );
+
 define('CACHE_LITE_ERROR_RETURN', 1);
 define('CACHE_LITE_ERROR_DIE', 8);
 
-class Cache_Lite
+class Cache_Lite extends Object
 {
 
     // --- Private properties ---
@@ -121,16 +123,6 @@
     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
@@ -265,6 +257,10 @@
             }
         }
         $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;
     }
     
     /**
@@ -283,20 +279,19 @@
         $data = false;
         if ($this->_caching) {
             $this->_setFileName($id, $group);
-//            :TODO: we won't have memory caching at all .. 
-//            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;
-//                    }
-//                }
-//            }
+            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;
+                    }
+                }
+            }
             if ($doNotTestCacheValidity) {
                 if (file_exists($this->_file)) {
                     $data = $this->_read();
@@ -306,9 +301,9 @@
                     $data = $this->_read();
                 }
             }
-//            if (($data) and ($this->_memoryCaching)) {
-//                $this->_memoryCacheAdd($this->_file, $data);
-//            }
+            if (($data) and ($this->_memoryCaching)) {
+                $this->_memoryCacheAdd($this->_file, $data);
+            }
             if (($this->_automaticSerialization) and (is_string($data))) {
                 $data = unserialize($data);
             }
@@ -335,18 +330,17 @@
             if (isset($id)) {
                 $this->_setFileName($id, $group);
             }
-//            :TODO: we won't have memory caching at all..
-//            if ($this->_memoryCaching) {
-//                $this->_memoryCacheAdd($this->_file, $data);
-//                if ($this->_onlyMemoryCaching) {
-//                    return true;
-//                }
-//            }
-	    if ($this->_automaticCleaningFactor>0) {
+            if ($this->_memoryCaching) {
+                $this->_memoryCacheAdd($this->_file, $data);
+                if ($this->_onlyMemoryCaching) {
+                    return true;
+                }
+            }
+            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)) {
@@ -356,8 +350,8 @@
                     return true;
                 }
             } else {
-	        return $this->_write($data);
-	    }
+                return $this->_write($data);
+            }
         }
         return false;
     }
@@ -373,16 +367,15 @@
     function remove($id, $group = 'default')
     {
         $this->_setFileName($id, $group);
-//        :TODO: we won't have memory caching at all..
-//        if ($this->_memoryCaching) {
-//            if (isset($this->_memoryCachingArray[$this->_file])) {
-//                unset($this->_memoryCachingArray[$this->_file]);
-//                $this->_memoryCachingCounter = $this->_memoryCachingCounter - 1;
-//            }
-//            if ($this->_onlyMemoryCaching) {
-//                return true;
-//            }
-//        }
+        if ($this->_memoryCaching) {
+            if (isset($this->_memoryCachingArray[$this->_file])) {
+                unset($this->_memoryCachingArray[$this->_file]);
+                $this->_memoryCachingCounter = $this->_memoryCachingCounter - 1;
+            }
+            if ($this->_onlyMemoryCaching) {
+                return true;
+            }
+        }
         return $this->_unlink($this->_file);
     }
 
@@ -476,23 +469,6 @@
         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');
-        PEAR::raiseError($msg, $code, $this->_pearErrorMode);
-    }
-    
     // --- Private methods ---
 
     /**
@@ -505,7 +481,7 @@
     function _unlink($file)
     {
         if (file_exists($file) && !@unlink($file)) {
-            $this->raiseError('Cache_Lite : Unable to remove cache !', -3);
+            $this->log->log('Unable to remove cache !', LOGGER_PRIO_ERROR );
             return false;
         } else {
             return true;
@@ -540,7 +516,7 @@
             }
         }
         if (!($dh = opendir($dir))) {
-            $this->raiseError('Cache_Lite : Unable to open cache directory !', -4);
+            $this->log->log('Unable to open cache directory!', LOGGER_PRIO_ERROR );
             return false;
         }
         $result = true;
@@ -647,6 +623,9 @@
             @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)); 
                     return false;
@@ -654,7 +633,7 @@
             }
             return $data;
         }
-        $this->raiseError('Cache_Lite : Unable to read cache !', -2);   
+        $this->log->log( 'Unable to read cache !', LOGGER_PRIO_ERROR );   
         return false;
     }
     
@@ -672,7 +651,11 @@
         if ($fp) {
             if ($this->_fileLocking) @flock($fp, LOCK_EX);
             if ($this->_readControl) {
-                @fwrite($fp, $this->_hash($data, $this->_readControlType), 32);
+                $hashData = $this->_hash($data, $this->_readControlType);
+                if ($hasData) 
+                    @fwrite($fp, $hasData, 32);
+                else
+                    return false;
             }
             $len = strlen($data);
             @fwrite($fp, $data, $len);
@@ -680,7 +663,7 @@
             @fclose($fp);
             return true;
         }
-        $this->raiseError('Cache_Lite : Unable to write cache file : '.$this->_file, -1);
+        $this->log->log('Unable to write cache file : '.$this->_file, LOGGER_PRIO_ERROR );
         return false;
     }
 
@@ -730,9 +713,27 @@
         case 'strlen':
             return sprintf('% 32d', strlen($data));
         default:
-            $this->raiseError('Unknown controlType ! (available values are only \'md5\', \'crc32\', \'strlen\')', -5);
+            $this->log->log('Unknown cache controlType! Caching will be disabled 
+                            (available values are only \'md5\', \'crc32\', \'strlen\')',
+                            LOGGER_PRIO_ERROR );
+            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;
+    }
     
 } 
 




More information about the pLog-svn mailing list