3use Psr\Log\LoggerInterface;
99 $params[
'segmentationSize'] = $params[
'segmentationSize'] ?? INF;
100 if ( empty( $params[
'url'] ) ) {
101 throw new InvalidArgumentException(
'URL parameter is required' );
104 if ( empty( $params[
'client'] ) ) {
110 foreach ( [
'caBundlePath',
'proxy' ] as $key ) {
111 if ( isset( $params[$key] ) ) {
112 $clientParams[$key] = $params[$key];
117 $this->client = $params[
'client'];
120 $this->httpParams[
'writeMethod'] = $params[
'httpParams'][
'writeMethod'] ??
'PUT';
121 $this->httpParams[
'readHeaders'] = $params[
'httpParams'][
'readHeaders'] ?? [];
122 $this->httpParams[
'writeHeaders'] = $params[
'httpParams'][
'writeHeaders'] ?? [];
123 $this->httpParams[
'deleteHeaders'] = $params[
'httpParams'][
'deleteHeaders'] ?? [];
124 $this->extendedErrorBodyFields = $params[
'extendedErrorBodyFields'] ?? [];
125 $this->serializationType = $params[
'serialization_type'] ??
'legacy';
126 $this->hmacKey = $params[
'hmac_key'] ??
'';
129 parent::__construct( $params );
132 $this->url = rtrim( $params[
'url'],
'/' ) .
'/';
140 $this->client->setLogger(
$logger );
143 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
148 'url' => $this->url . rawurlencode( $key ),
149 'headers' => $this->httpParams[
'readHeaders'],
152 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
153 if ( $rcode === 200 ) {
154 if ( is_string( $rbody ) ) {
157 $casToken = ( $value !== false ) ? $rbody :
null;
163 if ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
164 return $this->
handleError(
"Failed to fetch $key", $rcode, $rerr, $rhdrs, $rbody );
169 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
173 'method' => $this->httpParams[
'writeMethod'],
174 'url' => $this->url . rawurlencode( $key ),
176 'headers' => $this->httpParams[
'writeHeaders'],
179 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
180 if ( $rcode === 200 || $rcode === 201 || $rcode === 204 ) {
183 return $this->
handleError(
"Failed to store $key", $rcode, $rerr, $rhdrs, $rbody );
186 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
188 if ( $this->
get( $key ) ===
false ) {
189 return $this->
set( $key, $value, $exptime, $flags );
198 'method' =>
'DELETE',
199 'url' => $this->url . rawurlencode( $key ),
200 'headers' => $this->httpParams[
'deleteHeaders'],
203 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
204 if ( in_array( $rcode, [ 200, 204, 205, 404, 410 ] ) ) {
207 return $this->
handleError(
"Failed to delete $key", $rcode, $rerr, $rhdrs, $rbody );
210 public function incr( $key, $value = 1, $flags = 0 ) {
214 $n = max( $n + (
int)$value, 0 );
216 return $this->
set( $key, $n ) ? $n :
false;
222 public function decr( $key, $value = 1, $flags = 0 ) {
223 return $this->
incr( $key, -$value, $flags );
233 if ( $this->serializationType ===
'legacy' ) {
236 $pieces = explode(
'.', $body, 3 );
237 if ( count( $pieces ) !== 3 || $pieces[0] !== $this->serializationType ) {
241 if ( $this->hmacKey !==
'' ) {
242 $checkHmac = hash_hmac(
'sha256',
$serialized, $this->hmacKey,
true );
243 if ( !hash_equals( $checkHmac, base64_decode( $hmac ) ) ) {
249 switch ( $this->serializationType ) {
252 return ( json_last_error() === JSON_ERROR_NONE ) ? $value :
false;
259 throw new \DomainException(
260 "Unknown serialization type: $this->serializationType"
273 switch ( $this->serializationType ) {
275 $value = json_encode( $body );
276 if ( $value ===
false ) {
277 throw new InvalidArgumentException( __METHOD__ .
": body could not be encoded." );
287 throw new \DomainException(
288 "Unknown serialization type: $this->serializationType"
292 if ( $this->serializationType !==
'legacy' ) {
293 if ( $this->hmacKey !==
'' ) {
294 $hmac = base64_encode(
295 hash_hmac(
'sha256', $value, $this->hmacKey,
true )
300 $value = $this->serializationType .
'.' . $hmac .
'.' . $value;
315 protected function handleError( $msg, $rcode, $rerr, $rhdrs, $rbody ) {
316 $message =
"$msg : ({code}) {error}";
322 if ( $this->extendedErrorBodyFields !== [] ) {
326 foreach ( $this->extendedErrorBodyFields as $field ) {
327 if ( isset( $body[$field] ) ) {
328 $extraFields .=
" : ({$field}) {$body[$field]}";
331 if ( $extraFields !==
'' ) {
332 $message .=
" {extra_fields}";
333 $context[
'extra_fields'] = $extraFields;
338 $this->logger->error( $message,
$context );
const READ_LATEST
Bitfield constants for get()/getMulti(); these are only advisory.
Storage medium specific cache for storing items (e.g.
isInteger( $value)
Check if a value is an integer.
setLastError( $err)
Set the "last error" registry.
Class to handle multiple HTTP requests.
Interface to key-value storage behind an HTTP server.
array $httpParams
http parameters: readHeaders, writeHeaders, deleteHeaders, writeMethod
string $serializationType
Optional serialization type to use.
setLogger(LoggerInterface $logger)
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
const DEFAULT_CONN_TIMEOUT
Default connection timeout in seconds.
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
decodeBody( $body)
Processes the response body.
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
const DEFAULT_REQ_TIMEOUT
Default request timeout.
doGet( $key, $flags=0, &$casToken=null)
doDelete( $key, $flags=0)
Delete an item.
array $extendedErrorBodyFields
additional body fields to log on error, if possible
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
string $hmacKey
Optional HMAC Key for protecting the serialized blob.
encodeBody( $body)
Prepares the request body (the "value" portion of our key/value store) for transmission.
handleError( $msg, $rcode, $rerr, $rhdrs, $rbody)
Handle storage error.
string $url
REST URL to use for storage.
foreach( $res as $row) $serialized