Skip to content

Commit

Permalink
feat: remove increment/decrement functionality from storage adapters
Browse files Browse the repository at this point in the history
As per #117, the increment and decrement functionality (at least decrement) leads to issues with several backends. This commit removes that functionality at all as it should be either part of specific adapters instead (via dedicated interface) or as part of project specific code. This cache component can not guarantee increment/decrement for all adapters out there (especially for adapters which do not support this feature natively to prevent race conditions).

Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com>
  • Loading branch information
boesing committed Feb 28, 2024
1 parent 9cca821 commit b1f3dff
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 400 deletions.
18 changes: 0 additions & 18 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,6 @@
<code>$normalizedKey</code>
<code>$normalizedKey</code>
<code>$normalizedKey</code>
<code>$value</code>
<code>$value</code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code>$key</code>
Expand Down Expand Up @@ -338,17 +336,11 @@
<code>bool</code>
<code>bool</code>
<code>bool</code>
<code>int|bool</code>
<code>int|bool</code>
<code>int|bool</code>
<code>int|bool</code>
</MixedInferredReturnType>
<MixedMethodCall>
<code>setAdapter</code>
</MixedMethodCall>
<MixedReturnStatement>
<code>$newValue</code>
<code>$newValue</code>
<code><![CDATA[$this->options]]></code>
<code><![CDATA[$this->triggerException(__FUNCTION__, $args, $keys, $e)]]></code>
<code><![CDATA[$this->triggerException(__FUNCTION__, $args, $keys, $e)]]></code>
Expand Down Expand Up @@ -392,20 +384,12 @@
<NullableReturnStatement>
<code>$success</code>
</NullableReturnStatement>
<PossiblyInvalidArgument>
<code><![CDATA[$args['key']]]></code>
<code><![CDATA[$args['key']]]></code>
<code><![CDATA[$args['value']]]></code>
<code><![CDATA[$args['value']]]></code>
</PossiblyInvalidArgument>
<PossiblyUnusedMethod>
<code>getCaching</code>
<code>setCaching</code>
</PossiblyUnusedMethod>
<RedundantCastGivenDocblockType>
<code>(bool) $flag</code>
<code>(int) $value</code>
<code>(int) $value</code>
<code>(string) $key</code>
</RedundantCastGivenDocblockType>
</file>
Expand Down Expand Up @@ -672,8 +656,6 @@
<file src="src/Storage/StorageInterface.php">
<PossiblyUnusedMethod>
<code>addItem</code>
<code>decrementItem</code>
<code>incrementItem</code>
<code>replaceItem</code>
<code>touchItem</code>
</PossiblyUnusedMethod>
Expand Down
228 changes: 0 additions & 228 deletions src/Storage/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1097,234 +1097,6 @@ protected function internalRemoveItems(array &$normalizedKeys)
return $result;
}

/**
* Increment an item.
*
* @param string $key
* @param int $value
* @return int|bool The new value on success, false on failure
* @throws Exception\ExceptionInterface
* @triggers incrementItem.pre(PreEvent)
* @triggers incrementItem.post(PostEvent)
* @triggers incrementItem.exception(ExceptionEvent)
*/
public function incrementItem($key, $value)
{
if (! $this->getOptions()->getWritable()) {
return false;
}

$this->normalizeKey($key);
$args = new ArrayObject([
'key' => &$key,
'value' => &$value,
]);

try {
$eventRs = $this->triggerPre(__FUNCTION__, $args);

$result = $eventRs->stopped()
? $eventRs->last()
: $this->internalIncrementItem($args['key'], $args['value']);

return $this->triggerPost(__FUNCTION__, $args, $result);
} catch (\Exception $e) {
$result = false;
return $this->triggerException(__FUNCTION__, $args, $result, $e);
}
}

/**
* Internal method to increment an item.
*
* @param string $normalizedKey
* @param int $value
* @return int|bool The new value on success, false on failure
* @throws Exception\ExceptionInterface
*/
protected function internalIncrementItem(&$normalizedKey, &$value)
{
$success = null;
$value = (int) $value;
$get = (int) $this->internalGetItem($normalizedKey, $success);
$newValue = $get + $value;

if ($success) {
$this->internalReplaceItem($normalizedKey, $newValue);
} else {
$this->internalAddItem($normalizedKey, $newValue);
}

return $newValue;
}

/**
* Increment multiple items.
*
* @param array $keyValuePairs
* @return array Associative array of keys and new values
* @throws Exception\ExceptionInterface
* @triggers incrementItems.pre(PreEvent)
* @triggers incrementItems.post(PostEvent)
* @triggers incrementItems.exception(ExceptionEvent)
*/
public function incrementItems(array $keyValuePairs)
{
if (! $this->getOptions()->getWritable()) {
return [];
}

$this->normalizeKeyValuePairs($keyValuePairs);
$args = new ArrayObject([
'keyValuePairs' => &$keyValuePairs,
]);

try {
$eventRs = $this->triggerPre(__FUNCTION__, $args);

$result = $eventRs->stopped()
? $eventRs->last()
: $this->internalIncrementItems($args['keyValuePairs']);

return $this->triggerPost(__FUNCTION__, $args, $result);
} catch (\Exception $e) {
$result = [];
return $this->triggerException(__FUNCTION__, $args, $result, $e);
}
}

