From fac5e12fdb00b4d2b4114af14379fa34c5e34cf5 Mon Sep 17 00:00:00 2001 From: Dominik Szamburski Date: Sun, 11 Feb 2024 10:08:09 +0100 Subject: [PATCH 1/2] feat: implement APCu Cache Pool Adapter --- composer.json | 1 + src/Adapter/APCu/APCuAdapter.php | 93 ++++++++++++++++++++++ src/Adapter/APCu/Tests/APCuAdapterTest.php | 44 ++++++++++ src/Exception/CacheException.php | 8 ++ 4 files changed, 146 insertions(+) create mode 100644 src/Adapter/APCu/APCuAdapter.php create mode 100644 src/Adapter/APCu/Tests/APCuAdapterTest.php create mode 100644 src/Exception/CacheException.php diff --git a/composer.json b/composer.json index a2d20c4..25bc3a7 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,7 @@ "psr/simple-cache-implementation": "1.0" }, "require": { + "ext-apcu": "*", "php": "~8.1.0 || ~8.2.0 || ~8.3.0", "psr/cache": "dev-master", "psr/simple-cache": "dev-master" diff --git a/src/Adapter/APCu/APCuAdapter.php b/src/Adapter/APCu/APCuAdapter.php new file mode 100644 index 0000000..6a75cc9 --- /dev/null +++ b/src/Adapter/APCu/APCuAdapter.php @@ -0,0 +1,93 @@ + $item->get(), + 'ttl' => $item->getExpirationTime() + ]; + + return \apcu_store( + $item->getKey(), + \serialize($entry), + (int) $item->getExpirationTime() + ); + } + + /** + * @inheritDoc + */ + protected function doFetch(array $keys): iterable + { + $ok = false; + $values = []; + + $results = \apcu_fetch($keys, $ok); + + if (!\is_array($results)) { + $results = [$results]; + } + + foreach ($results as $key => $result) { + if ($ok && $result !== null) { + $entry = \unserialize($result); + $values[$key] = $entry['data']; + } + } + + return $values; + + } + + /** + * @inheritDoc + */ + protected function doDelete(array $keys): bool + { + return (bool) \apcu_delete($keys); + } + + /** + * @inheritDoc + */ + protected function doHave(string $key): bool + { + return \apcu_exists($key); + } + + /** + * @inheritDoc + */ + protected function doClear(): bool + { + return \apcu_clear_cache(); + } +} diff --git a/src/Adapter/APCu/Tests/APCuAdapterTest.php b/src/Adapter/APCu/Tests/APCuAdapterTest.php new file mode 100644 index 0000000..6f08c04 --- /dev/null +++ b/src/Adapter/APCu/Tests/APCuAdapterTest.php @@ -0,0 +1,44 @@ +createMock(APCuAdapter::class); + $mock->method('get') + ->willReturn(new CacheItem('foo', true, 'bar')); + + $mock->set('foo', 'bar'); + + self::assertEquals( + 'bar', + $mock->get('foo')->get() + ); + } +} diff --git a/src/Exception/CacheException.php b/src/Exception/CacheException.php new file mode 100644 index 0000000..485931b --- /dev/null +++ b/src/Exception/CacheException.php @@ -0,0 +1,8 @@ + Date: Sun, 11 Feb 2024 10:10:00 +0100 Subject: [PATCH 2/2] style: fix coding standard --- src/Adapter/APCu/APCuAdapter.php | 16 +++++----------- src/Exception/CacheException.php | 1 - 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/Adapter/APCu/APCuAdapter.php b/src/Adapter/APCu/APCuAdapter.php index 6a75cc9..145b254 100644 --- a/src/Adapter/APCu/APCuAdapter.php +++ b/src/Adapter/APCu/APCuAdapter.php @@ -28,8 +28,7 @@ public function __construct() { /** * @inheritDoc */ - protected function doSave(CacheItem $item): array|bool - { + protected function doSave(CacheItem $item): array|bool { $entry = [ 'data' => $item->get(), 'ttl' => $item->getExpirationTime() @@ -45,8 +44,7 @@ protected function doSave(CacheItem $item): array|bool /** * @inheritDoc */ - protected function doFetch(array $keys): iterable - { + protected function doFetch(array $keys): iterable { $ok = false; $values = []; @@ -64,30 +62,26 @@ protected function doFetch(array $keys): iterable } return $values; - } /** * @inheritDoc */ - protected function doDelete(array $keys): bool - { + protected function doDelete(array $keys): bool { return (bool) \apcu_delete($keys); } /** * @inheritDoc */ - protected function doHave(string $key): bool - { + protected function doHave(string $key): bool { return \apcu_exists($key); } /** * @inheritDoc */ - protected function doClear(): bool - { + protected function doClear(): bool { return \apcu_clear_cache(); } } diff --git a/src/Exception/CacheException.php b/src/Exception/CacheException.php index 485931b..a3cdd8e 100644 --- a/src/Exception/CacheException.php +++ b/src/Exception/CacheException.php @@ -4,5 +4,4 @@ class CacheException extends \RuntimeException { - }