* Fixed $wgProfileToDatabase/$wgProfileCallTree interaction (later disabled the former)
[lhc/web/wiklou.git] / includes / memcached-client.php
index 2c5cc6b..7974530 100644 (file)
  * $val = $mc->get('key');
  *
  * @author  Ryan T. Dean <rtdean@cytherianage.net>
- * @package memcached-client
  * @version 0.1.2
  */
 
 // {{{ requirements
 // }}}
 
-// {{{ constants
-// {{{ flags
-
-/**
- * Flag: indicates data is serialized
- */
-define("MEMCACHE_SERIALIZED", 1<<0);
-
-/**
- * Flag: indicates data is compressed
- */
-define("MEMCACHE_COMPRESSED", 1<<1);
-
-// }}}
-
-/**
- * Minimum savings to store data compressed
- */
-define("COMPRESSION_SAVINGS", 0.20);
-
-// }}}
-
 // {{{ class memcached
 /**
  * memcached client class implemented using (p)fsockopen()
  *
  * @author  Ryan T. Dean <rtdean@cytherianage.net>
- * @package memcached-client
+ * @ingroup Cache
  */
 class memcached
 {
    // {{{ properties
    // {{{ public
 
+               // {{{ constants
+               // {{{ flags
+
+               /**
+                * Flag: indicates data is serialized
+                */
+               const SERIALIZED = 1;
+
+               /**
+                * Flag: indicates data is compressed
+                */
+               const COMPRESSED = 2;
+
+               // }}}
+
+               /**
+                * Minimum savings to store data compressed
+                */
+               const COMPRESSION_SAVINGS = 0.20;
+
+               // }}}
+
+
    /**
     * Command statistics
     *
@@ -152,7 +152,7 @@ class memcached
    /**
     * At how many bytes should we compress?
     *
-    * @var     interger
+    * @var     integer
     * @access  private
     */
    var $_compress_threshold;
@@ -192,7 +192,7 @@ class memcached
    /**
     * Total # of bit buckets we have
     *
-    * @var     interger
+    * @var     integer
     * @access  private
     */
    var $_bucketcount;
@@ -200,7 +200,7 @@ class memcached
    /**
     * # of total servers we have
     *
-    * @var     interger
+    * @var     integer
     * @access  private
     */
    var $_active;
@@ -272,9 +272,9 @@ class memcached
     * 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
-    * @param   mixed    $val     Value to store
-    * @param   interger $exp     (optional) Time to expire data at
+    * @param   string  $key     Key to set with data
+    * @param   mixed   $val     Value to store
+    * @param   integer $exp     (optional) Time to expire data at
     *
     * @return  boolean
     * @access  public
@@ -291,7 +291,7 @@ class memcached
     * Decriment a value stored on the memcache server
     *
     * @param   string   $key     Key to decriment
-    * @param   interger $amt     (optional) Amount to decriment
+    * @param   integer  $amt     (optional) Amount to decriment
     *
     * @return  mixed    FALSE on failure, value on success
     * @access  public
@@ -308,7 +308,7 @@ class memcached
     * Deletes a key from the server, optionally after $time
     *
     * @param   string   $key     Key to delete
-    * @param   interger $time    (optional) How long to wait before deleting
+    * @param   integer  $time    (optional) How long to wait before deleting
     *
     * @return  boolean  TRUE on success, FALSE on failure
     * @access  public
@@ -401,6 +401,10 @@ class memcached
       $fname = 'memcached::get';
       wfProfileIn( $fname );
 
+      if ( $this->_debug ) {
+         $this->_debugprint( "get($key)\n" );
+      }
+
       if (!$this->_active) {
         wfProfileOut( $fname );
          return false;
@@ -428,7 +432,7 @@ class memcached
 
       if ($this->_debug)
          foreach ($val as $k => $v)
-            $this->_debugprint(@sprintf("MemCache: sock %s got %s => %s\r\n", serialize($sock), $k, $v));
+            $this->_debugprint(sprintf("MemCache: sock %s got %s\n", serialize($sock), $k));
 
       wfProfileOut( $fname );
       return @$val[$key];
@@ -450,9 +454,9 @@ class memcached
       if (!$this->_active)
          return false;
 
-      $this->stats['get_multi']++;
+      @$this->stats['get_multi']++;
       $sock_keys = array();
-      
+
       foreach ($keys as $key)
       {
          $sock = $this->get_sock($key);
@@ -494,7 +498,7 @@ class memcached
 
       if ($this->_debug)
          foreach ($val as $k => $v)
-            $this->_debugprint(sprintf("MemCache: got %s => %s\r\n", $k, $v));
+            $this->_debugprint(sprintf("MemCache: got %s\n", $k));
 
       return $val;
    }
@@ -506,9 +510,9 @@ class memcached
     * Increments $key (optionally) by $amt
     *
     * @param   string   $key     Key to increment
-    * @param   interger $amt     (optional) amount to increment
+    * @param   integer  $amt     (optional) amount to increment
     *
-    * @return  interger New key value?
+    * @return  integer  New key value?
     * @access  public
     */
    function incr ($key, $amt=1)
@@ -524,7 +528,7 @@ class memcached
     *
     * @param   string   $key     Key to set value as
     * @param   mixed    $value   Value to store
-    * @param   interger $exp     (optional) Experiation time
+    * @param   integer  $exp     (optional) Experiation time
     *
     * @return  boolean
     * @access  public
@@ -582,7 +586,7 @@ class memcached
     *
     * @param   string   $key     Key to set value as
     * @param   mixed    $value   Value to set
-    * @param   interger $exp     (optional) Experiation time
+    * @param   integer  $exp     (optional) Experiation time
     *
     * @return  boolean  TRUE on success
     * @access  public
@@ -598,7 +602,7 @@ class memcached
    /**
     * Sets the compression threshold
     *
-    * @param   interger $thresh  Threshold to compress if larger than
+    * @param   integer  $thresh  Threshold to compress if larger than
     *
     * @access  public
     */
@@ -687,7 +691,7 @@ class memcached
    /**
     * Connects $sock to $host, timing out after $timeout
     *
-    * @param   interger $sock    Socket to connect
+    * @param   integer  $sock    Socket to connect
     * @param   string   $host    Host:IP to connect to
     *
     * @return  boolean
@@ -796,8 +800,8 @@ class memcached
          if (is_resource($sock)) {
             $this->_flush_read_buffer($sock);
             return $sock;
-         }
-         $hv += $this->_hashfunc($tries . $realkey);
+                }
+         $hv = $this->_hashfunc( $hv . $realkey );
       }
 
       return false;
@@ -807,11 +811,11 @@ class memcached
    // {{{ _hashfunc()
 
    /**
-    * Creates a hash interger based on the $key
+    * Creates a hash integer  based on the $key
     *
     * @param   string   $key     Key to hash
     *
-    * @return  interger Hash value
+    * @return  integer  Hash value
     * @access  private
     */
    function _hashfunc ($key)
@@ -830,9 +834,9 @@ class memcached
     *
     * @param   string   $cmd     Command to perform
     * @param   string   $key     Key to perform it on
-    * @param   interger $amt     Amount to adjust
+    * @param   integer  $amt     Amount to adjust
     *
-    * @return  interger    New value of $key
+    * @return  integer     New value of $key
     * @access  private
     */
    function _incrdecr ($cmd, $key, $amt=1)
@@ -904,12 +908,12 @@ class memcached
                return false;
             }
 
-            if ($this->_have_zlib && $flags & MEMCACHE_COMPRESSED)
+            if ($this->_have_zlib && $flags & memcached::COMPRESSED)
                $ret[$rkey] = gzuncompress($ret[$rkey]);
 
             $ret[$rkey] = rtrim($ret[$rkey]);
 
-            if ($flags & MEMCACHE_SERIALIZED)
+            if ($flags & memcached::SERIALIZED)
                $ret[$rkey] = unserialize($ret[$rkey]);
 
          } else
