Skip to content

Commit

Permalink
Merge pull request #21 from apple-x-co/fix-memcached-bind
Browse files Browse the repository at this point in the history
Fix Memcached bind
  • Loading branch information
koriym authored May 12, 2024
2 parents e3beaa7 + 60deff0 commit 308d0e9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"autoload": {
"psr-4": {
"Ray\\PsrCacheModule\\": ["src/", "src-deprecated"]
"Ray\\PsrCacheModule\\": ["src/", "src-deprecated/"]
}
},
"autoload-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use function func_get_args;

/** @psalm-suppress PropertyNotSetInConstructor */
/** @deprecated Use \Ray\PsrCacheModule\MemcachedAdapter instead */
class MemcachdAdapter extends OriginAdapter implements Serializable
{
use SerializableTrait;
Expand Down
36 changes: 36 additions & 0 deletions src/MemcachedAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Ray\PsrCacheModule;

use Memcached;
use Ray\Di\Di\Named;
use Ray\Di\ProviderInterface;
use Ray\PsrCacheModule\Annotation\CacheNamespace;
use Serializable;
use Symfony\Component\Cache\Adapter\MemcachedAdapter as OriginAdapter;
use Symfony\Component\Cache\Marshaller\MarshallerInterface;

use function func_get_args;

/** @psalm-suppress PropertyNotSetInConstructor */
class MemcachedAdapter extends OriginAdapter implements Serializable
{
use SerializableTrait;

/**
* @param ProviderInterface<Memcached> $clientProvider
*
* @Named("memcached")
* @CacheNamespace("namespace")
*/
#[CacheNamespace('namespace')]
#[Named('memcached')]
public function __construct(ProviderInterface $clientProvider, string $namespace = '', int $defaultLifetime = 0, ?MarshallerInterface $marshaller = null)
{
$this->args = func_get_args();

parent::__construct($clientProvider->get(), $namespace, $defaultLifetime, $marshaller);
}
}
14 changes: 7 additions & 7 deletions tests/MemcachedAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

class MemcachedAdapterTest extends TestCase
{
/** @return array{0:string, 1: MemcachdAdapter} */
/** @return array{0:string, 1: MemcachedAdapter} */
public function testSerialize(): array
{
$provider = new MemcachedProvider([['127.0.0.1', '11211']]);
$adapter = new MemcachdAdapter($provider);
$adapter = new MemcachedAdapter($provider);
$adapter->get('foo', static function (ItemInterface $item) {
return 'foobar';
});
Expand All @@ -34,19 +34,19 @@ public function testSerialize(): array
}

/**
* @param array{0:string, 1: MemcachdAdapter} $adapters
* @param array{0:string, 1: MemcachedAdapter} $adapters
*
* @depends testSerialize
*/
public function testUnserialize(array $adapters): void
{
$this->assertInstanceOf(MemcachdAdapter::class, $adapters[1]);
$this->assertInstanceOf(MemcachedAdapter::class, $adapters[1]);
$this->assertSame('foobar', $adapters[1]->get('foo', static function (ItemInterface $item) {
return '_no_serve_in_object';
}));

$adapter0 = unserialize($adapters[0]);
$this->assertInstanceOf(MemcachdAdapter::class, $adapter0);
$this->assertInstanceOf(MemcachedAdapter::class, $adapter0);
$this->assertSame('foobar', $adapter0->get('foo', static function (ItemInterface $item) {
return '_no_serve_in_serialize';
}));
Expand All @@ -59,11 +59,11 @@ protected function configure(): void
{
$this->install(new CacheNamespaceModule('a'));
$this->install(new CacheDirModule('/tmp/a'));
$this->bind(AbstractAdapter::class)->to(MemcachdAdapter::class);
$this->bind(AbstractAdapter::class)->to(MemcachedAdapter::class);
$this->install(new Psr6MemcachedModule('127.0.0.1:6379:1'));
}
});
$adapter = $injector->getInstance(AbstractAdapter::class);
$this->assertInstanceOf(MemcachdAdapter::class, $adapter);
$this->assertInstanceOf(MemcachedAdapter::class, $adapter);
}
}

0 comments on commit 308d0e9

Please sign in to comment.