[xiph-commits] r14695 - branches/dir.xiph.org/inc

balbinus at svn.xiph.org balbinus at svn.xiph.org
Thu Apr 10 09:50:02 PDT 2008


Author: balbinus
Date: 2008-04-10 09:50:02 -0700 (Thu, 10 Apr 2008)
New Revision: 14695

Added:
   branches/dir.xiph.org/inc/lib.genfile.php
Log:
Caching in memcached was a bad idea. Use flat files.

Added: branches/dir.xiph.org/inc/lib.genfile.php
===================================================================
--- branches/dir.xiph.org/inc/lib.genfile.php	                        (rev 0)
+++ branches/dir.xiph.org/inc/lib.genfile.php	2008-04-10 16:50:02 UTC (rev 14695)
@@ -0,0 +1,103 @@
+<?php
+
+class genfile
+{
+    protected static $path = "/tmp";
+    
+    /**
+     * Writes a genfile on the disk, atomically (write + mv).
+     * 
+     * @param string $name The name of the genfile
+     * @param mixed $data The data to write
+     * @param bool $no_serializing
+     * @return bool
+     */
+    public static function write($name, $data, $no_serializing = false)
+    {
+        // Open the temporary file
+	    $fp = fopen(self::$path . '/' . $name . '.tmp', 'w');
+	    if (!$fp)
+	    {
+		    return false;
+	    }
+	    
+	    // Serialize the data, if needed
+	    if (!$no_serializing)
+	    {
+    	    $data = serialize($data);
+	    }
+	    
+	    // Write the data
+	    $l = fwrite($fp, $data);
+	    fclose($fp);
+	    if ($l != strlen($data))
+	    {
+		    @unlink(self::$path . '/' . $name . '.tmp');
+		
+		    return false;
+	    }
+
+	    // Move the file to its definitive location (for atomicity)
+	    if (!rename(self::$path . '/' . $name . '.tmp',
+	                self::$path . '/' . $name))
+	    {
+		    return false;
+	    }
+	    
+	    return true;
+    }
+    
+    /**
+     * Retrives a genfile.
+     * 
+     * @param string $name The name of the genfile
+     * @param bool $no_unserialize
+     * @return mixed
+     */
+    public static function get($name, $no_unserialize = false)
+    {
+        $data = @file_get_contents(self::$path . '/' . $name);
+        
+        if ($data !== false)
+        {
+            if (!$no_unserialize)
+            {
+                $data_u = @unserialize($data);
+                
+                if ($data_u === false && $data !== serialize(false))
+                {
+                    return null;
+                }
+                else
+                {
+                    $data = $data_u;
+                    unset($data_u);
+                }
+            }
+            
+            return $data;
+        }
+        else
+        {
+            return null;
+        }
+    }
+    
+    /**
+     * Make a full genfile name by prepend the environment to it.
+     * 
+     * @param string $base_name
+     * @return string
+     */
+    public static function makeGenfileName($base_name)
+    {
+        if (!defined('ENVIRONMENT'))
+        {
+            throw new EnvironmentUndefinedException();
+        }
+        
+        return sprintf("%s_%s.genfile", ENVIRONMENT, $base_name);
+    }
+}
+
+?>



More information about the commits mailing list