]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blobdiff - includes/memcached-client.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / includes / memcached-client.php
index 3b0ae90d5b0b849d9cbb4e8e7d1c7d50857d61de..53f0324f7e53387150aaba5a4329676ad5467430 100644 (file)
@@ -1,40 +1,41 @@
 <?php
-//
-// +---------------------------------------------------------------------------+
-// | memcached client, PHP                                                     |
-// +---------------------------------------------------------------------------+
-// | Copyright (c) 2003 Ryan T. Dean <rtdean@cytherianage.net>                 |
-// | All rights reserved.                                                      |
-// |                                                                           |
-// | Redistribution and use in source and binary forms, with or without        |
-// | modification, are permitted provided that the following conditions        |
-// | are met:                                                                  |
-// |                                                                           |
-// | 1. Redistributions of source code must retain the above copyright         |
-// |    notice, this list of conditions and the following disclaimer.          |
-// | 2. Redistributions in binary form must reproduce the above copyright      |
-// |    notice, this list of conditions and the following disclaimer in the    |
-// |    documentation and/or other materials provided with the distribution.   |
-// |                                                                           |
-// | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR      |
-// | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
-// | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   |
-// | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,          |
-// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  |
-// | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
-// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     |
-// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       |
-// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF  |
-// | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.         |
-// +---------------------------------------------------------------------------+
-// | Author: Ryan T. Dean <rtdean@cytherianage.net>                            |
-// | Heavily influenced by the Perl memcached client by Brad Fitzpatrick.      |
-// |   Permission granted by Brad Fitzpatrick for relicense of ported Perl     |
-// |   client logic under 2-clause BSD license.                                |
-// +---------------------------------------------------------------------------+
-//
-// $TCAnet$
-//
+/**
+ * +---------------------------------------------------------------------------+
+ * | memcached client, PHP                                                     |
+ * +---------------------------------------------------------------------------+
+ * | Copyright (c) 2003 Ryan T. Dean <rtdean@cytherianage.net>                 |
+ * | All rights reserved.                                                      |
+ * |                                                                           |
+ * | Redistribution and use in source and binary forms, with or without        |
+ * | modification, are permitted provided that the following conditions        |
+ * | are met:                                                                  |
+ * |                                                                           |
+ * | 1. Redistributions of source code must retain the above copyright         |
+ * |    notice, this list of conditions and the following disclaimer.          |
+ * | 2. Redistributions in binary form must reproduce the above copyright      |
+ * |    notice, this list of conditions and the following disclaimer in the    |
+ * |    documentation and/or other materials provided with the distribution.   |
+ * |                                                                           |
+ * | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR      |
+ * | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
+ * | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   |
+ * | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,          |
+ * | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  |
+ * | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+ * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     |
+ * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       |
+ * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF  |
+ * | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.         |
+ * +---------------------------------------------------------------------------+
+ * | Author: Ryan T. Dean <rtdean@cytherianage.net>                            |
+ * | Heavily influenced by the Perl memcached client by Brad Fitzpatrick.      |
+ * |   Permission granted by Brad Fitzpatrick for relicense of ported Perl     |
+ * |   client logic under 2-clause BSD license.                                |
+ * +---------------------------------------------------------------------------+
+ *
+ * @file
+ * $TCAnet$
+ */
 
 /**
  * This is the PHP client for memcached - a distributed memory cache daemon.
@@ -239,17 +240,17 @@ class MWMemcached {
        /**
         * Memcache initializer
         *
-        * @param   array    $args    Associative array of settings
+        * @param $args Array Associative array of settings
         *
         * @return  mixed
         */
        public function __construct( $args ) {
                global $wgMemCachedTimeout;
-               $this->set_servers( @$args['servers'] );
-               $this->_debug = @$args['debug'];
+               $this->set_servers( isset( $args['servers'] ) ? $args['servers'] : array() );
+               $this->_debug = isset( $args['debug'] ) ? $args['debug'] : false;
                $this->stats = array();
-               $this->_compress_threshold = @$args['compress_threshold'];
-               $this->_persistant = array_key_exists( 'persistant', $args ) ? ( @$args['persistant'] ) : false;
+               $this->_compress_threshold = isset( $args['compress_threshold'] ) ? $args['compress_threshold'] : 0;
+               $this->_persistant = isset( $args['persistant'] ) ? $args['persistant'] : false;
                $this->_compress_enable = true;
                $this->_have_zlib = function_exists( 'gzcompress' );
 
@@ -270,11 +271,15 @@ class MWMemcached {
         * 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   integer $exp     (optional) Time to expire data at
+        * @param $key String: key to set with data
+        * @param $val Mixed: value to store
+        * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
+        * to cache for (up to 30 days inclusive).  Any timespans of 30 days + 1 second or
+        * longer must be the timestamp of the time at which the mapping should expire. It
+        * is safe to use timestamps in all cases, regardless of exipration
+        * eg: strtotime("+3 hour")
         *
-        * @return  boolean
+        * @return Boolean
         */
        public function add( $key, $val, $exp = 0 ) {
                return $this->_set( 'add', $key, $val, $exp );
@@ -284,12 +289,12 @@ class MWMemcached {
        // {{{ decr()
 
        /**
-        * Decriment a value stored on the memcache server
+        * Decrease a value stored on the memcache server
         *
-        * @param   string   $key     Key to decriment
-        * @param   integer  $amt     (optional) Amount to decriment
+        * @param $key String: key to decrease
+        * @param $amt Integer: (optional) amount to decrease
         *
-        * @return  mixed    FALSE on failure, value on success
+        * @return Mixed: FALSE on failure, value on success
         */
        public function decr( $key, $amt = 1 ) {
                return $this->_incrdecr( 'decr', $key, $amt );
@@ -301,10 +306,10 @@ class MWMemcached {
        /**
         * Deletes a key from the server, optionally after $time
         *
-        * @param   string   $key     Key to delete
-        * @param   integer  $time    (optional) How long to wait before deleting
+        * @param $key String: key to delete
+        * @param $time Integer: (optional) how long to wait before deleting
         *
-        * @return  boolean  TRUE on success, FALSE on failure
+        * @return Boolean: TRUE on success, FALSE on failure
         */
        public function delete( $key, $time = 0 ) {
                if ( !$this->_active ) {
@@ -318,7 +323,11 @@ class MWMemcached {
 
                $key = is_array( $key ) ? $key[1] : $key;
 
-               @$this->stats['delete']++;
+               if ( isset( $this->stats['delete'] ) ) {
+                       $this->stats['delete']++;
+               } else {
+                       $this->stats['delete'] = 1;
+               }
                $cmd = "delete $key $time\r\n";
                if( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
                        $this->_dead_sock( $sock );
@@ -356,7 +365,7 @@ class MWMemcached {
        /**
         * Enable / Disable compression
         *
-        * @param   boolean  $enable  TRUE to enable, FALSE to disable
+        * @param $enable Boolean: TRUE to enable, FALSE to disable
         */
        public function enable_compress( $enable ) {
                $this->_compress_enable = $enable;
@@ -378,9 +387,9 @@ class MWMemcached {
        /**
         * Retrieves the value associated with the key from the memcache server
         *
-        * @param  string   $key     Key to retrieve
+        * @param $key Mixed: key to retrieve
         *
-        * @return  mixed
+        * @return Mixed
         */
        public function get( $key ) {
                wfProfileIn( __METHOD__ );
@@ -401,7 +410,11 @@ class MWMemcached {
                        return false;
                }
 
-               @$this->stats['get']++;
+               if ( isset( $this->stats['get'] ) ) {
+                       $this->stats['get']++;
+               } else {
+                       $this->stats['get'] = 1;
+               }
 
                $cmd = "get $key\r\n";
                if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
@@ -429,16 +442,20 @@ class MWMemcached {
        /**
         * Get multiple keys from the server(s)
         *
-        * @param   array    $keys    Keys to retrieve
+        * @param $keys Array: keys to retrieve
         *
-        * @return  array
+        * @return Array
         */
        public function get_multi( $keys ) {
                if ( !$this->_active ) {
                        return false;
                }
 
-               @$this->stats['get_multi']++;
+               if ( isset( $this->stats['get_multi'] ) ) {
+                       $this->stats['get_multi']++;
+               } else {
+                       $this->stats['get_multi'] = 1;
+               }
                $sock_keys = array();
 
                foreach ( $keys as $key ) {
@@ -490,10 +507,12 @@ class MWMemcached {
        /**
         * Increments $key (optionally) by $amt
         *
-        * @param   string   $key     Key to increment
-        * @param   integer  $amt     (optional) amount to increment
+        * @param $key String: key to increment
+        * @param $amt Integer: (optional) amount to increment
         *
-        * @return  integer  New key value?
+        * @return Integer: null if the key does not exist yet (this does NOT
+        * create new mappings if the key does not exist). If the key does
+        * exist, this returns the new value for that key.
         */
        public function incr( $key, $amt = 1 ) {
                return $this->_incrdecr( 'incr', $key, $amt );
@@ -505,11 +524,15 @@ class MWMemcached {
        /**
         * Overwrites an existing value for key; only works if key is already set
         *
-        * @param   string   $key     Key to set value as
-        * @param   mixed    $value   Value to store
-        * @param   integer  $exp     (optional) Experiation time
+        * @param $key String: key to set value as
+        * @param $value Mixed: value to store
+        * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
+        * to cache for (up to 30 days inclusive).  Any timespans of 30 days + 1 second or
+        * longer must be the timestamp of the time at which the mapping should expire. It
+        * is safe to use timestamps in all cases, regardless of exipration
+        * eg: strtotime("+3 hour")
         *
-        * @return  boolean
+        * @return Boolean
         */
        public function replace( $key, $value, $exp = 0 ) {
                return $this->_set( 'replace', $key, $value, $exp );
@@ -528,13 +551,12 @@ class MWMemcached {
         *       with a \n.  This is with the PHP flag auto_detect_line_endings set
         *       to falase (the default).
         *
-        * @param   resource $sock    Socket to send command on
-        * @param   string   $cmd     Command to run
+        * @param $sock Ressource: socket to send command on
+        * @param $cmd String: command to run
         *
-        * @return  array    Output array
-        * @access  public
+        * @return Array: output array
         */
-       function run_command( $sock, $cmd ) {
+       public function run_command( $sock, $cmd ) {
                if ( !is_resource( $sock ) ) {
                        return array();
                }
@@ -563,11 +585,15 @@ class MWMemcached {
         * Unconditionally sets a key to a given value in the memcache.  Returns true
         * if set successfully.
         *
-        * @param   string   $key     Key to set value as
-        * @param   mixed    $value   Value to set
-        * @param   integer  $exp     (optional) Experiation time
+        * @param $key String: key to set value as
+        * @param $value Mixed: value to set
+        * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
+        * to cache for (up to 30 days inclusive).  Any timespans of 30 days + 1 second or
+        * longer must be the timestamp of the time at which the mapping should expire. It
+        * is safe to use timestamps in all cases, regardless of exipration
+        * eg: strtotime("+3 hour")
         *
-        * @return  boolean  TRUE on success
+        * @return Boolean: TRUE on success
         */
        public function set( $key, $value, $exp = 0 ) {
                return $this->_set( 'set', $key, $value, $exp );
@@ -579,7 +605,7 @@ class MWMemcached {
        /**
         * Sets the compression threshold
         *
-        * @param   integer  $thresh  Threshold to compress if larger than
+        * @param $thresh Integer: threshold to compress if larger than
         */
        public function set_compress_threshold( $thresh ) {
                $this->_compress_threshold = $thresh;
@@ -591,7 +617,7 @@ class MWMemcached {
        /**
         * Sets the debug flag
         *
-        * @param   boolean  $dbg     TRUE for debugging, FALSE otherwise
+        * @param $dbg Boolean: TRUE for debugging, FALSE otherwise
         *
         * @see     MWMemcached::__construct
         */
@@ -605,7 +631,7 @@ class MWMemcached {
        /**
         * Sets the server list to distribute key gets and puts between
         *
-        * @param   array    $list    Array of servers to connect to
+        * @param $list Array of servers to connect to
         *
         * @see     MWMemcached::__construct()
         */
@@ -624,8 +650,8 @@ class MWMemcached {
        /**
         * Sets the timeout for new connections
         *
-        * @param   integer  $seconds Number of seconds
-        * @param   integer  $microseconds  Number of microseconds
+        * @param $seconds Integer: number of seconds
+        * @param $microseconds Integer: number of microseconds
         */
        public function set_timeout( $seconds, $microseconds ) {
                $this->_timeout_seconds = $seconds;
@@ -640,7 +666,7 @@ class MWMemcached {
        /**
         * Close the specified socket
         *
-        * @param   string   $sock    Socket to close
+        * @param $sock String: socket to close
         *
         * @access  private
         */
@@ -656,8 +682,8 @@ class MWMemcached {
        /**
         * Connects $sock to $host, timing out after $timeout
         *
-        * @param   integer  $sock    Socket to connect
-        * @param   string   $host    Host:IP to connect to
+        * @param $sock Integer: socket to connect
+        * @param $host String: Host:IP to connect to
         *
         * @return  boolean
         * @access  private
@@ -668,11 +694,13 @@ class MWMemcached {
                $timeout = $this->_connect_timeout;
                $errno = $errstr = null;
                for( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
+                       wfSuppressWarnings();
                        if ( $this->_persistant == 1 ) {
-                               $sock = @pfsockopen( $ip, $port, $errno, $errstr, $timeout );
+                               $sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
                        } else {
-                               $sock = @fsockopen( $ip, $port, $errno, $errstr, $timeout );
+                               $sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
                        }
+                       wfRestoreWarnings();
                }
                if ( !$sock ) {
                        if ( $this->_debug ) {
@@ -693,7 +721,7 @@ class MWMemcached {
        /**
         * Marks a host as dead until 30-40 seconds in the future
         *
-        * @param   string   $sock    Socket to mark as dead
+        * @param $sock String: socket to mark as dead
         *
         * @access  private
         */
@@ -703,7 +731,8 @@ class MWMemcached {
        }
 
        function _dead_host( $host ) {
-               @list( $ip, /* $port */) = explode( ':', $host );
+               $parts = explode( ':', $host );
+               $ip = $parts[0];
                $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
                $this->_host_dead[$host] = $this->_host_dead[$ip];
                unset( $this->_cache_sock[$host] );
@@ -715,10 +744,10 @@ class MWMemcached {
        /**
         * get_sock
         *
-        * @param   string   $key     Key to retrieve value for;
+        * @param $key String: key to retrieve value for;
         *
-        * @return  mixed    resource on success, false on failure
-        * @access  private
+        * @return Mixed: resource on success, false on failure
+        * @access private
         */
        function get_sock( $key ) {
                if ( !$this->_active ) {
@@ -764,12 +793,12 @@ class MWMemcached {
        // {{{ _hashfunc()
 
        /**
-        * Creates a hash integer       based on the $key
+        * Creates a hash integer based on the $key
         *
-        * @param   string   $key     Key to hash
+        * @param $key String: key to hash
         *
-        * @return  integer  Hash value
-        * @access  private
+        * @return Integer: hash value
+        * @access private
         */
        function _hashfunc( $key ) {
                # Hash function must on [0,0x7ffffff]
@@ -784,12 +813,12 @@ class MWMemcached {
        /**
         * Perform increment/decriment on $key
         *
-        * @param   string   $cmd     Command to perform
-        * @param   string   $key     Key to perform it on
-        * @param   integer  $amt     Amount to adjust
+        * @param $cmd String: command to perform
+        * @param $key String: key to perform it on
+        * @param $amt Integer: amount to adjust
         *
-        * @return  integer     New value of $key
-        * @access  private
+        * @return Integer: new value of $key
+        * @access private
         */
        function _incrdecr( $cmd, $key, $amt = 1 ) {
                if ( !$this->_active ) {
@@ -802,7 +831,11 @@ class MWMemcached {
                }
 
                $key = is_array( $key ) ? $key[1] : $key;
-               @$this->stats[$cmd]++;
+               if ( isset( $this->stats[$cmd] ) ) {
+                       $this->stats[$cmd]++;
+               } else {
+                       $this->stats[$cmd] = 1;
+               }
                if ( !$this->_safe_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
                        return $this->_dead_sock( $sock );
                }
@@ -821,10 +854,10 @@ class MWMemcached {
        /**
         * Load items into $ret from $sock
         *
-        * @param   resource $sock    Socket to read from
-        * @param   array    $ret     Returned values
+        * @param $sock Ressource: socket to read from
+        * @param $ret Array: returned values
         *
-        * @access  private
+        * @access private
         */
        function _load_items( $sock, &$ret ) {
                while ( 1 ) {
@@ -844,7 +877,11 @@ class MWMemcached {
                                        }
                                        $offset += $n;
                                        $bneed -= $n;
-                                       @$ret[$rkey] .= $data;
+                                       if ( isset( $ret[$rkey] ) ) {
+                                               $ret[$rkey] .= $data;
+                                       } else {
+                                               $ret[$rkey] = $data;
+                                       }
                                }
 
                                if ( $offset != $len + 2 ) {
@@ -881,13 +918,17 @@ class MWMemcached {
        /**
         * Performs the requested storage operation to the memcache server
         *
-        * @param   string   $cmd     Command to perform
-        * @param   string   $key     Key to act on
-        * @param   mixed    $val     What we need to store
-        * @param   integer  $exp     When it should expire
+        * @param $cmd String: command to perform
+        * @param $key String: key to act on
+        * @param $val Mixed: what we need to store
+        * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
+        * to cache for (up to 30 days inclusive).  Any timespans of 30 days + 1 second or
+        * longer must be the timestamp of the time at which the mapping should expire. It
+        * is safe to use timestamps in all cases, regardless of exipration
+        * eg: strtotime("+3 hour")
         *
-        * @return  boolean
-        * @access  private
+        * @return Boolean
+        * @access private
         */
        function _set( $cmd, $key, $val, $exp ) {
                if ( !$this->_active ) {
@@ -899,7 +940,11 @@ class MWMemcached {
                        return false;
                }
 
-               @$this->stats[$cmd]++;
+               if ( isset( $this->stats[$cmd] ) ) {
+                       $this->stats[$cmd]++;
+               } else {
+                       $this->stats[$cmd] = 1;
+               }
 
                $flags = 0;
 
@@ -949,10 +994,10 @@ class MWMemcached {
        /**
         * Returns the socket for the host
         *
-        * @param   string   $host    Host:IP to get socket for
+        * @param $host String: Host:IP to get socket for
         *
-        * @return  mixed    IO Stream or false
-        * @access  private
+        * @return Mixed: IO Stream or false
+        * @access private
         */
        function sock_to_host( $host ) {
                if ( isset( $this->_cache_sock[$host] ) ) {
@@ -987,7 +1032,7 @@ class MWMemcached {
        /**
         * Write to a stream, timing out after the correct amount of time
         *
-        * @return bool false on failure, true on success
+        * @return Boolean: false on failure, true on success
         */
        /*
        function _safe_fwrite( $f, $buf, $len = false ) {