/**
* Increase stored value of $key by $value while preserving its TTL
* @param string $key Key to increase
- * @param int $value Value to add to $key (Default 1)
+ * @param int $value Value to add to $key (default: 1) [optional]
* @return int|bool New value or false on failure
*/
- public function incr( $key, $value = 1 ) {
- if ( !$this->lock( $key, 1 ) ) {
- return false;
- }
- $n = $this->get( $key, self::READ_LATEST );
- if ( $this->isInteger( $n ) ) { // key exists?
- $n += intval( $value );
- $this->set( $key, max( 0, $n ) ); // exptime?
- } else {
- $n = false;
- }
- $this->unlock( $key );
-
- return $n;
- }
+ abstract public function incr( $key, $value = 1 );
/**
* Decrease stored value of $key by $value while preserving its TTL
* @param string $key
- * @param int $value
+ * @param int $value Value to subtract from $key (default: 1) [optional]
* @return int|bool New value or false on failure
*/
public function decr( $key, $value = 1 ) {
return false; // key already set
}
+ public function incr( $key, $value = 1 ) {
+ $n = $this->backend->incr( $key, $value );
+ parent::delete( $key );
+
+ return $n;
+ }
+
public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
return $this->backend->lock( $key, $timeout, $expiry, $rclass );
}
return true;
}
+ public function incr( $key, $value = 1 ) {
+ return false;
+ }
+
public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
return true; // faster
}
return true;
}
+ public function incr( $key, $value = 1 ) {
+ $n = $this->get( $key );
+ if ( $this->isInteger( $n ) ) {
+ $n = max( $n + intval( $value ), 0 );
+ $this->bag[$key][self::KEY_VAL] = $n;
+
+ return $n;
+ }
+
+ return false;
+ }
+
public function clear() {
$this->bag = [];
}
$this->fixExpiry( $exptime ) );
}
+ public function incr( $key, $value = 1 ) {
+ $n = $this->client->incr( $this->validateKeyEncoding( $key ), $value );
+
+ return ( $n !== false && $n !== null ) ? $n : false;
+ }
+
+ public function decr( $key, $value = 1 ) {
+ $n = $this->client->decr( $this->validateKeyEncoding( $key ), $value );
+
+ return ( $n !== false && $n !== null ) ? $n : false;
+ }
+
public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
}
return $this->client->get_multi( $keys );
}
-
- public function incr( $key, $value = 1 ) {
- $this->validateKeyEncoding( $key );
-
- return $this->client->incr( $key, $value ) ?? false;
- }
-
- public function decr( $key, $value = 1 ) {
- $this->validateKeyEncoding( $key );
-
- return $this->client->decr( $key, $value ) ?? false;
- }
}
public function set( $key, $value, $exptime = 0, $flags = 0 ) {
// @TODO: respect WRITE_SYNC (e.g. EACH_QUORUM)
+ // @TODO: respect $exptime
$req = [
'method' => 'PUT',
'url' => $this->url . rawurlencode( $key ),
}
public function add( $key, $value, $exptime = 0, $flags = 0 ) {
+ // @TODO: make this atomic
if ( $this->get( $key ) === false ) {
return $this->set( $key, $value, $exptime, $flags );
}
}
return $this->handleError( "Failed to delete $key", $rcode, $rerr );
}
+
+ public function incr( $key, $value = 1 ) {
+ // @TODO: make this atomic
+ $n = $this->get( $key, self::READ_LATEST );
+ if ( $this->isInteger( $n ) ) { // key exists?
+ $n = max( $n + intval( $value ), 0 );
+ // @TODO: respect $exptime
+ return $this->set( $key, $n ) ? $n : false;
+ }
+
+ return false;
+ }
}