Merge "add since tags"
[lhc/web/wiklou.git] / includes / objectcache / MemcachedClient.php
index 51d312f..9602ffe 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 /**
+ * Memcached client for PHP.
+ *
  * +---------------------------------------------------------------------------+
  * | memcached client, PHP                                                     |
  * +---------------------------------------------------------------------------+
@@ -338,12 +340,32 @@ class MWMemcached {
                        $this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) );
                }
 
-               if ( $res == "DELETED" ) {
+               if ( $res == "DELETED" || $res == "NOT_FOUND" ) {
                        return true;
                }
+
                return false;
        }
 
+       /**
+        * @param $key
+        * @param $timeout int
+        * @return bool
+        */
+       public function lock( $key, $timeout = 0 ) {
+               /* stub */
+               return true;
+       }
+
+       /**
+        * @param $key
+        * @return bool
+        */
+       public function unlock( $key ) {
+               /* stub */
+               return true;
+       }
+
        // }}}
        // {{{ disconnect_all()
 
@@ -386,7 +408,7 @@ class MWMemcached {
        /**
         * Retrieves the value associated with the key from the memcache server
         *
-        * @param $key Mixed: key to retrieve
+        * @param $key array|string key to retrieve
         *
         * @return Mixed
         */
@@ -409,6 +431,7 @@ class MWMemcached {
                        return false;
                }
 
+               $key = is_array( $key ) ? $key[1] : $key;
                if ( isset( $this->stats['get'] ) ) {
                        $this->stats['get']++;
                } else {
@@ -460,7 +483,7 @@ class MWMemcached {
                        $this->stats['get_multi'] = 1;
                }
                $sock_keys = array();
-
+               $socks = array();
                foreach ( $keys as $key ) {
                        $sock = $this->get_sock( $key );
                        if ( !is_resource( $sock ) ) {
@@ -474,6 +497,7 @@ class MWMemcached {
                        $sock_keys[$sock][] = $key;
                }
 
+               $gather = array();
                // Send out the requests
                foreach ( $socks as $sock ) {
                        $cmd = 'get';
@@ -568,6 +592,7 @@ class MWMemcached {
                        return array();
                }
 
+               $ret = array();
                while ( true ) {
                        $res = fgets( $sock );
                        $ret[] = $res;
@@ -733,6 +758,9 @@ class MWMemcached {
                $this->_dead_host( $host );
        }
 
+       /**
+        * @param $host
+        */
        function _dead_host( $host ) {
                $parts = explode( ':', $host );
                $ip = $parts[0];
@@ -763,8 +791,8 @@ class MWMemcached {
                }
 
                $hv = is_array( $key ) ? intval( $key[0] ) : $this->_hashfunc( $key );
-
                if ( $this->_buckets === null ) {
+                       $bu = array();
                        foreach ( $this->_servers as $v ) {
                                if ( is_array( $v ) ) {
                                        for( $i = 0; $i < $v[1]; $i++ ) {
@@ -804,7 +832,7 @@ class MWMemcached {
         * @access private
         */
        function _hashfunc( $key ) {
-               # Hash function must on [0,0x7ffffff]
+               # Hash function must be in [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;
@@ -816,9 +844,9 @@ class MWMemcached {
        /**
         * Perform increment/decriment on $key
         *
-        * @param $cmd String: command to perform
-        * @param $key String: key to perform it on
-        * @param $amt Integer: amount to adjust
+        * @param $cmd String command to perform
+        * @param $key String|array key to perform it on
+        * @param $amt Integer amount to adjust
         *
         * @return Integer: new value of $key
         * @access private
@@ -840,7 +868,8 @@ class MWMemcached {
                        $this->stats[$cmd] = 1;
                }
                if ( !$this->_safe_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
-                       return $this->_dead_sock( $sock );
+                       $this->_dead_sock( $sock );
+                       return null;
                }
 
                $line = fgets( $sock );
@@ -860,6 +889,7 @@ class MWMemcached {
         * @param $sock Resource: socket to read from
         * @param $ret Array: returned values
         *
+        * @return bool|int
         * @access private
         */
        function _load_items( $sock, &$ret ) {
@@ -948,12 +978,6 @@ class MWMemcached {
                } else {
                        $this->stats[$cmd] = 1;
                }
-               
-               // Memcached doesn't seem to handle very high TTL values very well,
-               // so clamp them at 30 days
-               if ( $exp > 2592000 ) {
-                       $exp = 2592000;
-               }
 
                $flags = 0;
 
@@ -983,7 +1007,8 @@ class MWMemcached {
                        }
                }
                if ( !$this->_safe_fwrite( $sock, "$cmd $key $flags $exp $len\r\n$val\r\n" ) ) {
-                       return $this->_dead_sock( $sock );
+                       $this->_dead_sock( $sock );
+                       return false;
                }
 
                $line = trim( fgets( $sock ) );
@@ -1023,7 +1048,8 @@ class MWMemcached {
                }
 
                if ( !$this->_connect_sock( $sock, $host ) ) {
-                       return $this->_dead_host( $host );
+                       $this->_dead_host( $host );
+                       return null;
                }
 
                // Do not buffer writes
@@ -1034,6 +1060,9 @@ class MWMemcached {
                return $this->_cache_sock[$host];
        }
 
+       /**
+        * @param $str string
+        */
        function _debugprint( $str ) {
                print( $str );
        }
@@ -1065,6 +1094,10 @@ class MWMemcached {
 
        /**
         * Original behaviour
+        * @param $f
+        * @param $buf
+        * @param $len bool
+        * @return int
         */
        function _safe_fwrite( $f, $buf, $len = false ) {
                if ( $len === false ) {
@@ -1077,6 +1110,7 @@ class MWMemcached {
 
        /**
         * Flush the read buffer of a stream
+        * @param $f Resource
         */
        function _flush_read_buffer( $f ) {
                if ( !is_resource( $f ) ) {
@@ -1099,7 +1133,13 @@ class MWMemcached {
 // }}}
 
 class MemCachedClientforWiki extends MWMemcached {
+
        function _debugprint( $text ) {
-               wfDebug( "memcached: $text" );
+               global $wgDebugLogGroups;
+               if( !isset( $wgDebugLogGroups['memcached'] ) ) {
+                       # Prefix message since it will end up in main debug log file
+                       $text = "memcached: $text";
+               }
+               wfDebugLog( 'memcached', $text );
        }
 }