58 if (
count( $this->client->getServerList() ) ) {
59 $this->logger->debug( __METHOD__ .
": persistent Memcached object already loaded." );
66 if (
$params[
'use_binary_protocol'] ) {
67 $this->client->setOption( Memcached::OPT_BINARY_PROTOCOL,
true );
70 if ( isset(
$params[
'retry_timeout'] ) ) {
71 $this->client->setOption( Memcached::OPT_RETRY_TIMEOUT,
$params[
'retry_timeout'] );
74 if ( isset(
$params[
'server_failure_limit'] ) ) {
75 $this->client->setOption( Memcached::OPT_SERVER_FAILURE_LIMIT,
$params[
'server_failure_limit'] );
81 ini_set(
'memcached.compression_threshold',
$params[
'compress_threshold'] );
84 $this->client->setOption( Memcached::OPT_CONNECT_TIMEOUT,
$params[
'connect_timeout'] * 1000 );
85 $this->client->setOption( Memcached::OPT_SEND_TIMEOUT,
$params[
'timeout'] );
86 $this->client->setOption( Memcached::OPT_RECV_TIMEOUT,
$params[
'timeout'] );
87 $this->client->setOption( Memcached::OPT_POLL_TIMEOUT,
$params[
'timeout'] / 1000 );
95 $this->client->setOption( Memcached::OPT_LIBKETAMA_COMPATIBLE,
true );
98 switch (
$params[
'serializer'] ) {
100 $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_PHP );
103 if ( !Memcached::HAVE_IGBINARY ) {
104 throw new InvalidArgumentException(
105 __CLASS__ .
': the igbinary extension is not available ' .
106 'but igbinary serialization was requested.'
109 $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY );
112 throw new InvalidArgumentException(
113 __CLASS__ .
': invalid value for serializer parameter'
117 foreach (
$params[
'servers']
as $host ) {
118 if ( preg_match(
'/^\[(.+)\]:(\d+)$/', $host, $m ) ) {
119 $servers[] = [ $m[1], (int)$m[2] ];
120 } elseif ( preg_match(
'/^([^:]+):(\d+)$/', $host, $m ) ) {
121 $servers[] = [ $m[1], (int)$m[2] ];
123 $servers[] = [ $host,
false ];
126 $this->client->addServers( $servers );
132 if ( !isset(
$params[
'use_binary_protocol'] ) ) {
133 $params[
'use_binary_protocol'] =
false;
136 if ( !isset(
$params[
'serializer'] ) ) {
155 protected function cas( $casToken, $key,
$value, $exptime = 0 ) {
157 return $this->
checkResult( $key, parent::cas( $casToken, $key,
$value, $exptime ) );
160 public function delete( $key ) {
162 $result = parent::delete( $key );
163 if (
$result ===
false && $this->client->getResultCode() === Memcached::RES_NOTFOUND ) {
203 switch ( $this->client->getResultCode() ) {
204 case Memcached::RES_SUCCESS:
206 case Memcached::RES_DATA_EXISTS:
207 case Memcached::RES_NOTSTORED:
208 case Memcached::RES_NOTFOUND:
209 $this->
debugLog(
"result: " . $this->client->getResultMessage() );
212 $msg = $this->client->getResultMessage();
214 if ( $key !==
false ) {
215 $server = $this->client->getServerByKey( $key );
216 $logCtx[
'memcached-server'] =
"{$server['host']}:{$server['port']}";
217 $logCtx[
'memcached-key'] = $key;
218 $msg =
"Memcached error for key \"{memcached-key}\" on server \"{memcached-server}\": $msg";
220 $msg =
"Memcached error: $msg";
222 $this->logger->error( $msg, $logCtx );
229 $this->
debugLog(
'getMulti(' . implode(
', ',
$keys ) .
')' );
243 $this->
debugLog(
'setMulti(' . implode(
', ', array_keys( $data ) ) .
')' );
244 foreach ( array_keys( $data )
as $key ) {
253 $result = $this->client->touch( $key, $expiry );