* Fix explicit s-maxage=0 on raw pages; should help with proxy issues in
[lhc/web/wiklou.git] / includes / memcached-client.php
index bc7ce1c..697509e 100644 (file)
@@ -43,9 +43,9 @@
  * Usage example:
  *
  * require_once 'memcached.php';
- * 
+ *
  * $mc = new memcached(array(
- *              'servers' => array('127.0.0.1:10000', 
+ *              'servers' => array('127.0.0.1:10000',
  *                                 array('192.0.0.1:10010', 2),
  *                                 '127.0.0.1:10020'),
  *              'debug'   => false,
@@ -105,7 +105,7 @@ class memcached
     * @access  public
     */
    var $stats;
-   
+
    // }}}
    // {{{ private
 
@@ -116,7 +116,7 @@ class memcached
     * @access  private
     */
    var $_cache_sock;
-   
+
    /**
     * Current debug status; 0 - none to 9 - profiling
     *
@@ -124,7 +124,7 @@ class memcached
     * @access  private
     */
    var $_debug;
-   
+
    /**
     * Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'
     *
@@ -132,7 +132,7 @@ class memcached
     * @access  private
     */
    var $_host_dead;
-   
+
    /**
     * Is compression available?
     *
@@ -140,7 +140,7 @@ class memcached
     * @access  private
     */
    var $_have_zlib;
-   
+
    /**
     * Do we want to use compression?
     *
@@ -148,7 +148,7 @@ class memcached
     * @access  private
     */
    var $_compress_enable;
-   
+
    /**
     * At how many bytes should we compress?
     *
@@ -156,7 +156,7 @@ class memcached
     * @access  private
     */
    var $_compress_threshold;
-   
+
    /**
     * Are we using persistant links?
     *
@@ -164,7 +164,7 @@ class memcached
     * @access  private
     */
    var $_persistant;
-   
+
    /**
     * If only using one server; contains ip:port to connect to
     *
@@ -172,7 +172,7 @@ class memcached
     * @access  private
     */
    var $_single_sock;
-   
+
    /**
     * Array containing ip:port or array(ip:port, weight)
     *
@@ -180,7 +180,7 @@ class memcached
     * @access  private
     */
    var $_servers;
-   
+
    /**
     * Our bit buckets
     *
@@ -188,7 +188,7 @@ class memcached
     * @access  private
     */
    var $_buckets;
-   
+
    /**
     * Total # of bit buckets we have
     *
@@ -196,7 +196,7 @@ class memcached
     * @access  private
     */
    var $_bucketcount;
-   
+
    /**
     * # of total servers we have
     *
@@ -244,7 +244,7 @@ class memcached
       $this->_persistant = array_key_exists('persistant', $args) ? (@$args['persistant']) : false;
       $this->_compress_enable = true;
       $this->_have_zlib = function_exists("gzcompress");
-      
+
       $this->_cache_sock = array();
       $this->_host_dead = array();
 
@@ -256,7 +256,7 @@ class memcached
    // {{{ add()
 
    /**
-    * Adds a key/value to the memcache server if one isn't already set with 
+    * Adds a key/value to the memcache server if one isn't already set with
     * that key
     *
     * @param   string   $key     Key to set with data
@@ -304,13 +304,13 @@ class memcached
    {
       if (!$this->_active)
          return false;
-         
+
       $sock = $this->get_sock($key);
       if (!is_resource($sock))
          return false;
-      
+
       $key = is_array($key) ? $key[1] : $key;
-      
+
       @$this->stats['delete']++;
       $cmd = "delete $key $time\r\n";
       if(!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
@@ -319,10 +319,10 @@ class memcached
          return false;
       }
       $res = trim(fgets($sock));
-      
+
       if ($this->_debug)
          $this->_debugprint(sprintf("MemCache: delete %s (%s)\n", $key, $res));
-      
+
       if ($res == "DELETED")
          return true;
       return false;
@@ -392,16 +392,16 @@ class memcached
         wfProfileOut( $fname );
          return false;
       }
-         
+
       $sock = $this->get_sock($key);
-      
+
       if (!is_resource($sock)) {
         wfProfileOut( $fname );
          return false;
       }
-         
+
       @$this->stats['get']++;
-      
+
       $cmd = "get $key\r\n";
       if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
       {
@@ -409,10 +409,10 @@ class memcached
         wfProfileOut( $fname );
          return false;
       }
-      
+
       $val = array();
       $this->_load_items($sock, $val);
-      
+
       if ($this->_debug)
          foreach ($val as $k => $v)
             $this->_debugprint(@sprintf("MemCache: sock %s got %s => %s\r\n", serialize($sock), $k, $v));
@@ -436,9 +436,9 @@ class memcached
    {
       if (!$this->_active)
          return false;
-         
+
       $this->stats['get_multi']++;
-      
+
       foreach ($keys as $key)
       {
          $sock = $this->get_sock($key);
@@ -451,7 +451,7 @@ class memcached
          }
          $sock_keys[$sock][] = $key;
       }
-      
+
       // Send out the requests
       foreach ($socks as $sock)
       {
@@ -461,7 +461,7 @@ class memcached
             $cmd .= " ". $key;
          }
          $cmd .= "\r\n";
-         
+
          if ($this->_safe_fwrite($sock, $cmd, strlen($cmd)))
          {
             $gather[] = $sock;
@@ -470,18 +470,18 @@ class memcached
             $this->_dead_sock($sock);
          }
       }
-      
+
       // Parse responses
       $val = array();
       foreach ($gather as $sock)
       {
          $this->_load_items($sock, $val);
       }
-      
+
       if ($this->_debug)
          foreach ($val as $k => $v)
             $this->_debugprint(sprintf("MemCache: got %s => %s\r\n", $k, $v));
-            
+
       return $val;
    }
 
@@ -524,7 +524,7 @@ class memcached
    // {{{ run_command()
 
    /**
-    * Passes through $cmd to the memcache server connected by $sock; returns 
+    * Passes through $cmd to the memcache server connected by $sock; returns
     * output as an array (null array if no output)
     *
     * NOTE: due to a possible bug in how PHP reads while using fgets(), each
@@ -543,10 +543,10 @@ class memcached
    {
       if (!is_resource($sock))
          return array();
-      
+
       if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
          return array();
-         
+
       while (true)
       {
          $res = fgets($sock);
@@ -628,7 +628,7 @@ class memcached
       $this->_active = count($list);
       $this->_buckets = null;
       $this->_bucketcount = 0;
-      
+
       $this->_single_sock = null;
       if ($this->_active == 1)
          $this->_single_sock = $this->_servers[0];
@@ -639,7 +639,7 @@ class memcached
     *
     * @param   integer  $seconds Number of seconds
     * @param   integer  $microseconds  Number of microseconds
-    * 
+    *
     * @access  public
     */
    function set_timeout ($seconds, $microseconds)
@@ -647,7 +647,7 @@ class memcached
       $this->_timeout_seconds = $seconds;
       $this->_timeout_microseconds = $microseconds;
    }
-   
+
    // }}}
    // }}}
    // {{{ private methods
@@ -690,7 +690,7 @@ class memcached
       {
          $sock = @fsockopen($ip, $port, $errno, $errstr, $timeout);
       }
-      
+
       if (!$sock) {
          if ($this->_debug)
             $this->_debugprint( "Error connecting to $host: $errstr\n" );
@@ -742,9 +742,9 @@ class memcached
          $this->_flush_read_buffer($this->_single_sock);
          return $this->sock_to_host($this->_single_sock);
       }
-      
+
       $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key);
-      
+
       if ($this->_buckets === null)
       {
          foreach ($this->_servers as $v)
@@ -761,7 +761,7 @@ class memcached
          $this->_buckets = $bu;
          $this->_bucketcount = count($bu);
       }
-      
+
       $realkey = is_array($key) ? $key[1] : $key;
       for ($tries = 0; $tries<20; $tries++)
       {
@@ -773,7 +773,7 @@ class memcached
          }
          $hv += $this->_hashfunc($tries . $realkey);
       }