/**
* Internal method to increment multiple items.
*
* @return array Associative array of keys and new values
* @throws Exception\ExceptionInterface
*/
protected function internalIncrementItems(array &$normalizedKeyValuePairs)
{
$result = [];
foreach ($normalizedKeyValuePairs as $normalizedKey => $value) {
$newValue = $this->internalIncrementItem($normalizedKey, $value);
if ($newValue !== false) {
$result[$normalizedKey] = $newValue;
}
}
return $result;
}

/**
* Decrement an item.
*
* @param string $key
* @param int $value
* @return int|bool The new value on success, false on failure
* @throws Exception\ExceptionInterface
* @triggers decrementItem.pre(PreEvent)
* @triggers decrementItem.post(PostEvent)
* @triggers decrementItem.exception(ExceptionEvent)
*/
public function decrementItem($key, $value)
{
if (! $this->getOptions()->getWritable()) {
return false;
}

$this->normalizeKey($key);
$args = new ArrayObject([
'key' => &$key,
'value' => &$value,
]);

try {
$eventRs = $this->triggerPre(__FUNCTION__, $args);

$result = $eventRs->stopped()
? $eventRs->last()
: $this->internalDecrementItem($args['key'], $args['value']);

return $this->triggerPost(__FUNCTION__, $args, $result);
} catch (\Exception $e) {
$result = false;
return $this->triggerException(__FUNCTION__, $args, $result, $e);
}
}

/**
* Internal method to decrement an item.
*
* @param string $normalizedKey
* @param int $value
* @return int|bool The new value on success, false on failure
* @throws Exception\ExceptionInterface
*/
protected function internalDecrementItem(&$normalizedKey, &$value)
{
$success = null;
$value = (int) $value;
$get = (int) $this->internalGetItem($normalizedKey, $success);
$newValue = $get - $value;

if ($success) {
$this->internalReplaceItem($normalizedKey, $newValue);
} else {
$this->internalAddItem($normalizedKey, $newValue);
}

return $newValue;
}

/**
* Decrement multiple items.
*
* @param array $keyValuePairs
* @return array Associative array of keys and new values
* @throws Exception\ExceptionInterface
* @triggers incrementItems.pre(PreEvent)
* @triggers incrementItems.post(PostEvent)
* @triggers incrementItems.exception(ExceptionEvent)
*/
public function decrementItems(array $keyValuePairs)
{
if (! $this->getOptions()->getWritable()) {
return [];
}

$this->normalizeKeyValuePairs($keyValuePairs);
$args = new ArrayObject([
'keyValuePairs' => &$keyValuePairs,
]);

try {
$eventRs = $this->triggerPre(__FUNCTION__, $args);

$result = $eventRs->stopped()
? $eventRs->last()
: $this->internalDecrementItems($args['keyValuePairs']);

return $this->triggerPost(__FUNCTION__, $args, $result);
} catch (\Exception $e) {
$result = [];
return $this->triggerException(__FUNCTION__, $args, $result, $e);
}
}

/**
* Internal method to decrement multiple items.
*
* @return array Associative array of keys and new values
* @throws Exception\ExceptionInterface
*/
protected function internalDecrementItems(array &$normalizedKeyValuePairs)
{
$result = [];
foreach ($normalizedKeyValuePairs as $normalizedKey => $value) {
$newValue = $this->internalDecrementItem($normalizedKey, $value);
if ($newValue !== false) {
$result[$normalizedKey] = $newValue;
}
}
return $result;
}

/* status */

/**
Expand Down
36 changes: 0 additions & 36 deletions src/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,42 +159,6 @@ public function removeItem($key);
*/
public function removeItems(array $keys);

/**
* Increment an item.
*
* @param string $key
* @param int $value
* @return int|bool The new value on success, false on failure
* @throws ExceptionInterface
*/
public function incrementItem($key, $value);

/**
* Increment multiple items.
*
* @return array Associative array of keys and new values
* @throws ExceptionInterface
*/
public function incrementItems(array $keyValuePairs);

/**
* Decrement an item.
*
* @param string $key
* @param int $value
* @return int|bool The new value on success, false on failure
* @throws ExceptionInterface
*/
public function decrementItem($key, $value);

/**
* Decrement multiple items.
*
* @return array Associative array of keys and new values
* @throws ExceptionInterface
*/
public function decrementItems(array $keyValuePairs);

/* status */

/**
Expand Down
Loading

0 comments on commit b1f3dff

Please sign in to comment.