69 use Psr\Log\LoggerInterface;
70 use Psr\Log\NullLogger;
261 $this->_debug =
$args[
'debug'] ??
false;
262 $this->stats = array();
263 $this->_compress_threshold =
$args[
'compress_threshold'] ?? 0;
264 $this->_persistent =
$args[
'persistent'] ??
false;
265 $this->_compress_enable =
true;
266 $this->_have_zlib = function_exists(
'gzcompress' );
268 $this->_cache_sock = array();
269 $this->_host_dead = array();
271 $this->_timeout_seconds = 0;
272 $this->_timeout_microseconds =
$args[
'timeout'] ?? 500000;
274 $this->_connect_timeout =
$args[
'connect_timeout'] ?? 0.1;
275 $this->_connect_attempts = 2;
277 $this->_logger =
$args[
'logger'] ??
new NullLogger();
314 public function add( $key, $val, $exp = 0 ) {
315 return $this->
_set(
'add', $key, $val, $exp );
329 public function decr( $key, $amt = 1 ) {
330 return $this->
_incrdecr(
'decr', $key, $amt );
344 public function delete( $key, $time = 0 ) {
345 if ( !$this->_active ) {
350 if ( !is_resource( $sock ) ) {
354 $key = is_array( $key ) ? $key[1] : $key;
356 if ( isset( $this->stats[
'delete'] ) ) {
357 $this->stats[
'delete']++;
359 $this->stats[
'delete'] = 1;
361 $cmd =
"delete $key $time\r\n";
362 if ( !$this->
_fwrite( $sock, $cmd ) ) {
367 if ( $this->_debug ) {
368 $this->
_debugprint( sprintf(
"MemCache: delete %s (%s)", $key,
$res ) );
371 if (
$res ==
"DELETED" ||
$res ==
"NOT_FOUND" ) {
386 public function touch( $key, $time = 0 ) {
387 if ( !$this->_active ) {
392 if ( !is_resource( $sock ) ) {
396 $key = is_array( $key ) ? $key[1] : $key;
398 if ( isset( $this->stats[
'touch'] ) ) {
399 $this->stats[
'touch']++;
401 $this->stats[
'touch'] = 1;
403 $cmd =
"touch $key $time\r\n";
404 if ( !$this->
_fwrite( $sock, $cmd ) ) {
409 if ( $this->_debug ) {
410 $this->
_debugprint( sprintf(
"MemCache: touch %s (%s)", $key,
$res ) );
413 if (
$res ==
"TOUCHED" ) {
427 foreach ( $this->_cache_sock as $sock ) {
431 $this->_cache_sock = array();
443 $this->_compress_enable = $enable;
453 $this->_host_dead = array();
467 public function get( $key, &$casToken = null ) {
468 if ( $this->_debug ) {
472 if ( !is_array( $key ) && strval( $key ) ===
'' ) {
473 $this->
_debugprint(
"Skipping key which equals to an empty string" );
477 if ( !$this->_active ) {
483 if ( !is_resource( $sock ) ) {
487 $key = is_array( $key ) ? $key[1] : $key;
488 if ( isset( $this->stats[
'get'] ) ) {
489 $this->stats[
'get']++;
491 $this->stats[
'get'] = 1;
494 $cmd =
"gets $key\r\n";
495 if ( !$this->
_fwrite( $sock, $cmd ) ) {
502 if ( $this->_debug ) {
503 foreach ( $val as $k => $v ) {
505 sprintf(
"MemCache: sock %s got %s", $this->
serialize( $sock ), $k ) );
510 if ( isset( $val[$key] ) ) {
527 if ( !$this->_active ) {
531 if ( isset( $this->stats[
'get_multi'] ) ) {
532 $this->stats[
'get_multi']++;
534 $this->stats[
'get_multi'] = 1;
536 $sock_keys = array();
538 foreach (
$keys as $key ) {
540 if ( !is_resource( $sock ) ) {
543 $key = is_array( $key ) ? $key[1] : $key;
544 if ( !isset( $sock_keys[$sock] ) ) {
545 $sock_keys[intval( $sock )] = array();
548 $sock_keys[intval( $sock )][] = $key;
553 foreach ( $socks as $sock ) {
555 foreach ( $sock_keys[intval( $sock )] as $key ) {
560 if ( $this->
_fwrite( $sock, $cmd ) ) {
567 foreach ( $gather as $sock ) {
571 if ( $this->_debug ) {
572 foreach ( $val as $k => $v ) {
573 $this->
_debugprint( sprintf(
"MemCache: got %s", $k ) );
593 public function incr( $key, $amt = 1 ) {
594 return $this->
_incrdecr(
'incr', $key, $amt );
613 public function replace( $key, $value, $exp = 0 ) {
614 return $this->
_set(
'replace', $key, $value, $exp );
630 if ( !is_resource( $sock ) ) {
634 if ( !$this->
_fwrite( $sock, $cmd ) ) {
642 if ( preg_match(
'/^END/',
$res ) ) {
645 if ( strlen(
$res ) == 0 ) {
669 public function set( $key, $value, $exp = 0 ) {
670 return $this->
_set(
'set', $key, $value, $exp );
691 public function cas( $casToken, $key, $value, $exp = 0 ) {
692 return $this->
_set(
'cas', $key, $value, $exp, $casToken );
704 $this->_compress_threshold = $thresh;
717 $this->_debug = $dbg;
730 $this->_servers = $list;
731 $this->_active = count( $list );
732 $this->_buckets =
null;
733 $this->_bucketcount = 0;
735 $this->_single_sock =
null;
736 if ( $this->_active == 1 ) {
737 $this->_single_sock = $this->_servers[0];
748 $this->_timeout_seconds = $seconds;
749 $this->_timeout_microseconds = $microseconds;
765 $host = array_search( $sock, $this->_cache_sock );
766 fclose( $this->_cache_sock[$host] );
767 unset( $this->_cache_sock[$host] );
783 list( $ip, $port ) = preg_split(
'/:(?=\d)/', $host );
786 $errno = $errstr =
null;
788 Wikimedia\suppressWarnings();
789 if ( $this->_persistent == 1 ) {
790 $sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
792 $sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
794 Wikimedia\restoreWarnings();
797 $this->
_error_log(
"Error connecting to $host: $errstr" );
803 stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds );
807 if ( $this->_persistent ) {
824 $host = array_search( $sock, $this->_cache_sock );
832 $ip = explode(
':', $host )[0];
833 $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
834 $this->_host_dead[$host] = $this->_host_dead[$ip];
835 unset( $this->_cache_sock[$host] );
850 if ( !$this->_active ) {
854 if ( $this->_single_sock !==
null ) {
858 $hv = is_array( $key ) ? intval( $key[0] ) : $this->
_hashfunc( $key );
859 if ( $this->_buckets ===
null ) {
861 foreach ( $this->_servers as $v ) {
862 if ( is_array( $v ) ) {
863 for ( $i = 0; $i < $v[1]; $i++ ) {
870 $this->_buckets = $bu;
871 $this->_bucketcount = count( $bu );
874 $realkey = is_array( $key ) ? $key[1] : $key;
875 for ( $tries = 0; $tries < 20; $tries++ ) {
878 if ( is_resource( $sock ) ) {
881 $hv = $this->
_hashfunc( $hv . $realkey );
899 # Hash function must be in [0,0x7ffffff]
900 # We take the first 31 bits of the MD5 hash, which unlike the hash
901 # function used in a previous version of this client, works
902 return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
919 if ( !$this->_active ) {
924 if ( !is_resource( $sock ) ) {
928 $key = is_array( $key ) ? $key[1] : $key;
929 if ( isset( $this->stats[$cmd] ) ) {
930 $this->stats[$cmd]++;
932 $this->stats[$cmd] = 1;
934 if ( !$this->
_fwrite( $sock,
"$cmd $key $amt\r\n" ) ) {
940 if ( !preg_match(
'/^(\d+)/',
$line, $match ) ) {
963 $decl = $this->
_fgets( $sock );
965 if ( $decl ===
false ) {
971 } elseif ( preg_match(
'/^VALUE (\S+) (\d+) (\d+) (\d+)$/', $decl, $match ) ) {
984 $this->
_fread( $sock, $match[3] + 2 ),
986 } elseif ( $decl ==
"END" ) {
987 if ( count( $results ) == 0 ) {
995 foreach ( $results as $vars ) {
996 list( $rkey, $flags, $len, $casToken, $data ) = $vars;
998 if ( $data ===
false || substr( $data, -2 ) !==
"\r\n" ) {
1000 'line ending missing from data block from $1' );
1003 $data = substr( $data, 0, -2 );
1004 $ret[$rkey] = $data;
1006 if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
1007 $ret[$rkey] = gzuncompress( $ret[$rkey] );
1019 if ( $flags & self::SERIALIZED ) {
1021 } elseif ( $flags & self::INTVAL ) {
1022 $ret[$rkey] = intval( $ret[$rkey] );
1028 $this->
_handle_error( $sock,
'Error parsing response from $1' );
1053 function _set( $cmd, $key, $val, $exp, $casToken =
null ) {
1054 if ( !$this->_active ) {
1059 if ( !is_resource( $sock ) ) {
1063 if ( isset( $this->stats[$cmd] ) ) {
1064 $this->stats[$cmd]++;
1066 $this->stats[$cmd] = 1;
1071 if ( is_int( $val ) ) {
1073 } elseif ( !is_scalar( $val ) ) {
1076 if ( $this->_debug ) {
1077 $this->
_debugprint( sprintf(
"client: serializing data as it is not scalar" ) );
1081 $len = strlen( $val );
1083 if ( $this->_have_zlib && $this->_compress_enable
1084 && $this->_compress_threshold && $len >= $this->_compress_threshold
1086 $c_val = gzcompress( $val, 9 );
1087 $c_len = strlen( $c_val );
1089 if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) {
1090 if ( $this->_debug ) {
1091 $this->
_debugprint( sprintf(
"client: compressing data; was %d bytes is now %d bytes", $len, $c_len ) );
1099 $command =
"$cmd $key $flags $exp $len";
1104 if ( !$this->
_fwrite( $sock,
"$command\r\n$val\r\n" ) ) {
1110 if ( $this->_debug ) {
1113 if (
$line ===
"STORED" ) {
1115 } elseif (
$line ===
"NOT_STORED" && $cmd ===
"set" ) {
1135 if ( isset( $this->_cache_sock[$host] ) ) {
1136 return $this->_cache_sock[$host];
1141 list( $ip, ) = explode(
':', $host );
1142 if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now ||
1143 isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now
1153 stream_set_write_buffer( $sock, 0 );
1155 $this->_cache_sock[$host] = $sock;
1157 return $this->_cache_sock[$host];
1164 $this->_logger->debug( $text );
1171 $this->_logger->error(
"Memcached error: $text" );
1183 $bufSize = strlen( $buf );
1184 while ( $bytesWritten < $bufSize ) {
1185 $result = fwrite( $sock, $buf );
1186 $data = stream_get_meta_data( $sock );
1187 if ( $data[
'timed_out'] ) {
1192 if ( $result ===
false || $result === 0 ) {
1196 $bytesWritten += $result;
1209 $peer = stream_socket_get_name( $sock,
true );
1210 if ( strval( $peer ) ===
'' ) {
1211 $peer = array_search( $sock, $this->_cache_sock );
1212 if ( $peer ===
false ) {
1213 $peer =
'[unknown host]';
1216 $msg = str_replace(
'$1', $peer, $msg );
1231 while ( $len > 0 ) {
1232 $result = fread( $sock, $len );
1233 $data = stream_get_meta_data( $sock );
1234 if ( $data[
'timed_out'] ) {
1238 if ( $result ===
false ) {
1239 $this->
_handle_error( $sock,
'error reading buffer from $1' );
1242 if ( $result ===
'' ) {
1244 $this->
_handle_error( $sock,
'unexpected end of file reading from $1' );
1247 $len -= strlen( $result );
1261 $result = fgets( $sock );
1265 $data = stream_get_meta_data( $sock );
1266 if ( $data[
'timed_out'] ) {
1267 $this->
_handle_error( $sock,
'timeout reading line from $1' );
1270 if ( $result ===
false ) {
1271 $this->
_handle_error( $sock,
'error reading line from $1' );
1274 if ( substr( $result, -2 ) ===
"\r\n" ) {
1275 $result = substr( $result, 0, -2 );
1276 } elseif ( substr( $result, -1 ) ===
"\n" ) {
1277 $result = substr( $result, 0, -1 );
1279 $this->
_handle_error( $sock,
'line ending missing in response from $1' );
1290 if ( !is_resource( $f ) ) {
1296 $n = stream_select( $r, $w, $e, 0, 0 );
1297 while ( $n == 1 && !feof( $f ) ) {
1302 $n = stream_select( $r, $w, $e, 0, 0 );