-      
+
       return false;
    }
 
@@ -791,7 +791,7 @@ class memcached
    function _hashfunc ($key)
    {
       # Hash function must on [0,0x7ffffff]
-      # We take the first 31 bits of the MD5 hash, which unlike the hash 
+      # We take the first 31 bits of the MD5 hash, which unlike the hash
       # function used in a previous version of this client, works
       return hexdec(substr(md5($key),0,8)) & 0x7fffffff;
    }
@@ -813,16 +813,16 @@ class memcached
    {
       if (!$this->_active)
          return null;
-         
+
       $sock = $this->get_sock($key);
       if (!is_resource($sock))
          return null;
-         
+
       $key = is_array($key) ? $key[1] : $key;
       @$this->stats[$cmd]++;
       if (!$this->_safe_fwrite($sock, "$cmd $key $amt\r\n"))
          return $this->_dead_sock($sock);
-         
+
       stream_set_timeout($sock, 1, 0);
       $line = fgets($sock);
       if (!preg_match('/^(\d+)/', $line, $match))
@@ -854,7 +854,7 @@ class memcached
             list($rkey, $flags, $len) = array($match[1], $match[2], $match[3]);
             $bneed = $len+2;
             $offset = 0;
-            
+
             while ($bneed > 0)
             {
                $data = fread($sock, $bneed);
@@ -865,7 +865,7 @@ class memcached
                $bneed -= $n;
                @$ret[$rkey] .= $data;
             }
-            
+
             if ($offset != $len+2)
             {
                // Something is borked!
@@ -876,7 +876,7 @@ class memcached
                $this->_close_sock($sock);
                return false;
             }
-            
+
             if ($this->_have_zlib && $flags & MEMCACHE_COMPRESSED)
                $ret[$rkey] = gzuncompress($ret[$rkey]);
 
@@ -885,7 +885,7 @@ class memcached
             if ($flags & MEMCACHE_SERIALIZED)
                $ret[$rkey] = unserialize($ret[$rkey]);
 
-         } else 
+         } else
          {
             $this->_debugprint("Error parsing memcached response\n");
             return 0;
@@ -911,15 +911,15 @@ class memcached
    {
       if (!$this->_active)
          return false;
-         
+
       $sock = $this->get_sock($key);
       if (!is_resource($sock))
          return false;
-         
+
       @$this->stats[$cmd]++;
-      
+
       $flags = 0;
-      
+
       if (!is_scalar($val))
       {
          $val = serialize($val);
@@ -927,15 +927,15 @@ class memcached
          if ($this->_debug)
             $this->_debugprint(sprintf("client: serializing data as it is not scalar\n"));
       }
-      
+
       $len = strlen($val);
-      
-      if ($this->_have_zlib && $this->_compress_enable && 
+
+      if ($this->_have_zlib && $this->_compress_enable &&
           $this->_compress_threshold && $len >= $this->_compress_threshold)
       {
          $c_val = gzcompress($val, 9);
          $c_len = strlen($c_val);
-         
+
          if ($c_len < $len*(1 - COMPRESSION_SAVINGS))
          {
             if ($this->_debug)
@@ -947,9 +947,9 @@ class memcached
       }
       if (!$this->_safe_fwrite($sock, "$cmd $key $flags $exp $len\r\n$val\r\n"))
          return $this->_dead_sock($sock);
-         
+
       $line = trim(fgets($sock));
-      
+
       if ($this->_debug)
       {
          if ($flags & MEMCACHE_COMPRESSED)
@@ -976,21 +976,21 @@ class memcached
    {
       if (isset($this->_cache_sock[$host]))
          return $this->_cache_sock[$host];
-      
+
       $now = time();
       list ($ip, $port) = explode (":", $host);
       if (isset($this->_host_dead[$host]) && $this->_host_dead[$host] > $now ||
           isset($this->_host_dead[$ip]) && $this->_host_dead[$ip] > $now)
          return null;
-         
+
       if (!$this->_connect_sock($sock, $host))
          return $this->_dead_sock($host);
-         
+
       // Do not buffer writes
       stream_set_write_buffer($sock, 0);
-      
+
       $this->_cache_sock[$host] = $sock;
-      
+
       return $this->_cache_sock[$host];
    }
 
@@ -1000,7 +1000,7 @@ class memcached
 
    /**
     * Write to a stream, timing out after the correct amount of time
-    * 
+    *
     * @return bool false on failure, true on success
     */
     /*