3 use Psr\Log\LoggerInterface;
56 if ( empty(
$params[
'url'] ) ) {
57 throw new InvalidArgumentException(
'URL parameter is required' );
59 if ( empty(
$params[
'client'] ) ) {
65 foreach ( [
'caBundlePath',
'proxy' ]
as $key ) {
67 $clientParams[$key] =
$params[$key];
72 $this->client =
$params[
'client'];
77 $this->url = rtrim(
$params[
'url'],
'/' ) .
'/';
84 $this->client->setLogger(
$logger );
87 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
92 'url' => $this->url . rawurlencode( $key ),
95 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run(
$req );
96 if ( $rcode === 200 ) {
97 if ( is_string( $rbody ) ) {
106 if ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
107 return $this->
handleError(
"Failed to fetch $key", $rcode, $rerr );
112 public function set( $key,
$value, $exptime = 0, $flags = 0 ) {
117 'url' => $this->url . rawurlencode( $key ),
120 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run(
$req );
121 if ( $rcode === 200 || $rcode === 201 || $rcode === 204 ) {
124 return $this->
handleError(
"Failed to store $key", $rcode, $rerr );
127 public function add( $key,
$value, $exptime = 0, $flags = 0 ) {
129 if ( $this->
get( $key ) ===
false ) {
130 return $this->
set( $key,
$value, $exptime, $flags );
136 public function delete( $key, $flags = 0 ) {
139 'method' =>
'DELETE',
140 'url' => $this->url . rawurlencode( $key ),
142 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run(
$req );
143 if ( in_array( $rcode, [ 200, 204, 205, 404, 410 ] ) ) {
146 return $this->
handleError(
"Failed to delete $key", $rcode, $rerr );
153 $n = max( $n + intval(
$value ), 0 );
155 return $this->
set( $key, $n ) ? $n :
false;
169 $this->logger->error(
"$msg : ({code}) {error}", [
173 $this->
setLastError( $rcode === 0 ? self::ERR_UNREACHABLE : self::ERR_UNEXPECTED );