@@ -929,7 +933,7 @@ class memcached
     * @param   string   $cmd     Command to perform
     * @param   string   $key     Key to act on
     * @param   mixed    $val     What we need to store
-    * @param   interger $exp     When it should expire
+    * @param   integer  $exp     When it should expire
     *
     * @return  boolean
     * @access  private
@@ -950,7 +954,7 @@ class memcached
       if (!is_scalar($val))
       {
          $val = serialize($val);
-         $flags |= MEMCACHE_SERIALIZED;
+         $flags |= memcached::SERIALIZED;
          if ($this->_debug)
             $this->_debugprint(sprintf("client: serializing data as it is not scalar\n"));
       }
@@ -963,13 +967,13 @@ class memcached
          $c_val = gzcompress($val, 9);
          $c_len = strlen($c_val);
 
-         if ($c_len < $len*(1 - COMPRESSION_SAVINGS))
+         if ($c_len < $len*(1 - memcached::COMPRESSION_SAVINGS))
          {
             if ($this->_debug)
                $this->_debugprint(sprintf("client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len));
             $val = $c_val;
             $len = $c_len;
-            $flags |= MEMCACHE_COMPRESSED;
+            $flags |= memcached::COMPRESSED;
          }
       }
       if (!$this->_safe_fwrite($sock, "$cmd $key $flags $exp $len\r\n$val\r\n"))
@@ -979,9 +983,7 @@ class memcached
 
       if ($this->_debug)
       {
-         if ($flags & MEMCACHE_COMPRESSED)
-            $val = 'compressed data';
-         $this->_debugprint(sprintf("MemCache: %s %s => %s (%s)\n", $cmd, $key, $val, $line));
+         $this->_debugprint(sprintf("%s %s (%s)\n", $cmd, $key, $line));
       }
       if ($line == "STORED")
          return true;
@@ -1085,4 +1087,3 @@ class memcached
 // vim: sts=3 sw=3 et
 
 // }}}
-?>