Changeset 90
- Timestamp:
- 08/10/03 22:48:54 (5 years ago)
- Files:
-
- trunk/CONTRIBUTORS (modified) (2 diffs)
- trunk/api/php/ChangeLog (modified) (2 diffs)
- trunk/api/php/Documentation (modified) (8 diffs)
- trunk/api/php/MemCachedClient.inc.php (modified) (50 diffs)
- trunk/website/apis.bml (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/CONTRIBUTORS
r83 r90 24 24 -- packaging for Gentoo Linux 25 25 26 seanc@FreeBSD.org 26 Sean Chittenden <seanc@FreeBSD.org> 27 27 -- packaging for FreeBSD 28 28 … … 31 31 http://bugs.gentoo.org/show_bug.cgi?id=25385 32 32 33 Brion Vibber <brion@pobox.com> 34 -- debugging abstraction in PHP client trunk/api/php/ChangeLog
r74 r90 1 1 Release 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 2 15 Release 1.0.6 3 16 ------------- 4 17 * removed all array_push() calls 5 * appl yed patch provided by Stuart Herbert<stuart@gentoo.org>18 * applied patch provided by Stuart Herbert<stuart@gentoo.org> 6 19 corrects possible endless loop. Available at 7 20 http://bugs.gentoo.org/show_bug.cgi?id=25385 … … 17 30 * initial release, version numbers kept 18 31 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 1 Ryan Gilfether <hotrodder@rocketmail.com> 2 http://www.gilfether.com 1 3 This module is Copyright (c) 2003 Ryan Gilfether. 2 4 All rights reserved. … … 5 7 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND. 6 8 7 See the memcached website: 8 http://www.danga.com/memcached/ 9 10 Ryan Gilfether <hotrodder@rocketmail.com> 11 http://www.gilfether.com 9 See the memcached website: http://www.danga.com/memcached/ 12 10 13 11 … … 34 32 // objects on the same memcache server, so you could use the user's 35 33 // unique id as the hash value. 34 // Possible errors set are: 35 // MC_ERR_GET 36 36 MemCachedClient::get($key); 37 37 38 38 // just like get(), but takes an array of keys, returns FALSE on error 39 // Possible errors set are: 40 // MC_ERR_NOT_ACTIVE 39 41 MemCachedClient::get_multi($keys) 40 42 … … 44 46 // hash value, as described above. 45 47 // 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 46 54 MemCachedClient::set($key, $value, $exptime); 47 55 48 56 // Like set(), but only stores in memcache if the key doesn't already exist. 49 57 // 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 50 64 MemCachedClient::add($key, $value, $exptime); 51 65 52 66 // Like set(), but only stores in memcache if the key already exists. 53 67 // 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 54 74 MemCachedClient::replace($key, $value, $exptime); 55 75 56 76 // 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 94 MemCachedClient::delete($key, $time = 0); 59 95 60 96 // Sends a command to the server to atomically increment the value for … … 63 99 // incrementing. Value should be zero or greater. Overflow on server 64 100 // 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 65 106 // returns new value on success, else returns FALSE 66 107 // ONLY WORKS WITH NUMERIC VALUES … … 70 111 // values are capped at 0. If server value is 1, a decrement of 2 71 112 // 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 72 118 // returns new value on success, else returns FALSE 73 119 // ONLY WORKS WITH NUMERIC VALUES … … 83 129 // remove all cached hosts that are no longer good 84 130 MemCachedClient::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 136 MemCachedClient::error() 137 138 // Returns a string describing the error set in error() 139 // See error() 140 // returns a string describing the error code given 141 MemCachedClient::error_string() 142 143 // Resets the error number and error string 144 MemCachedClient::error_clear() 145 146 Error codes are as follows: 147 MC_ERR_NOT_ACTIVE // no active servers 148 MC_ERR_SOCKET_WRITE // socket_write() failed 149 MC_ERR_SOCKET_READ // socket_read() failed 150 MC_ERR_SOCKET_CONNECT // failed to connect to host 151 MC_ERR_DELETE // delete() did not recieve DELETED command 152 MC_ERR_HOST_FORMAT // sock_to_host() invalid host format 153 MC_ERR_HOST_DEAD // sock_to_host() host is dead 154 MC_ERR_GET_SOCK // get_sock() failed to find a valid socket 155 MC_ERR_SET // _set() failed to receive the STORED response 156 MC_ERR_LOADITEM_HEADER // _load_items failed to receive valid data header 157 MC_ERR_LOADITEM_END // _load_items failed to receive END response 158 MC_ERR_LOADITEM_BYTES // _load_items bytes read larger than bytes available 159 MC_ERR_GET // failed to get value associated with key 160 85 161 86 162 … … 171 247 // retrieved from memcache 172 248 $fp = fopen("./test.jpg","wb"); 173 print "WRITING<BR>";174 249 $data = $memc->get( "key" ) ; 175 250 print "Data length is " . strlen( $data ) . "\n" ; trunk/api/php/MemCachedClient.inc.php
r74 r90 6 6 * http://www.gilfether.com 7 7 * 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 * 9 17 */ 10 18 11 define("MC_VERSION", "1.0.6"); 19 /** 20 * version string 21 */ 22 define("MC_VERSION", "1.0.7"); 23 /** 24 * int, buffer size used for sending and receiving 25 * data from sockets 26 */ 12 27 define("MC_BUFFER_SZ", 1024); 13 28 /** 29 * MemCached error numbers 30 * used with the MemCachedError class 31 */ 32 define("MC_ERR_NOT_ACTIVE", 1001); // no active servers 33 define("MC_ERR_SOCKET_WRITE", 1002); // socket_write() failed 34 define("MC_ERR_SOCKET_READ", 1003); // socket_read() failed 35 define("MC_ERR_SOCKET_CONNECT", 1004); // failed to connect to host 36 define("MC_ERR_DELETE", 1005); // delete() did not recieve DELETED command 37 define("MC_ERR_HOST_FORMAT", 1006); // sock_to_host() invalid host format 38 define("MC_ERR_HOST_DEAD", 1007); // sock_to_host() host is dead 39 define("MC_ERR_GET_SOCK", 1008); // get_sock() failed to find a valid socket 40 define("MC_ERR_SET", 1009); // _set() failed to receive the STORED response 41 define("MC_ERR_LOADITEM_HEADER", 1010); // _load_items failed to receive valid data header 42 define("MC_ERR_LOADITEM_END", 1011); // _load_items failed to receive END response 43 define("MC_ERR_LOADITEM_BYTES", 1012); // _load_items bytes read larger than bytes available 44 define("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 */ 14 58 class MemCachedClient 15 59 { 60 /** 61 * array of servers no long available 62 * @var array 63 */ 16 64 var $host_dead; 65 /** 66 * array of open sockets 67 * @var array 68 */ 17 69 var $cache_sock; 70 /** 71 * determine if debugging is either on or off 72 * @var bool 73 */ 18 74 var $debug; 75 /** 76 * array of servers to attempt to use, "host:port" string format 77 * @var array 78 */ 19 79 var $servers; 80 /** 81 * count of currently active connections to servers 82 * @var int 83 */ 20 84 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 */ 25 115 function MemCachedClient($options = 0) 26 116 { … … 32 122 } 33 123 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 */ 38 136 function set_servers($servers) 39 137 { 40 138 $this->servers = $servers; 41 139 $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 */ 49 150 function set_debug($do_debug) 50 151 { … … 53 154 54 155 156 /** 157 * remove all cached hosts that are no longer good 158 * 159 * @access public 160 */ 55 161 function forget_dead_hosts() 56 162 { … … 59 165 60 166 61 // disconnect all open connections 167 /** 168 * disconnects from all servers 169 * 170 * @access public 171 */ 62 172 function disconnect_all() 63 173 { … … 66 176 67 177 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) 73 207 { 74 208 if(!$this->active) 75 209 { 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."); 78 215 79 216 return FALSE; … … 83 220 84 221 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 } 86 231 87 232 if(is_array($key)) 88 233 $key = $key[1]; 89 234 90 // send the command to the server 91 $cmd = "delete $key\r\n"; 235 $cmd = "delete $key $time\r\n"; 92 236 $cmd_len = strlen($cmd); 93 237 $offset = 0; … … 102 246 else if($offset < $cmd_len) 103 247 { 104 if($this->debug) 248 $this->errno = MC_ERR_SOCKET_WRITE; 249 $this->errstr = "Failed to write to socket."; 250 251 if($this->debug) 105 252 { 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)); 108 255 } 109 256 … … 115 262 if(($retval = socket_read($sock, MC_BUFFER_SZ, PHP_NORMAL_READ)) === FALSE) 116 263 { 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)); 121 271 } 122 272 … … 130 280 if($retval == "DELETED") 131 281 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 */ 141 311 function add($key, $val, $exptime = 0) 142 312 { … … 145 315 146 316 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 */ 148 333 function replace($key, $val, $exptime = 0) 149 334 { … … 152 337 153 338 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 */ 155 357 function set($key, $val, $exptime = 0) 156 358 { … … 159 361 160 362 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 */ 163 378 function get($key) 164 379 { … … 166 381 167 382 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 } 169 392 170 393 return $val[$key]; … … 172 395 173 396 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 */ 175 406 function get_multi($keys) 176 407 { … … 180 411 181 412 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 } 183 422 184 423 if(!is_array($keys)) … … 221 460 { 222 461 while(list($k, $v) = each($val)) 223 print "MemCache: got $k = $v\n";462 $this->_debug("MemCache: got $k = $v"); 224 463 } 225 464 … … 228 467 229 468 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) 234 488 { 235 489 return $this->_incrdecr("incr", $key, $value); … … 237 491 238 492 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) 243 510 { 244 511 return $this->_incrdecr("decr", $key, $value); … … 246 513 247 514 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 /* 250 556 * 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 */ 254 574 function sock_to_host($host) 255 575 { … … 263 583 if(count($conn) != 2) 264 584 { 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"); 267 590 268 591 return FALSE; … … 272 595 ($this->host_dead[$conn[0]] && $this->host_dead[$conn[0]] > $now)) 273 596 { 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."); 276 602 277 603 return FALSE; … … 286 612 $this->host_dead[$host]=$this->host_dead[$conn[0]]=$now+60+intval(rand(0, 10)); 287 613 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]); 290 619 291 620 return FALSE; … … 299 628 300 629 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 */ 302 640 function get_sock($key) 303 641 { 642 $buckets = 0; 643 304 644 if(!$this->active) 305 645 { 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"); 308 651 309 652 return FALSE; … … 312 655 $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key); 313 656 314 if(!$ this->buckets)315 { 316 $bu = array();657 if(!$buckets) 658 { 659 $bu = $buckets = array(); 317 660 318 661 foreach($this->servers as $v) … … 327 670 } 328 671 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; 333 676 $tries = 0; 334 677 while($tries < 20) 335 678 { 336 $host = $ this->buckets[$hv % $this->bucketcount];679 $host = $buckets[$hv % count($buckets)]; 337 680 $sock = $this->sock_to_host($host); 338 681 … … 340 683 return $sock; 341 684 342 $hv += $this->_hashfunc($tries );685 $hv += $this->_hashfunc($tries.$real_key); 343 686 ++$tries; 344 687 } 345 688 689 $this->errno = MC_ERR_GET_SOCK; 690 $this->errstr = "Unable to retrieve a valid socket."; 691 346 692 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"); 348 694 349 695 return FALSE; … … 351 697 352 698 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 */ 357 715 function _incrdecr($cmdname, $key, $value) 358 716 { 359 717 if(!$this->active) 360 718 { 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"); 363 724 364 725 return FALSE; … … 368 729 if(!is_resource($sock)) 369 730 { 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 } 377 739 378 740 if($value == "") … … 383 745 $offset = 0; 384 746 385 // now send the command747 // write the command to the server 386 748 while($offset < $cmd_len) 387 749 { … … 392 754 else if($offset < $cmd_len) 393 755 {
