Skip to content

Fix Memcached bind #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}