* Fixed unclosed <p> tag
[lhc/web/wiklou.git] / includes / memcached-client.php
index 17d9ecb..4ff0da9 100644 (file)
@@ -205,6 +205,22 @@ class memcached
     */
    var $_active;
 
+   /**
+    * Stream timeout in seconds. Applies for example to fread()
+    *
+    * @var     integer
+    * @access  private
+    */
+   var $_timeout_seconds;
+
+   /**
+    * Stream timeout in microseconds
+    *
+    * @var     integer
+    * @access  private
+    */
+   var $_timeout_microseconds;
+
    // }}}
    // }}}
    // {{{ methods
@@ -231,6 +247,9 @@ class memcached
       
       $this->_cache_sock = array();
       $this->_host_dead = array();
+
+      $this->_timeout_seconds = 1;
+      $this->_timeout_microseconds = 0;
    }
 
    // }}}
@@ -606,6 +625,20 @@ class memcached
          $this->_single_sock = $this->_servers[0];
    }
 
+   /**
+    * Sets the timeout for new connections
+    *
+    * @param   integer  $seconds Number of seconds
+    * @param   integer  $microseconds  Number of microseconds
+    * 
+    * @access  public
+    */
+   function set_timeout ($seconds, $microseconds)
+   {
+      $this->_timeout_seconds = $seconds;
+      $this->_timeout_microseconds = $microseconds;
+   }
+   
    // }}}
    // }}}
    // {{{ private methods
@@ -649,8 +682,15 @@ class memcached
          $sock = @fsockopen($ip, $port, $errno, $errstr, $timeout);
       }
       
-      if (!$sock)
+      if (!$sock) {
+         if ($this->_debug)
+            $this->_debugprint( "Error connecting to $host: $errstr\n" );
          return false;
+      }
+
+      // Initialise timeout
+      stream_set_timeout($sock, $this->_timeout_seconds, $this->_timeout_microseconds);
+
       return true;
    }
 
@@ -737,7 +777,10 @@ class memcached
     */
    function _hashfunc ($key)
    {
-      return crc32($key);
+      # Hash function must on [0,0x7ffffff]
+      # 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;
    }
 
    // }}}
@@ -763,7 +806,7 @@ class memcached
          return null;
          
       $key = is_array($key) ? $key[1] : $key;
-      $this->stats[$cmd]++;
+      @$this->stats[$cmd]++;
       if (!fwrite($sock, "$cmd $key $amt\r\n"))
          return $this->_dead_sock($sock);
          
@@ -821,11 +864,11 @@ class memcached
                return false;
             }
             
-            $ret[$rkey] = rtrim($ret[$rkey]);
-
             if ($this->_have_zlib && $flags & MEMCACHE_COMPRESSED)
                $ret[$rkey] = gzuncompress($ret[$rkey]);
 
+            $ret[$rkey] = rtrim($ret[$rkey]);
+
             if ($flags & MEMCACHE_SERIALIZED)
                $ret[$rkey] = unserialize($ret[$rkey]);
 
@@ -880,7 +923,7 @@ class memcached
          $c_val = gzcompress($val, 9);
          $c_len = strlen($c_val);
          
-         if ($c_len < $len*(1 - COMPRESS_SAVINGS))
+         if ($c_len < $len*(1 - COMPRESSION_SAVINGS))
          {
             if ($this->_debug)
                $this->_debugprint(sprintf("client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len));