From c2f20af91cadcec2bded29ac348949d89226b01e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Wed, 28 Feb 2024 21:19:59 +0100 Subject: [PATCH] feat: remove increment/decrement functionality from storage adapters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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> --- psalm-baseline.xml | 42 ---- src/Storage/Adapter/AbstractAdapter.php | 228 ------------------- src/Storage/StorageInterface.php | 36 --- test/Storage/Adapter/AbstractAdapterTest.php | 118 ---------- 4 files changed, 424 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index efefcf22..8b0b88ac 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -271,18 +271,6 @@ $args $args $args - $args - $args - $args - $args - $args - $args - $args - $args - $args - $args - $args - $args @@ -308,16 +296,12 @@ $normalizedKey $normalizedKey $normalizedKey - $value - $value $key $normalizedKey $normalizedKey $normalizedKey - $normalizedKey - $normalizedKey AdapterOptions @@ -329,8 +313,6 @@ array array array - array - array bool bool bool @@ -338,17 +320,11 @@ bool bool bool - int|bool - int|bool - int|bool - int|bool setAdapter - $newValue - $newValue options]]> triggerException(__FUNCTION__, $args, $keys, $e)]]> triggerException(__FUNCTION__, $args, $keys, $e)]]> @@ -365,14 +341,6 @@ triggerException(__FUNCTION__, $args, $result, $e)]]> triggerException(__FUNCTION__, $args, $result, $e)]]> triggerException(__FUNCTION__, $args, $result, $e)]]> - triggerException(__FUNCTION__, $args, $result, $e)]]> - triggerException(__FUNCTION__, $args, $result, $e)]]> - triggerException(__FUNCTION__, $args, $result, $e)]]> - triggerException(__FUNCTION__, $args, $result, $e)]]> - triggerPost(__FUNCTION__, $args, $result)]]> - triggerPost(__FUNCTION__, $args, $result)]]> - triggerPost(__FUNCTION__, $args, $result)]]> - triggerPost(__FUNCTION__, $args, $result)]]> triggerPost(__FUNCTION__, $args, $result)]]> triggerPost(__FUNCTION__, $args, $result)]]> triggerPost(__FUNCTION__, $args, $result)]]> @@ -392,20 +360,12 @@ $success - - - - - - getCaching setCaching (bool) $flag - (int) $value - (int) $value (string) $key @@ -672,8 +632,6 @@ addItem - decrementItem - incrementItem replaceItem touchItem diff --git a/src/Storage/Adapter/AbstractAdapter.php b/src/Storage/Adapter/AbstractAdapter.php index 17b60652..88c8e75c 100644 --- a/src/Storage/Adapter/AbstractAdapter.php +++ b/src/Storage/Adapter/AbstractAdapter.php @@ -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 */ /** diff --git a/src/Storage/StorageInterface.php b/src/Storage/StorageInterface.php index 518ed855..eb01bd93 100644 --- a/src/Storage/StorageInterface.php +++ b/src/Storage/StorageInterface.php @@ -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 */ /** diff --git a/test/Storage/Adapter/AbstractAdapterTest.php b/test/Storage/Adapter/AbstractAdapterTest.php index 2228053c..c03122a7 100644 --- a/test/Storage/Adapter/AbstractAdapterTest.php +++ b/test/Storage/Adapter/AbstractAdapterTest.php @@ -389,10 +389,6 @@ public function simpleEventHandlingMethodDefinitions(): array ['touchItems', 'internalTouchItems', [['k1', 'k2']], []], ['removeItem', 'internalRemoveItem', ['k'], true], ['removeItems', 'internalRemoveItems', [['k1', 'k2']], []], - ['incrementItem', 'internalIncrementItem', ['k', 1], true], - ['incrementItems', 'internalIncrementItems', [['k1' => 1, 'k2' => 2]], []], - ['decrementItem', 'internalDecrementItem', ['k', 1], true], - ['decrementItems', 'internalDecrementItems', [['k1' => 1, 'k2' => 2]], []], ['getCapabilities', 'internalGetCapabilities', [], $capabilities], ]; } @@ -653,90 +649,6 @@ public function testRemoveItemsFail(): void self::assertSame($keys, $storage->removeItems($keys)); } - public function testIncrementItems(): void - { - $storage = $this->getMockForAbstractAdapter(['incrementItem', 'internalIncrementItem']); - - $items = [ - 'key1' => 2, - 'key2' => 2, - ]; - - // foreach item call 'internalIncrementItem' instead of 'incrementItem' - $storage->expects($this->never())->method('incrementItem'); - $storage - ->expects($this->exactly(count($items))) - ->method('internalIncrementItem') - ->with($this->stringContains('key'), $this->equalTo(2)) - ->willReturn(4); - - self::assertSame([ - 'key1' => 4, - 'key2' => 4, - ], $storage->incrementItems($items)); - } - - public function testIncrementItemsFail(): void - { - $storage = $this->getMockForAbstractAdapter(['internalIncrementItem']); - - $items = [ - 'key1' => 2, - 'key2' => 2, - ]; - - // return false to indicate that the operation failed - $storage - ->expects($this->exactly(count($items))) - ->method('internalIncrementItem') - ->with($this->stringContains('key'), $this->equalTo(2)) - ->willReturn(false); - - self::assertSame([], $storage->incrementItems($items)); - } - - public function testDecrementItems(): void - { - $storage = $this->getMockForAbstractAdapter(['decrementItem', 'internalDecrementItem']); - - $items = [ - 'key1' => 2, - 'key2' => 2, - ]; - - // foreach item call 'internalDecrementItem' instead of 'decrementItem' - $storage->expects($this->never())->method('decrementItem'); - $storage - ->expects($this->exactly(count($items))) - ->method('internalDecrementItem') - ->with($this->stringContains('key'), $this->equalTo(2)) - ->willReturn(4); - - self::assertSame([ - 'key1' => 4, - 'key2' => 4, - ], $storage->decrementItems($items)); - } - - public function testDecrementItemsFail(): void - { - $storage = $this->getMockForAbstractAdapter(['internalDecrementItem']); - - $items = [ - 'key1' => 2, - 'key2' => 2, - ]; - - // return false to indicate that the operation failed - $storage - ->expects($this->exactly(count($items))) - ->method('internalDecrementItem') - ->with($this->stringContains('key'), $this->equalTo(2)) - ->willReturn(false); - - self::assertSame([], $storage->decrementItems($items)); - } - public function testTouchItems(): void { $storage = $this->getMockForAbstractAdapter(['touchItem', 'internalTouchItem']); @@ -879,36 +791,6 @@ public function testPreEventsCanChangeArguments(): void ], [ 'keys' => ['changedKey'], ]); - - // incrementItem(s) - $this->checkPreEventCanChangeArguments('incrementItem', [ - 'key' => 'key', - 'value' => 1, - ], [ - 'key' => 'changedKey', - 'value' => 2, - ]); - - $this->checkPreEventCanChangeArguments('incrementItems', [ - 'keyValuePairs' => ['key' => 1], - ], [ - 'keyValuePairs' => ['changedKey' => 2], - ]); - - // decrementItem(s) - $this->checkPreEventCanChangeArguments('decrementItem', [ - 'key' => 'key', - 'value' => 1, - ], [ - 'key' => 'changedKey', - 'value' => 2, - ]); - - $this->checkPreEventCanChangeArguments('decrementItems', [ - 'keyValuePairs' => ['key' => 1], - ], [ - 'keyValuePairs' => ['changedKey' => 2], - ]); } protected function checkPreEventCanChangeArguments(string $method, array $args, array $expectedArgs): void