-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Memcached EtagPool module and unit test
Introduce `StorageMemcachedEtagModule` for managing Etag pools using Memcached. This commit includes a unit test to ensure proper instantiation and configuration of the CacheItemPoolInterface with Memcached.
- Loading branch information
Showing
2 changed files
with
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BEAR\QueryRepository; | ||
|
||
use BEAR\RepositoryModule\Annotation\EtagPool; | ||
use Memcached; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Ray\Di\AbstractModule; | ||
use Ray\PsrCacheModule\Annotation\CacheNamespace; | ||
use Ray\PsrCacheModule\Annotation\MemcacheConfig; | ||
use Ray\PsrCacheModule\MemcachedAdapter; | ||
use Ray\PsrCacheModule\MemcachedProvider; | ||
|
||
use function array_map; | ||
use function explode; | ||
|
||
/** | ||
* Memcached EtagPool module | ||
*/ | ||
final class StorageMemcachedEtagModule extends AbstractModule | ||
{ | ||
/** @var list<list<string>> */ | ||
private readonly array $memcacheServer; | ||
|
||
/** @param string $redisServer 'localhost:6379' {host}:{port} */ | ||
public function __construct( | ||
Check failure on line 28 in src/StorageMemcachedEtagModule.php GitHub Actions / sa / PHPStan
|
||
string $memcacheServer, | ||
AbstractModule|null $module = null, | ||
) { | ||
$this->memcacheServer = array_map(static fn ($memcacheServer) => explode(':', $memcacheServer), explode(',', $memcacheServer)); | ||
|
||
parent::__construct($module); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this->bind(CacheItemPoolInterface::class)->annotatedWith(EtagPool::class)->toConstructor(MemcachedAdapter::class, [ | ||
'namespace' => CacheNamespace::class, | ||
'clientProvider' => 'memcached', | ||
]); | ||
$this->bind()->annotatedWith(MemcacheConfig::class)->toInstance($this->memcacheServer); | ||
$this->bind(MemcachedProvider::class); | ||
$this->bind(Memcached::class)->toProvider(MemcachedProvider::class); | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BEAR\QueryRepository; | ||
|
||
use BEAR\RepositoryModule\Annotation\EtagPool; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Ray\Di\Injector; | ||
use Ray\PsrCacheModule\Annotation\Shared; | ||
use Ray\PsrCacheModule\MemcachedAdapter; | ||
|
||
class StorageMemcachedEtagModuleTest extends TestCase | ||
{ | ||
public function testNew(): void | ||
{ | ||
// @see http://php.net/manual/en/memcached.addservers.php | ||
$servers = 'mem1.domain.com:11211:33,mem2.domain.com:11211:67'; | ||
$cache = (new Injector(new StorageMemcachedEtagModule($servers)))->getInstance(CacheItemPoolInterface::class, EtagPool::class); | ||
$this->assertInstanceOf(MemcachedAdapter::class, $cache); | ||
} | ||
} |