Skip to content

Commit

Permalink
Add Memcached EtagPool module and unit test
Browse files Browse the repository at this point in the history
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
koriym committed Sep 3, 2024
1 parent 856220b commit b38898c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/StorageMemcachedEtagModule.php
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

View workflow job for this annotation

GitHub Actions / sa / PHPStan

PHPDoc tag @param references unknown parameter: $redisServer
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);
}
}
23 changes: 23 additions & 0 deletions tests-pecl-ext/StorageMemcachedEtagModuleTest.php
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);
}
}

0 comments on commit b38898c

Please sign in to comment.