Changeset 90

Show
Ignore:
Timestamp:
08/10/03 22:48:54 (5 years ago)
Author:
bradfitz
Message:

PHP client 1.0.7.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/CONTRIBUTORS

    r83 r90  
    2424  -- packaging for Gentoo Linux 
    2525 
    26 seanc@FreeBSD.org 
     26Sean Chittenden <seanc@FreeBSD.org> 
    2727  -- packaging for FreeBSD 
    2828 
     
    3131     http://bugs.gentoo.org/show_bug.cgi?id=25385 
    3232 
     33Brion Vibber <brion@pobox.com> 
     34  -- debugging abstraction in PHP client 
  • trunk/api/php/ChangeLog

    r74 r90  
    1  
     1Release 1.0.7 
     2------------- 
     3* added 3 functions which handle error reporting 
     4  error() - returns error number of last error generated, else returns 0 
     5  error_string() - returns a string description of error number retuned 
     6  error_clear() - clears the last error number and error string 
     7* removed call to preg_match() in _loaditems() 
     8* only non-scalar values are serialize() before being 
     9  sent to the server 
     10* added the optional timestamp argument for delete() 
     11  read Documentation file for details 
     12* PHPDocs/PEAR style comments added 
     13* abstract debugging (Brion Vibber <brion@pobox.com>) 
     14         
    215Release 1.0.6 
    316------------- 
    417* removed all array_push() calls 
    5 * applyed patch provided by Stuart Herbert<stuart@gentoo.org> 
     18* applied patch provided by Stuart Herbert<stuart@gentoo.org> 
    619  corrects possible endless loop. Available at 
    720  http://bugs.gentoo.org/show_bug.cgi?id=25385 
     
    1730* initial release, version numbers kept 
    1831  in sync with MemCached version 
    19 * capable of storing any datatype in MemCache 
     32* capable of storing any datatype in MemCached 
  • trunk/api/php/Documentation

    r74 r90  
     1Ryan Gilfether <hotrodder@rocketmail.com> 
     2http://www.gilfether.com 
    13This module is Copyright (c) 2003 Ryan Gilfether. 
    24All rights reserved. 
     
    57This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND. 
    68 
    7 See the memcached website: 
    8    http://www.danga.com/memcached/ 
    9  
    10 Ryan Gilfether <hotrodder@rocketmail.com> 
    11 http://www.gilfether.com 
     9See the memcached website: http://www.danga.com/memcached/ 
    1210 
    1311 
     
    3432// objects on the same memcache server, so you could use the user's 
    3533// unique id as the hash value. 
     34// Possible errors set are: 
     35//              MC_ERR_GET 
    3636MemCachedClient::get($key); 
    3737 
    3838// just like get(), but takes an array of keys, returns FALSE on error 
     39// Possible errors set are: 
     40//              MC_ERR_NOT_ACTIVE 
    3941MemCachedClient::get_multi($keys) 
    4042 
     
    4446// hash value, as described above. 
    4547// returns TRUE on success else FALSE 
     48// Possible errors set are: 
     49//              MC_ERR_NOT_ACTIVE 
     50//              MC_ERR_GET_SOCK 
     51//              MC_ERR_SOCKET_WRITE 
     52//              MC_ERR_SOCKET_READ 
     53//              MC_ERR_SET 
    4654MemCachedClient::set($key, $value, $exptime); 
    4755 
    4856// Like set(), but only stores in memcache if the key doesn't already exist. 
    4957// returns TRUE on success else FALSE 
     58// Possible errors set are: 
     59//              MC_ERR_NOT_ACTIVE 
     60//              MC_ERR_GET_SOCK 
     61//              MC_ERR_SOCKET_WRITE 
     62//              MC_ERR_SOCKET_READ 
     63//              MC_ERR_SET 
    5064MemCachedClient::add($key, $value, $exptime); 
    5165 
    5266// Like set(), but only stores in memcache if the key already exists. 
    5367// returns TRUE on success else FALSE 
     68// Possible errors set are: 
     69//              MC_ERR_NOT_ACTIVE 
     70//              MC_ERR_GET_SOCK 
     71//              MC_ERR_SOCKET_WRITE 
     72//              MC_ERR_SOCKET_READ 
     73//              MC_ERR_SET 
    5474MemCachedClient::replace($key, $value, $exptime); 
    5575 
    5676// removes the key from the MemCache 
    57 // returns TRUE on success else FALSE 
    58 MemCachedClient::delete($key); 
     77// $time is the amount of time in seconds (or Unix time) until which 
     78// the client wishes the server to refuse "add" and "replace" commands 
     79// with this key. For this amount of item, the item is put into a 
     80// delete queue, which means that it won't possible to retrieve it by 
     81// the "get" command, but "add" and "replace" command with this key 
     82// will also fail (the "set" command will succeed, however). After the 
     83// time passes, the item is finally deleted from server memory. 
     84// The parameter $time is optional, and, if absent, defaults to 0 
     85// (which means that the item will be deleted immediately and further 
     86// storage commands with this key will succeed). 
     87// returns TRUE on success else returns FALSE 
     88// Possible errors set are: 
     89//              MC_ERR_NOT_ACTIVE 
     90//              MC_ERR_GET_SOCK 
     91//              MC_ERR_SOCKET_WRITE 
     92//              MC_ERR_SOCKET_READ 
     93//              MC_ERR_DELETE 
     94MemCachedClient::delete($key, $time = 0); 
    5995 
    6096// Sends a command to the server to atomically increment the value for 
     
    6399// incrementing.  Value should be zero or greater.  Overflow on server 
    64100// is not checked.  Be aware of values approaching 2**32.  See decr. 
     101// Possible errors set are: 
     102//              MC_ERR_NOT_ACTIVE 
     103//              MC_ERR_GET_SOCK 
     104//              MC_ERR_SOCKET_WRITE 
     105//              MC_ERR_SOCKET_READ 
    65106// returns new value on success, else returns FALSE 
    66107// ONLY WORKS WITH NUMERIC VALUES 
     
    70111// values are capped at 0.  If server value is 1, a decrement of 2 
    71112// returns 0, not -1. 
     113// Possible errors set are: 
     114//              MC_ERR_NOT_ACTIVE 
     115//              MC_ERR_GET_SOCK 
     116//              MC_ERR_SOCKET_WRITE 
     117//              MC_ERR_SOCKET_READ 
    72118// returns new value on success, else returns FALSE 
    73119// ONLY WORKS WITH NUMERIC VALUES 
     
    83129// remove all cached hosts that are no longer good 
    84130MemCachedClient::forget_dead_hosts(); 
     131 
     132// When a function returns FALSE, an error code is set. 
     133// This funtion will return the error code. 
     134// See error_string() 
     135// returns last error code set 
     136MemCachedClient::error() 
     137 
     138// Returns a string describing the error set in error() 
     139// See error() 
     140// returns a string describing the error code given 
     141MemCachedClient::error_string() 
     142 
     143// Resets the error number and error string 
     144MemCachedClient::error_clear() 
     145 
     146Error codes are as follows: 
     147MC_ERR_NOT_ACTIVE               // no active servers 
     148MC_ERR_SOCKET_WRITE             // socket_write() failed 
     149MC_ERR_SOCKET_READ              // socket_read() failed 
     150MC_ERR_SOCKET_CONNECT   // failed to connect to host 
     151MC_ERR_DELETE                   // delete() did not recieve DELETED command 
     152MC_ERR_HOST_FORMAT              // sock_to_host() invalid host format 
     153MC_ERR_HOST_DEAD                // sock_to_host() host is dead 
     154MC_ERR_GET_SOCK                 // get_sock() failed to find a valid socket 
     155MC_ERR_SET                              // _set() failed to receive the STORED response 
     156MC_ERR_LOADITEM_HEADER  // _load_items failed to receive valid data header 
     157MC_ERR_LOADITEM_END             // _load_items failed to receive END response 
     158MC_ERR_LOADITEM_BYTES   // _load_items bytes read larger than bytes available 
     159MC_ERR_GET                              // failed to get value associated with key 
     160 
    85161 
    86162 
     
    171247// retrieved from memcache 
    172248$fp = fopen("./test.jpg","wb"); 
    173 print "WRITING<BR>"; 
    174249$data = $memc->get( "key" ) ; 
    175250print "Data length is " . strlen( $data ) . "\n" ; 
  • trunk/api/php/MemCachedClient.inc.php

    r74 r90  
    66 * http://www.gilfether.com 
    77 * 
    8  * Translated from Brad Fitzpatrick's <brad@danga.com> MemCached Perl client 
     8 * Originally translated from Brad Fitzpatrick's <brad@danga.com> MemCached Perl client 
     9 * See the memcached website: 
     10 * http://www.danga.com/memcached/ 
     11 * 
     12 * This module is Copyright (c) 2003 Ryan Gilfether. 
     13 * All rights reserved. 
     14 * You may distribute under the terms of the GNU General Public License 
     15 * This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND. 
     16 * 
    917 */ 
    1018 
    11 define("MC_VERSION", "1.0.6"); 
     19/** 
     20 * version string 
     21 */ 
     22define("MC_VERSION", "1.0.7"); 
     23/** 
     24 * int, buffer size used for sending and receiving 
     25 * data from sockets 
     26 */ 
    1227define("MC_BUFFER_SZ", 1024); 
    13  
     28/** 
     29 * MemCached error numbers 
     30 * used with the MemCachedError class 
     31 */ 
     32define("MC_ERR_NOT_ACTIVE", 1001);              // no active servers 
     33define("MC_ERR_SOCKET_WRITE", 1002);    // socket_write() failed 
     34define("MC_ERR_SOCKET_READ", 1003);             // socket_read() failed 
     35define("MC_ERR_SOCKET_CONNECT", 1004);  // failed to connect to host 
     36define("MC_ERR_DELETE", 1005);                  // delete() did not recieve DELETED command 
     37define("MC_ERR_HOST_FORMAT", 1006);             // sock_to_host() invalid host format 
     38define("MC_ERR_HOST_DEAD", 1007);               // sock_to_host() host is dead 
     39define("MC_ERR_GET_SOCK", 1008);                // get_sock() failed to find a valid socket 
     40define("MC_ERR_SET", 1009);                             // _set() failed to receive the STORED response 
     41define("MC_ERR_LOADITEM_HEADER", 1010); // _load_items failed to receive valid data header 
     42define("MC_ERR_LOADITEM_END", 1011);    // _load_items failed to receive END response 
     43define("MC_ERR_LOADITEM_BYTES", 1012);  // _load_items bytes read larger than bytes available 
     44define("MC_ERR_GET", 1013);                             // failed to get value associated with key 
     45 
     46 
     47/** 
     48 * MemCached PHP client Class. 
     49 * 
     50 * Communicates with the MemCached server, and executes the MemCached protocol 
     51 * MemCached available at http://www.danga.com/memcached 
     52 * 
     53 * @author Ryan Gilfether <ryan@gilfether.com> 
     54 * @package MemCachedClient 
     55 * @access public 
     56 * @version 1.0.7 
     57 */ 
    1458class MemCachedClient 
    1559{ 
     60    /** 
     61     * array of servers no long available 
     62     * @var array 
     63     */ 
    1664        var $host_dead; 
     65        /** 
     66         * array of open sockets 
     67         * @var array 
     68         */ 
    1769        var $cache_sock; 
     70    /** 
     71     * determine if debugging is either on or off 
     72     * @var bool 
     73     */ 
    1874        var $debug; 
     75        /** 
     76     * array of servers to attempt to use, "host:port" string format 
     77     * @var array 
     78     */ 
    1979        var $servers; 
     80        /** 
     81         * count of currently active connections to servers 
     82         * @var int 
     83         */ 
    2084        var $active; 
    21         var $buckets; 
    22         var $bucketcount; 
    23  
    24  
     85        /** 
     86         * error code if one is set 
     87         * @var int 
     88         */ 
     89        var $errno; 
     90        /** 
     91         * string describing error 
     92         * @var string 
     93         */ 
     94        var $errstr; 
     95 
     96 
     97    /** 
     98     * Constructor 
     99     * 
     100     * Creates a new MemCachedClient object 
     101         * Takes one parameter, a array of options.  The most important key is 
     102         * $options["servers"], but that can also be set later with the set_servers() 
     103         * method.  The servers must be an array of hosts, each of which is 
     104         * either a scalar of the form <10.0.0.10:11211> or an array of the 
     105         * former and an integer weight value.  (the default weight if 
     106         * unspecified is 1.)  It's recommended that weight values be kept as low 
     107         * as possible, as this module currently allocates memory for bucket 
     108         * distribution proportional to the total host weights. 
     109         * $options["debug"] turns the debugging on if set to true 
     110     * 
     111     * @access public 
     112     * @param array $option an array of servers and debug status 
     113     * @return object MemCachedClient the new MemCachedClient object 
     114     */ 
    25115        function MemCachedClient($options = 0) 
    26116        { 
     
    32122                } 
    33123 
    34                 return $this; 
    35         } 
    36  
    37         // $servers must be in the format described in the constructor 
     124                $this->errno = 0; 
     125                $this->errstr = ""; 
     126        } 
     127 
     128 
     129        /** 
     130         * sets up the list of servers and the ports to connect to 
     131         * takes an array of servers in the same format as in the constructor 
     132         * 
     133         * @access public 
     134         * @param array $servers array of servers in the format described in the constructor 
     135         */ 
    38136        function set_servers($servers) 
    39137        { 
    40138                $this->servers = $servers; 
    41139                $this->active = count($this->servers); 
    42                 $this->buckets = ""; 
    43                 $this->bucketcount = 0; 
    44  
    45                 return $this; 
    46         } 
    47  
    48  
     140        } 
     141 
     142 
     143        /** 
     144         * if $do_debug is set to true, will print out 
     145         * debugging info, else debug is turned off 
     146         * 
     147         * @access public 
     148         * @param bool $do_debug set to true to turn debugging on, false to turn off 
     149         */ 
    49150        function set_debug($do_debug) 
    50151        { 
     
    53154 
    54155 
     156        /** 
     157         * remove all cached hosts that are no longer good 
     158         * 
     159         * @access public 
     160         */ 
    55161        function forget_dead_hosts() 
    56162        { 
     
    59165 
    60166 
    61         // disconnect all open connections 
     167        /** 
     168         * disconnects from all servers 
     169         * 
     170         * @access public 
     171         */ 
    62172        function disconnect_all() 
    63173        { 
     
    66176 
    67177                unset($this->cache_sock); 
    68         } 
    69  
    70  
    71         // delete the key, return true on success, false on error 
    72         function delete($key) 
     178                $this->active = 0; 
     179        } 
     180 
     181 
     182        /** 
     183         * removes the key from the MemCache 
     184         * $time is the amount of time in seconds (or Unix time) until which 
     185         * the client wishes the server to refuse "add" and "replace" commands 
     186         * with this key. For this amount of item, the item is put into a 
     187         * delete queue, which means that it won't possible to retrieve it by 
     188         * the "get" command, but "add" and "replace" command with this key 
     189         * will also fail (the "set" command will succeed, however). After the 
     190         * time passes, the item is finally deleted from server memory. 
     191         * The parameter $time is optional, and, if absent, defaults to 0 
     192         * (which means that the item will be deleted immediately and further 
     193         * storage commands with this key will succeed). 
     194         * Possible errors set are: 
     195         *              MC_ERR_NOT_ACTIVE 
     196         *              MC_ERR_GET_SOCK 
     197         *              MC_ERR_SOCKET_WRITE 
     198         *              MC_ERR_SOCKET_READ 
     199         *              MC_ERR_DELETE 
     200         * 
     201         * @access public 
     202         * @param string $key the key to delete 
     203         * @param timestamp $time optional, the amount of time server will refuse commands on key 
     204         * @return bool TRUE on success, FALSE if key does not exist 
     205         */ 
     206        function delete($key, $time = 0) 
    73207        { 
    74208                if(!$this->active) 
    75209                { 
    76                         if($this->debug) 
    77                                 print "delete(): There are no active servers available\r\n"; 
     210                        $this->errno = MC_ERR_NOT_ACTIVE; 
     211                        $this->errstr = "No active servers are available"; 
     212 
     213                        if($this->debug) 
     214                                $this->_debug("delete(): There are no active servers available."); 
    78215 
    79216                        return FALSE; 
     
    83220 
    84221                if(!is_resource($sock)) 
    85                         return FALSE; 
     222                { 
     223                        $this->errno = MC_ERR_GET_SOCK; 
     224                        $this->errstr = "Unable to retrieve a valid socket."; 
     225 
     226                        if($this->debug) 
     227                                $this->_debug("delete(): get_sock() returned an invalid socket."); 
     228 
     229                        return FALSE; 
     230                } 
    86231 
    87232                if(is_array($key)) 
    88233                        $key = $key[1]; 
    89234 
    90                 // send the command to the server 
    91                 $cmd = "delete $key\r\n"; 
     235                $cmd = "delete $key $time\r\n"; 
    92236                $cmd_len = strlen($cmd); 
    93237                $offset = 0; 
     
    102246                        else if($offset < $cmd_len) 
    103247                        { 
    104                 if($this->debug) 
     248                                $this->errno = MC_ERR_SOCKET_WRITE; 
     249                                $this->errstr = "Failed to write to socket."; 
     250 
     251                                if($this->debug) 
    105252                                { 
    106                                         $errno = socket_last_error($sock); 
    107                                         print "_delete(): socket_write() returned FALSE. Error $errno: ".socket_strerror($errno)."\r\n"
     253                                        $sockerr = socket_last_error($sock); 
     254                                        $this->_debug("delete(): socket_write() returned FALSE. Socket Error $sockerr: ".socket_strerror($sockerr))
    108255                                } 
    109256 
     
    115262                if(($retval = socket_read($sock, MC_BUFFER_SZ, PHP_NORMAL_READ)) === FALSE) 
    116263                { 
    117                         if($this->debug) 
    118                         { 
    119                                 $errno = socket_last_error($sock); 
    120                                 print "_delete(): socket_read() returned FALSE. Error $errno: ".socket_strerror($errno)."\r\n"; 
     264                        $this->errno = MC_ERR_SOCKET_READ; 
     265                        $this->errstr = "Failed to read from socket."; 
     266 
     267                        if($this->debug) 
     268                        { 
     269                                $sockerr = socket_last_error($sock); 
     270                                $this->_debug("delete(): socket_read() returned FALSE. Socket Error $sockerr: ".socket_strerror($sockerr)); 
    121271                        } 
    122272 
     
    130280                if($retval == "DELETED") 
    131281                        return TRUE; 
    132  
    133         if($this->debug) 
    134                         print "_delete(): Failed to receive DELETED response from server. Received $retval instead.\r\n"; 
    135  
    136                 return FALSE; 
    137         } 
    138  
    139  
    140         // Like set(), but only stores in memcache if the key doesn't already exist. 
     282                else 
     283                { 
     284                        // something went wrong, create the error 
     285                        $this->errno = MC_ERR_DELETE; 
     286                        $this->errstr = "Failed to receive DELETED response from server."; 
     287 
     288                        if($this->debug) 
     289                                $this->_debug("delete(): Failed to receive DELETED response from server. Received $retval instead."); 
     290 
     291                        return FALSE; 
     292                } 
     293        } 
     294 
     295 
     296        /** 
     297         * Like set(), but only stores in memcache if the key doesn't already exist. 
     298         * Possible errors set are: 
     299         *              MC_ERR_NOT_ACTIVE 
     300         *              MC_ERR_GET_SOCK 
     301         *              MC_ERR_SOCKET_WRITE 
     302         *              MC_ERR_SOCKET_READ 
     303         *              MC_ERR_SET 
     304         * 
     305         * @access public 
     306         * @param string $key the key to set 
     307         * @param mixed $val the value of the key 
     308         * @param timestamp $exptime optional, the to to live of the key 
     309         * @return bool TRUE on success, else FALSE 
     310         */ 
    141311        function add($key, $val, $exptime = 0) 
    142312        { 
     
    145315 
    146316 
    147         // Like set(), but only stores in memcache if the key already exists. 
     317        /** 
     318         * Like set(), but only stores in memcache if the key already exists. 
     319         * returns TRUE on success else FALSE 
     320         * Possible errors set are: 
     321         *              MC_ERR_NOT_ACTIVE 
     322         *              MC_ERR_GET_SOCK 
     323         *              MC_ERR_SOCKET_WRITE 
     324         *              MC_ERR_SOCKET_READ 
     325         *              MC_ERR_SET 
     326         * 
     327         * @access public 
     328         * @param string $key the key to set 
     329         * @param mixed $val the value of the key 
     330         * @param timestamp $exptime optional, the to to live of the key 
     331         * @return bool TRUE on success, else FALSE 
     332         */ 
    148333        function replace($key, $val, $exptime = 0) 
    149334        { 
     
    152337 
    153338 
    154         // Unconditionally sets a key to a given value in the memcache. 
     339        /** 
     340         * Unconditionally sets a key to a given value in the memcache.  Returns true 
     341         * if it was stored successfully. 
     342         * The $key can optionally be an arrayref, with the first element being the 
     343         * hash value, as described above. 
     344         * Possible errors set are: 
     345         *              MC_ERR_NOT_ACTIVE 
     346         *              MC_ERR_GET_SOCK 
     347         *              MC_ERR_SOCKET_WRITE 
     348         *              MC_ERR_SOCKET_READ 
     349         *              MC_ERR_SET 
     350         * 
     351         * @access public 
     352         * @param string $key the key to set 
     353         * @param mixed $val the value of the key 
     354         * @param timestamp $exptime optional, the to to live of the key 
     355         * @return bool TRUE on success, else FALSE 
     356         */ 
    155357        function set($key, $val, $exptime = 0) 
    156358        { 
     
    159361 
    160362 
    161         // Retrieves a key from the memcache and returns its value, 
    162         // else returns false. 
     363        /** 
     364         * Retrieves a key from the memcache.  Returns the value (automatically 
     365         * unserialized, if necessary) or FALSE if it fails. 
     366         * The $key can optionally be an array, with the first element being the 
     367         * hash value, if you want to avoid making this module calculate a hash 
     368         * value.  You may prefer, for example, to keep all of a given user's 
     369         * objects on the same memcache server, so you could use the user's 
     370         * unique id as the hash value. 
     371         * Possible errors set are: 
     372         *              MC_ERR_GET 
     373         * 
     374         * @access public 
     375         * @param string $key the key to retrieve 
     376         * @return mixed the value of the key, FALSE on error 
     377         */ 
    163378        function get($key) 
    164379        { 
     
    166381 
    167382                if(!$val) 
    168                         return FALSE; 
     383                { 
     384                        $this->errno = MC_ERR_GET; 
     385                        $this->errstr = "No value found for key $key"; 
     386 
     387                        if($this->debug) 
     388                                $this->_debug("get(): No value found for key $key"); 
     389 
     390                        return FALSE; 
     391                } 
    169392 
    170393                return $val[$key]; 
     
    172395 
    173396 
    174         // like get() but takes an array of keys 
     397        /** 
     398         * just like get(), but takes an array of keys, returns FALSE on error 
     399         * Possible errors set are: 
     400         *              MC_ERR_NOT_ACTIVE 
     401         * 
     402         * @access public 
     403         * @param array $keys the keys to retrieve 
     404         * @return array the value of each key, FALSE on error 
     405         */ 
    175406        function get_multi($keys) 
    176407        { 
     
    180411 
    181412                if(!$this->active) 
    182                         return FALSE; 
     413                { 
     414                        $this->errno = MC_ERR_NOT_ACTIVE; 
     415                        $this->errstr = "No active servers are available"; 
     416 
     417                        if($this->debug) 
     418                                $this->_debug("get_multi(): There are no active servers available."); 
     419 
     420                        return FALSE; 
     421                } 
    183422 
    184423                if(!is_array($keys)) 
     
    221460                { 
    222461                        while(list($k, $v) = each($val)) 
    223                                 print "MemCache: got $k = $v\n"
     462                                $this->_debug("MemCache: got $k = $v")
    224463                } 
    225464 
     
    228467 
    229468 
    230         // increments a numerical value by the value given in $value 
    231         // otherwise assumes 1 
    232         // ONLY WORKS WITH NUMERIC VALUES 
    233         function incr($key, $value = "") 
     469        /** 
     470         * Sends a command to the server to atomically increment the value for 
     471         * $key by $value, or by 1 if $value is undefined.  Returns FALSE if $key 
     472         * doesn't exist on server, otherwise it returns the new value after 
     473         * incrementing.  Value should be zero or greater.  Overflow on server 
     474         * is not checked.  Be aware of values approaching 2**32.  See decr. 
     475         * ONLY WORKS WITH NUMERIC VALUES 
     476         * Possible errors set are: 
     477         *              MC_ERR_NOT_ACTIVE 
     478         *              MC_ERR_GET_SOCK 
     479         *              MC_ERR_SOCKET_WRITE 
     480         *              MC_ERR_SOCKET_READ 
     481         * 
     482         * @access public 
     483         * @param string $key the keys to increment 
     484         * @param int $value the amount to increment the key bye 
     485         * @return int the new value of the key, else FALSE 
     486         */ 
     487        function incr($key, $value = 1) 
    234488        { 
    235489        return $this->_incrdecr("incr", $key, $value); 
     
    237491 
    238492 
    239         // decrements a numerical value by the value given in $value 
    240         // otherwise assumes 1 
    241         // ONLY WORKS WITH NUMERIC VALUES 
    242         function decr($key, $value = "") 
     493        /** 
     494         * Like incr, but decrements.  Unlike incr, underflow is checked and new 
     495         * values are capped at 0.  If server value is 1, a decrement of 2 
     496         * returns 0, not -1. 
     497         * ONLY WORKS WITH NUMERIC VALUES 
     498         * Possible errors set are: 
     499         *              MC_ERR_NOT_ACTIVE 
     500         *              MC_ERR_GET_SOCK 
     501         *              MC_ERR_SOCKET_WRITE 
     502         *              MC_ERR_SOCKET_READ 
     503         * 
     504         * @access public 
     505         * @param string $key the keys to increment 
     506         * @param int $value the amount to increment the key bye 
     507         * @return int the new value of the key, else FALSE 
     508         */ 
     509        function decr($key, $value = 1) 
    243510        { 
    244511        return $this->_incrdecr("decr", $key, $value); 
     
    246513 
    247514 
    248  
    249         /************************************************** 
     515        /** 
     516         * When a function returns FALSE, an error code is set. 
     517         * This funtion will return the error code. 
     518         * See error_string() 
     519         * 
     520         * @access public 
     521         * @return int the value of the last error code 
     522         */ 
     523        function error() 
     524        { 
     525                return $this->errno; 
     526        } 
     527 
     528 
     529        /** 
     530         * Returns a string describing the error set in error() 
     531         * See error() 
     532         * 
     533         * @access public 
     534         * @return int a string describing the error code given 
     535         */ 
     536        function error_string() 
     537    { 
     538                return $this->errstr; 
     539        } 
     540 
     541 
     542        /** 
     543         * Resets the error number and error string 
     544         * 
     545         * @access public 
     546         */ 
     547        function error_clear() 
     548        { 
     549                // reset to no error 
     550                $this->errno = 0; 
     551                $this->errstr = ""; 
     552        } 
     553 
     554 
     555        /* 
    250556         * PRIVATE FUNCTIONS 
    251          **************************************************/ 
    252  
    253         // connects to the server 
     557         */ 
     558 
     559 
     560        /** 
     561         * connects to a server 
     562         * The $host may either a string int the form of host:port or an array of the 
     563         * former and an integer weight value.  (the default weight if 
     564         * unspecified is 1.) See the constructor for details 
     565         * Possible errors set are: 
     566         *              MC_ERR_HOST_FORMAT 
     567         *              MC_ERR_HOST_DEAD 
     568         *              MC_ERR_SOCKET_CONNECT 
     569         * 
     570         * @access private 
     571         * @param mixed $host either an array or a string 
     572         * @return resource the socket of the new connection, else FALSE 
     573         */ 
    254574        function sock_to_host($host) 
    255575        { 
     
    263583                if(count($conn) != 2) 
    264584                { 
    265                         if($this->debug) 
    266                                 print "sock_to_host(): Host address was not in the format of host:port\r\n"; 
     585                        $this->errno = MC_ERR_HOST_FORMAT; 
     586                        $this->errstr = "Host address was not in the format of host:port"; 
     587 
     588                        if($this->debug) 
     589                                $this->_debug("sock_to_host(): Host address was not in the format of host:port"); 
    267590 
    268591                        return FALSE; 
     
    272595                ($this->host_dead[$conn[0]] && $this->host_dead[$conn[0]] > $now)) 
    273596                { 
    274                         if($this->debug) 
    275                                 print "sock_to_host(): The host $host is not available.\r\n"; 
     597                        $this->errno = MC_ERR_HOST_DEAD; 
     598                        $this->errstr = "Host $host is not available."; 
     599 
     600                        if($this->debug) 
     601                                $this->_debug("sock_to_host(): Host $host is not available."); 
    276602 
    277603                        return FALSE; 
     
    286612                        $this->host_dead[$host]=$this->host_dead[$conn[0]]=$now+60+intval(rand(0, 10)); 
    287613 
    288                         if($this->debug) 
    289                                 print "sock_to_host(): Failed to connect to ".$conn[0].":".$conn[1]."\r\n"; 
     614                        $this->errno = MC_ERR_SOCKET_CONNECT; 
     615                        $this->errstr = "Failed to connect to ".$conn[0].":".$conn[1]; 
     616 
     617                        if($this->debug) 
     618                                $this->_debug("sock_to_host(): Failed to connect to ".$conn[0].":".$conn[1]); 
    290619 
    291620                        return FALSE; 
     
    299628 
    300629 
    301         // returns the socket from the key, else FALSE 
     630        /** 
     631         * retrieves the socket associated with a key 
     632         * Possible errors set are: 
     633         *              MC_ERR_NOT_ACTIVE 
     634         *              MC_ERR_GET_SOCK 
     635         * 
     636         * @access private 
     637         * @param string $key the key to retrieve the socket from 
     638         * @return resource the socket of the connection, else FALSE 
     639         */ 
    302640        function get_sock($key) 
    303641        { 
     642                $buckets = 0; 
     643 
    304644                if(!$this->active) 
    305645                { 
    306                         if($this->debug) 
    307                                 print "get_sock(): There are no active servers available\r\n"; 
     646                        $this->errno = MC_ERR_NOT_ACTIVE; 
     647                        $this->errstr = "No active servers are available"; 
     648 
     649                        if($this->debug) 
     650                                $this->_debug("get_sock(): There are no active servers available"); 
    308651 
    309652                        return FALSE; 
     
    312655                $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key); 
    313656 
    314                 if(!$this->buckets) 
    315                 { 
    316                         $bu = array(); 
     657                if(!$buckets) 
     658                { 
     659                        $bu = $buckets = array(); 
    317660 
    318661                        foreach($this->servers as $v) 
     
    327670                        } 
    328671 
    329                         $this->buckets = $bu; 
    330                        $this->bucketcount = count($this->buckets); 
    331                 } 
    332  
     672                        $buckets = $bu; 
     673                } 
     674 
     675                $real_key = is_array($key) ? $key[1] : $key; 
    333676                $tries = 0; 
    334677                while($tries < 20) 
    335678                { 
    336                         $host = $this->buckets[$hv % $this->bucketcount]; 
     679                        $host = $buckets[$hv % count($buckets)]; 
    337680                        $sock = $this->sock_to_host($host); 
    338681 
     
    340683                                return $sock; 
    341684 
    342                         $hv += $this->_hashfunc($tries); 
     685                        $hv += $this->_hashfunc($tries.$real_key); 
    343686                        ++$tries; 
    344687                } 
    345688 
     689                $this->errno = MC_ERR_GET_SOCK; 
     690                $this->errstr = "Unable to retrieve a valid socket."; 
     691 
    346692                if($this->debug) 
    347                         print "get_sock(): get_sock(): Unable to retrieve a valid socket\r\n"
     693                        $this->_debug("get_sock(): Unable to retrieve a valid socket")
    348694 
    349695                return FALSE; 
     
    351697 
    352698 
    353         // private function. increments or decrements a 
    354         // numerical value in memcached. this function is 
    355         // called from incr() and decr() 
    356         // ONLY WORKS WITH NUMERIC VALUES 
     699        /** 
     700         * increments or decrements a numerical value in memcached. this function is 
     701         * called from incr() and decr() 
     702         * ONLY WORKS WITH NUMERIC VALUES 
     703         * Possible errors set are: 
     704         *              MC_ERR_NOT_ACTIVE 
     705         *              MC_ERR_GET_SOCK 
     706         *              MC_ERR_SOCKET_WRITE 
     707         *              MC_ERR_SOCKET_READ 
     708         * 
     709         * @access private 
     710         * @param string $cmdname the command to send, either incr or decr 
     711         * @param string $key the key to perform the command on 
     712         * @param mixed $value the value to incr or decr the key value by 
     713         * @return int the new value of the key, FALSE if something went wrong 
     714         */ 
    357715        function _incrdecr($cmdname, $key, $value) 
    358716        { 
    359717                if(!$this->active) 
    360718                { 
    361                         if($this->debug) 
    362                                 print "_incrdecr(): There are no active servers available\r\n"; 
     719                        $this->errno = MC_ERR_NOT_ACTIVE; 
     720                        $this->errstr = "No active servers are available"; 
     721 
     722                        if($this->debug) 
     723                                $this->_debug("_incrdecr(): There are no active servers available"); 
    363724 
    364725                        return FALSE; 
     
    368729                if(!is_resource($sock)) 
    369730                { 
    370                         if($this->debug) 
    371                                 print "_incrdecr(): Invalid socket returned by get_sock()\r\n"; 
    372  
    373                         return FALSE; 
    374                 } 
    375  
    376                 // something about stats 
     731                        $this->errno = MC_ERR_GET_SOCK; 
     732                        $this->errstr = "Unable to retrieve a valid socket."; 
     733 
     734                        if($this->debug) 
     735                                $this->_debug("_incrdecr(): Invalid socket returned by get_sock()"); 
     736 
     737                        return FALSE; 
     738                } 
    377739 
    378740                if($value == "") 
     
    383745                $offset = 0; 
    384746 
    385                 // now send the command 
     747                // write the command to the server 
    386748                while($offset < $cmd_len) 
    387749                { 
     
    392754                        else if($offset < $cmd_len) 
    393755                        {