-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from charonlab/feature/apcu_adapter
Implement APCu Cache Pool Adapter
- Loading branch information
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the charonlab/charon-cache. | ||
* | ||
* Copyright (C) 2024 Charon Lab Development Team | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE.md file for details. | ||
*/ | ||
|
||
namespace Charon\Cache\Adapter\APCu; | ||
|
||
use Charon\Cache\Adapter\GenericAdapter; | ||
use Charon\Cache\CacheItem; | ||
use Charon\Cache\Exception\CacheException; | ||
|
||
class APCuAdapter extends GenericAdapter | ||
{ | ||
public function __construct() { | ||
if (!\extension_loaded('apcu')) { | ||
throw new CacheException('APCu extension is not loaded'); | ||
} | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function doSave(CacheItem $item): array|bool { | ||
$entry = [ | ||
'data' => $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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the charonlab/charon-cache. | ||
* | ||
* Copyright (C) 2024 Charon Lab Development Team | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE.md file for details. | ||
*/ | ||
|
||
namespace Charon\Cache\Adapter\APCu\Tests; | ||
|
||
use Charon\Cache\Adapter\APCu\APCuAdapter; | ||
use Charon\Cache\CacheItem; | ||
use Charon\Cache\Exception\CacheException; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Group; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[Group('unit')] | ||
#[CoversClass(APCuAdapter::class)] | ||
class APCuAdapterTest extends TestCase | ||
{ | ||
public function testThrowsExceptionIfAPCuExtensionIsNotLoaded(): void { | ||
self::expectException(CacheException::class); | ||
self::expectExceptionMessage('APCu extension is not loaded'); | ||
|
||
new APCuAdapter(); | ||
} | ||
|
||
public function testSerialization(): void { | ||
$mock = $this->createMock(APCuAdapter::class); | ||
$mock->method('get') | ||
->willReturn(new CacheItem('foo', true, 'bar')); | ||
|
||
$mock->set('foo', 'bar'); | ||
|
||
self::assertEquals( | ||
'bar', | ||
$mock->get('foo')->get() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Charon\Cache\Exception; | ||
|
||
class CacheException extends \RuntimeException | ||
{ | ||
} |