[Solar-svn] Revision 2977

pmjones at solarphp.com pmjones at solarphp.com
Tue Feb 26 19:59:23 CST 2008


Solar_Cache_Adapter_File

* [FIX] Don't specify byte-length in fwrite() call; the whole $data will be written regardless of length, so this helps soothe multi-byte woes.  Thanks, Antti Holvikari.  Fixes #113.

* [FIX] Honor "life = 0" as a lifetime of "forever" (vice immediate expiration).  Thanks, Rodrigo Moraes.  Fixes #114.




Modified: trunk/Solar/Cache/Adapter/File.php
===================================================================
--- trunk/Solar/Cache/Adapter/File.php	2008-01-03 14:05:49 UTC (rev 2976)
+++ trunk/Solar/Cache/Adapter/File.php	2008-02-27 01:59:23 UTC (rev 2977)
@@ -165,8 +165,12 @@
             
             // yes.  exclusive lock for writing.
             flock($fp, LOCK_EX);
-            fwrite($fp, $data, strlen($data));
             
+            // don't need the 3rd param (byte length) because Solar has
+            // already turned off magic_quotes_runtime.
+            // <http://php.net/fwrite>
+            fwrite($fp, $data);
+            
             // add a .serial file? (do this while the file is locked to avoid
             // race conditions)
             if ($serial) {
@@ -243,20 +247,22 @@
         // this avoids race conditions.
         $file = $this->entry($key);
         
-        // make sure the file exists and is readable,
-        if (file_exists($file) && is_readable($file)) {
-            // has the file expired?
-            $expire_time = filemtime($file) + $this->_config['life'];
-            if (time() > $expire_time) {
-                // expired, remove it
-                $this->delete($key);
-                return false;
-            }
-        } else {
+        // make sure the file exists and is readable
+        if (! file_exists($file) || ! is_readable($file)) {
             return false;
         }
         
-        // file exists; open it for reading
+        // make sure file is still within its lifetime
+        if ($this->_isExpired($file)) {
+            // expired, remove it
+            $this->delete($key);
+            return false;
+        }
+        
+        // the file data, if we can open the file.
+        $data = false;
+        
+        // file exists and is not expired; open it for reading
         $fp = @fopen($file, 'rb', false, $this->_context);
         
         // could it be opened?
@@ -265,10 +271,11 @@
             // lock the file right away
             flock($fp, LOCK_SH);
             
-            // get the cache entry data.
             // PHP caches file lengths; clear that out so we get
             // an accurate file length.
             clearstatcache();
+            
+            // get the cache entry data.
             $len = filesize($file);
             $data = fread($fp, $len);
             
@@ -281,12 +288,33 @@
             // unlock and close the file
             flock($fp, LOCK_UN);
             fclose($fp);
-            
-            // done!
-            return $data;
         }
         
-        // could not open file.
+        // will be false if fopen() failed, otherwise is the file contents.
+        return $data;
+    }
+    
+    /**
+     * 
+     * Checks if a file has expired (is past its lifetime) or not.
+     * 
+     * If lifetime is empty (zero), then the file never expires.
+     * 
+     * @param string $file The file name.
+     * 
+     * @return bool
+     * 
+     */
+    protected function _isExpired($file)
+    {
+        if ($this->_config['life']) {
+            $expire_time = filemtime($file) + $this->_config['life'];
+            if (time() > $expire_time) {
+                return true;
+            }
+        }
+        
+        // lifetime is forever, or not past expiration yet.
         return false;
     }
     




More information about the Solar-svn mailing list