69use Psr\Log\LoggerInterface;
70use Psr\Log\NullLogger;
266 $this->_debug = $args[
'debug'] ??
false;
267 $this->stats = array();
268 $this->_compress_threshold = $args[
'compress_threshold'] ?? 0;
269 $this->_persistent = $args[
'persistent'] ??
false;
270 $this->_compress_enable =
true;
271 $this->_have_zlib = function_exists(
'gzcompress' );
273 $this->_cache_sock = array();
274 $this->_host_dead = array();
276 $this->_timeout_seconds = 0;
277 $this->_timeout_microseconds = $args[
'timeout'] ?? 500_000;
279 $this->_connect_timeout = $args[
'connect_timeout'] ?? 0.1;
280 $this->_connect_attempts = 2;
282 $this->_logger = $args[
'logger'] ??
new NullLogger();
319 public function add( $key, $val, $exp = 0 ) {
320 $this->_last_cmd_status = BagOStuff::ERR_NONE;
322 return $this->
_set(
'add', $key, $val, $exp );
336 public function decr( $key, $amt = 1 ) {
337 $this->_last_cmd_status = BagOStuff::ERR_NONE;
339 return $this->
_incrdecr(
'decr', $key, $amt );
353 public function delete( $key, $time = 0 ) {
354 $this->_last_cmd_status = BagOStuff::ERR_NONE;
356 if ( !$this->_active ) {
365 $key = is_array( $key ) ? $key[1] : $key;
367 if ( isset( $this->stats[
'delete'] ) ) {
368 $this->stats[
'delete']++;
370 $this->stats[
'delete'] = 1;
372 $cmd =
"delete $key $time\r\n";
373 if ( !$this->
_fwrite( $sock, $cmd ) ) {
376 $res = $this->
_fgets( $sock );
378 if ( $this->_debug ) {
379 $this->
_debugprint( sprintf(
"MemCache: delete %s (%s)", $key, $res ) );
382 if ( $res ==
"DELETED" || $res ==
"NOT_FOUND" ) {
386 $this->_last_cmd_status = BagOStuff::ERR_UNEXPECTED;
399 public function touch( $key, $time = 0 ) {
400 $this->_last_cmd_status = BagOStuff::ERR_NONE;
402 if ( !$this->_active ) {
411 $key = is_array( $key ) ? $key[1] : $key;
413 if ( isset( $this->stats[
'touch'] ) ) {
414 $this->stats[
'touch']++;
416 $this->stats[
'touch'] = 1;
418 $cmd =
"touch $key $time\r\n";
419 if ( !$this->
_fwrite( $sock, $cmd ) ) {
422 $res = $this->
_fgets( $sock );
424 if ( $this->_debug ) {
425 $this->
_debugprint( sprintf(
"MemCache: touch %s (%s)", $key, $res ) );
428 if ( $res ==
"TOUCHED" ) {
442 foreach ( $this->_cache_sock as $sock ) {
446 $this->_cache_sock = array();
458 $this->_compress_enable = $enable;
468 $this->_host_dead = array();
482 public function get( $key, &$casToken = null ) {
483 $getToken = ( func_num_args() >= 2 );
485 $this->_last_cmd_status = BagOStuff::ERR_NONE;
487 if ( $this->_debug ) {
491 if ( !is_array( $key ) && strval( $key ) ===
'' ) {
492 $this->_last_cmd_status = BagOStuff::ERR_UNEXPECTED;
493 $this->
_debugprint(
"Skipping key which equals to an empty string" );
497 if ( !$this->_active ) {
498 $this->_last_cmd_status = BagOStuff::ERR_UNEXPECTED;
506 $this->_last_cmd_status = BagOStuff::ERR_UNREACHABLE;
511 $key = is_array( $key ) ? $key[1] : $key;
512 if ( isset( $this->stats[
'get'] ) ) {
513 $this->stats[
'get']++;
515 $this->stats[
'get'] = 1;
518 $cmd = $getToken ?
"gets" :
"get";
520 if ( !$this->
_fwrite( $sock, $cmd ) ) {
521 $this->_last_cmd_status = BagOStuff::ERR_NO_RESPONSE;
527 if ( !$this->
_load_items( $sock, $val, $casToken ) ) {
528 $this->_last_cmd_status = BagOStuff::ERR_NO_RESPONSE;
531 if ( $this->_debug ) {
532 foreach ( $val as $k => $v ) {
534 sprintf(
"MemCache: sock %s got %s", $this->
serialize( $sock ), $k ) );
539 if ( isset( $val[$key] ) ) {
556 $this->_last_cmd_status = BagOStuff::ERR_NONE;
558 if ( !$this->_active ) {
559 $this->_last_cmd_status = BagOStuff::ERR_UNEXPECTED;
564 if ( isset( $this->stats[
'get_multi'] ) ) {
565 $this->stats[
'get_multi']++;
567 $this->stats[
'get_multi'] = 1;
569 $sock_keys = array();
571 foreach ( $keys as $key ) {
574 $this->_last_cmd_status = BagOStuff::ERR_UNREACHABLE;
577 $key = is_array( $key ) ? $key[1] : $key;
578 $sockValue = intval( $sock );
580 if ( !isset( $sock_keys[$sockValue] ) ) {
581 $sock_keys[$sockValue] = array();
584 $sock_keys[$sockValue][] = $key;
589 foreach ( $socks as $sock ) {
591 foreach ( $sock_keys[intval( $sock )] as $key ) {
596 if ( $this->
_fwrite( $sock, $cmd ) ) {
599 $this->_last_cmd_status = BagOStuff::ERR_NO_RESPONSE;
605 foreach ( $gather as $sock ) {
607 $this->_last_cmd_status = BagOStuff::ERR_NO_RESPONSE;
611 if ( $this->_debug ) {
612 foreach ( $val as $k => $v ) {
613 $this->
_debugprint( sprintf(
"MemCache: got %s", $k ) );
633 public function incr( $key, $amt = 1 ) {
634 return $this->
_incrdecr(
'incr', $key, $amt );
653 public function replace( $key, $value, $exp = 0 ) {
654 return $this->
_set(
'replace', $key, $value, $exp );
674 if ( !$this->
_fwrite( $sock, $cmd ) ) {
680 $res = $this->
_fgets( $sock );
682 if ( preg_match(
'/^END/', $res ) ) {
685 if ( strlen( $res ) == 0 ) {
709 public function set( $key, $value, $exp = 0 ) {
710 return $this->
_set(
'set', $key, $value, $exp );
731 public function cas( $casToken, $key, $value, $exp = 0 ) {
732 return $this->
_set(
'cas', $key, $value, $exp, $casToken );
744 $this->_compress_threshold = $thresh;
757 $this->_debug = $dbg;
770 $this->_servers = $list;
771 $this->_active = count( $list );
772 $this->_buckets =
null;
773 $this->_bucketcount = 0;
775 $this->_single_sock =
null;
776 if ( $this->_active == 1 ) {
777 $this->_single_sock = $this->_servers[0];
788 $this->_timeout_seconds = $seconds;
789 $this->_timeout_microseconds = $microseconds;
805 $host = array_search( $sock, $this->_cache_sock );
806 fclose( $this->_cache_sock[$host] );
807 unset( $this->_cache_sock[$host] );
824 $hostAndPort = IPUtils::splitHostAndPort( $host );
825 if ( $hostAndPort ) {
826 $ip = $hostAndPort[0];
827 if ( $hostAndPort[1] ) {
828 $port = $hostAndPort[1];
834 $timeout = $this->_connect_timeout;
835 $errno = $errstr =
null;
836 for ( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
838 if ( $this->_persistent == 1 ) {
839 $sock = @pfsockopen( $ip, $port, $errno, $errstr, $timeout );
841 $sock = @fsockopen( $ip, $port, $errno, $errstr, $timeout );
845 $this->
_error_log(
"Error connecting to $host: $errstr" );
851 stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds );
855 if ( $this->_persistent ) {
872 $host = array_search( $sock, $this->_cache_sock );
880 $hostAndPort = IPUtils::splitHostAndPort( $host );
881 if ( $hostAndPort ) {
882 $ip = $hostAndPort[0];
886 $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
887 $this->_host_dead[$host] = $this->_host_dead[$ip];
888 unset( $this->_cache_sock[$host] );
903 if ( !$this->_active ) {
907 if ( $this->_single_sock !==
null ) {
911 $hv = is_array( $key ) ? intval( $key[0] ) : $this->
_hashfunc( $key );
912 if ( $this->_buckets ===
null ) {
914 foreach ( $this->_servers as $v ) {
915 if ( is_array( $v ) ) {
916 for ( $i = 0; $i < $v[1]; $i++ ) {
923 $this->_buckets = $bu;
924 $this->_bucketcount = count( $bu );
927 $realkey = is_array( $key ) ? $key[1] : $key;
928 for ( $tries = 0; $tries < 20; $tries++ ) {
929 $host = $this->_buckets[$hv % $this->_bucketcount];
934 $hv = $this->
_hashfunc( $hv . $realkey );
952 # Hash function must be in [0,0x7ffffff]
953 # We take the first 31 bits of the MD5 hash, which unlike the hash
954 # function used in a previous version of this client, works
955 return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
972 $this->_last_cmd_status = BagOStuff::ERR_NONE;
974 if ( !$this->_active ) {
975 $this->_last_cmd_status = BagOStuff::ERR_UNEXPECTED;
982 $this->_last_cmd_status = BagOStuff::ERR_UNREACHABLE;
987 $key = is_array( $key ) ? $key[1] : $key;
988 if ( isset( $this->stats[$cmd] ) ) {
989 $this->stats[$cmd]++;
991 $this->stats[$cmd] = 1;
993 if ( !$this->
_fwrite( $sock,
"$cmd $key $amt\r\n" ) ) {
994 $this->_last_cmd_status = BagOStuff::ERR_NO_RESPONSE;
999 $line = $this->
_fgets( $sock );
1000 if ( $this->_debug ) {
1005 if ( !preg_match(
'/^(\d+)/', $line, $match ) ) {
1006 $this->_last_cmd_status = BagOStuff::ERR_NO_RESPONSE;
1011 return (
int)$match[1];
1031 $decl = $this->
_fgets( $sock );
1033 if ( $decl ===
false ) {
1039 } elseif ( preg_match(
'/^VALUE (\S+) (\d+) (\d+)(?: (\d+))?$/', $decl, $match ) ) {
1052 $this->
_fread( $sock, $match[3] + 2 ),
1054 } elseif ( $decl ==
"END" ) {
1059 foreach ( $results as [ $rkey, $flags, , $casToken, $data ] ) {
1060 if ( $data ===
false || !str_ends_with( $data,
"\r\n" ) ) {
1062 'line ending missing from data block from $1' );
1065 $data = substr( $data, 0, -2 );
1066 $ret[$rkey] = $data;
1068 if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
1069 $ret[$rkey] = gzuncompress( $ret[$rkey] );
1081 if ( $flags & self::SERIALIZED ) {
1083 } elseif ( $flags & self::INTVAL ) {
1084 $ret[$rkey] = intval( $ret[$rkey] );
1090 $this->
_handle_error( $sock,
'Error parsing response from $1' );
1115 function _set( $cmd, $key, $val, $exp, $casToken =
null ) {
1116 $this->_last_cmd_status = BagOStuff::ERR_NONE;
1118 if ( !$this->_active ) {
1119 $this->_last_cmd_status = BagOStuff::ERR_UNEXPECTED;
1126 $this->_last_cmd_status = BagOStuff::ERR_UNREACHABLE;
1131 if ( isset( $this->stats[$cmd] ) ) {
1132 $this->stats[$cmd]++;
1134 $this->stats[$cmd] = 1;
1139 if ( is_int( $val ) ) {
1140 $flags |= self::INTVAL;
1141 } elseif ( !is_scalar( $val ) ) {
1143 $flags |= self::SERIALIZED;
1144 if ( $this->_debug ) {
1145 $this->
_debugprint(
"client: serializing data as it is not scalar" );
1149 $len = strlen( $val );
1151 if ( $this->_have_zlib && $this->_compress_enable
1152 && $this->_compress_threshold && $len >= $this->_compress_threshold
1154 $c_val = gzcompress( $val, 9 );
1155 $c_len = strlen( $c_val );
1157 if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) {
1158 if ( $this->_debug ) {
1159 $this->
_debugprint( sprintf(
"client: compressing data; was %d bytes is now %d bytes", $len, $c_len ) );
1163 $flags |= self::COMPRESSED;
1167 $command =
"$cmd $key $flags $exp $len";
1169 $command .=
" $casToken";
1172 if ( !$this->
_fwrite( $sock,
"$command\r\n$val\r\n" ) ) {
1173 $this->_last_cmd_status = BagOStuff::ERR_NO_RESPONSE;
1178 $line = $this->
_fgets( $sock );
1179 if ( $this->_debug ) {
1180 $this->
_debugprint( sprintf(
"%s %s (%s)", $cmd, $key, $line ) );
1183 if ( $line ===
"STORED" ) {
1185 } elseif ( $line ===
"NOT_STORED" && $cmd ===
"set" ) {
1190 if ( $line ===
false ) {
1191 $this->_last_cmd_status = BagOStuff::ERR_NO_RESPONSE;
1209 if ( isset( $this->_cache_sock[$host] ) ) {
1210 return $this->_cache_sock[$host];
1215 $hostAndPort = IPUtils::splitHostAndPort( $host );
1216 if ( $hostAndPort ) {
1217 $ip = $hostAndPort[0];
1221 if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now ||
1222 isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now
1232 stream_set_write_buffer( $sock, 0 );
1234 $this->_cache_sock[$host] = $sock;
1236 return $this->_cache_sock[$host];
1243 $this->_logger->debug( $text );
1250 $this->_logger->error(
"Memcached error: $text" );
1262 $bufSize = strlen( $buf );
1263 while ( $bytesWritten < $bufSize ) {
1264 $result = fwrite( $sock, $buf );
1265 $data = stream_get_meta_data( $sock );
1266 if ( $data[
'timed_out'] ) {
1271 if ( $result ===
false || $result === 0 ) {
1275 $bytesWritten += $result;
1288 $peer = stream_socket_get_name( $sock,
true );
1289 if ( strval( $peer ) ===
'' ) {
1290 $peer = array_search( $sock, $this->_cache_sock );
1291 if ( $peer ===
false ) {
1292 $peer =
'[unknown host]';
1295 $msg = str_replace(
'$1', $peer, $msg );
1310 while ( $len > 0 ) {
1311 $result = fread( $sock, $len );
1312 $data = stream_get_meta_data( $sock );
1313 if ( $data[
'timed_out'] ) {
1317 if ( $result ===
false ) {
1318 $this->
_handle_error( $sock,
'error reading buffer from $1' );
1321 if ( $result ===
'' ) {
1323 $this->
_handle_error( $sock,
'unexpected end of file reading from $1' );
1326 $len -= strlen( $result );
1340 $result = fgets( $sock );
1344 $data = stream_get_meta_data( $sock );
1345 if ( $data[
'timed_out'] ) {
1346 $this->
_handle_error( $sock,
'timeout reading line from $1' );
1349 if ( $result ===
false ) {
1350 $this->
_handle_error( $sock,
'error reading line from $1' );
1353 if ( str_ends_with( $result,
"\r\n" ) ) {
1354 $result = substr( $result, 0, -2 );
1355 } elseif ( str_ends_with( $result,
"\n" ) ) {
1356 $result = substr( $result, 0, -1 );
1358 $this->
_handle_error( $sock,
'line ending missing in response from $1' );
1375 $n = stream_select( $r, $w, $e, 0, 0 );
1376 while ( $n == 1 && !feof( $f ) ) {
1381 $n = stream_select( $r, $w, $e, 0, 0 );
memcached client class implemented using (p)fsockopen()
set_debug( $dbg)
Set the debug flag.
array $_cache_sock
Cached Sockets that are connected.
const SERIALIZED
Flag: indicates data is serialized.
const COMPRESSION_SAVINGS
Minimum savings to store data compressed.
_hashfunc( $key)
Creates a hash integer based on the $key.
set_compress_threshold( $thresh)
Set the compression threshold.
_fread( $sock, $len)
Read the specified number of bytes from a stream.
array $_servers
Array containing ip:port or array(ip:port, weight)
bool $_compress_enable
Do we want to use compression?
decr( $key, $amt=1)
Decrease a value stored on the memcache server.
const INTVAL
Flag: indicates data is an integer.
int $_compress_threshold
At how many bytes should we compress?
int $_timeout_seconds
Stream timeout in seconds.
disconnect_all()
Disconnects all connected sockets.
cas( $casToken, $key, $value, $exp=0)
Sets a key to a given value in the memcache if the current value still corresponds to a known,...
array $_buckets
Our bit buckets.
set_timeout( $seconds, $microseconds)
Sets the timeout for new connections.
_fgets( $sock)
Read a line from a stream.
incr( $key, $amt=1)
Increments $key (optionally) by $amt.
forget_dead_hosts()
Forget about all of the dead hosts.
_incrdecr( $cmd, $key, $amt=1)
Perform increment/decrement on $key.
$_connect_attempts
Number of connection attempts for each server.
add( $key, $val, $exp=0)
Adds a key/value to the memcache server if one isn't already set with that key.
_handle_error( $sock, $msg)
Handle an I/O error.
sock_to_host( $host)
Returns the socket for the host.
int $_last_cmd_status
BagOStuff:ERR_* constant of the last cache command.
$_connect_timeout
Connect timeout in seconds.
touch( $key, $time=0)
Changes the TTL on a key from the server to $time.
bool $_have_zlib
Is compression available?
int $_bucketcount
Total # of bit buckets we have.
_fwrite( $sock, $buf)
Write to a stream.
bool $_debug
Current debug status; 0 - none to 9 - profiling.
replace( $key, $value, $exp=0)
Overwrites an existing value for key; only works if key is already set.
set_servers( $list)
Set the server list to distribute key gets and puts between.
_load_items( $sock, &$ret, &$casToken=null)
Load items into $ret from $sock.
string $_single_sock
If only using one server; contains ip:port to connect to.
get_multi( $keys)
Get multiple keys from the server(s)
_close_sock( $sock)
Close the specified socket.
run_command( $sock, $cmd)
Passes through $cmd to the memcache server connected by $sock; returns output as an array (null array...
_set( $cmd, $key, $val, $exp, $casToken=null)
Performs the requested storage operation to the memcache server.
int $_timeout_microseconds
Stream timeout in microseconds.
__construct( $args)
Memcache initializer.
_flush_read_buffer( $f)
Flush the read buffer of a stream.
enable_compress( $enable)
Enable / Disable compression.
array $_host_dead
Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'.
const COMPRESSED
Flag: indicates data is compressed.
_dead_sock( $sock)
Marks a host as dead until 30-40 seconds in the future.
_connect_sock(&$sock, $host)
Connects $sock to $host, timing out after $timeout.
array $stats
Command statistics.
bool $_persistent
Are we using persistent links?