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..145b254 --- /dev/null +++ b/src/Adapter/APCu/APCuAdapter.php @@ -0,0 +1,87 @@ + $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..a3cdd8e --- /dev/null +++ b/src/Exception/CacheException.php @@ -0,0 +1,7 @@ +