Skip to content
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

Binding a serializable PsrCacheModule memcached adapter instead of Symfony's adapter #22

Merged
merged 5 commits into from
May 13, 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
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,11 @@
"allow-plugins": {
"bamarni/composer-bin-plugin": true
}
},
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": true
}
}
}
12 changes: 2 additions & 10 deletions src-deprecated/MemcachdAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,10 @@ class MemcachdAdapter 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)
public function __construct(MemcachedProvider $provider, string $namespace = '', int $defaultLifetime = 0, ?MarshallerInterface $marshaller = null)
{
$this->args = func_get_args();

parent::__construct($clientProvider->get(), $namespace, $defaultLifetime, $marshaller);
parent::__construct($provider->get(), $namespace, $defaultLifetime, $marshaller);
}
}
16 changes: 2 additions & 14 deletions src/MemcachedAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

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;
Expand All @@ -19,18 +15,10 @@ 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)
public function __construct(MemcachedProvider $provider, string $namespace = '', int $defaultLifetime = 0, ?MarshallerInterface $marshaller = null)
{
$this->args = func_get_args();

parent::__construct($clientProvider->get(), $namespace, $defaultLifetime, $marshaller);
parent::__construct($provider->get(), $namespace, $defaultLifetime, $marshaller);
}
}
4 changes: 1 addition & 3 deletions src/Psr6MemcachedModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
use Memcached;
use Psr\Cache\CacheItemPoolInterface;
use Ray\Di\AbstractModule;
use Ray\Di\ProviderInterface;
use Ray\Di\Scope;
use Ray\PsrCacheModule\Annotation\CacheNamespace;
use Ray\PsrCacheModule\Annotation\Local;
use Ray\PsrCacheModule\Annotation\MemcacheConfig;
use Ray\PsrCacheModule\Annotation\Shared;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;

use function array_map;
use function explode;
Expand All @@ -38,6 +36,6 @@ protected function configure(): void
$this->bind(CacheItemPoolInterface::class)->annotatedWith(Shared::class)->toConstructor(MemcachedAdapter::class, ['namespace' => CacheNamespace::class])->in(Scope::SINGLETON);
$this->bind()->annotatedWith(MemcacheConfig::class)->toInstance($this->servers);
$this->bind(Memcached::class)->toProvider(MemcachedProvider::class);
$this->bind(ProviderInterface::class)->annotatedWith('memcached')->to(MemcachedProvider::class);
$this->bind(MemcachedProvider::class);
}
}
19 changes: 18 additions & 1 deletion tests-pecl-ext/Psr6MemcacheModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,30 @@
use Ray\PsrCacheModule\CacheNamespaceModule;
use Ray\PsrCacheModule\Psr6MemcachedModule;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
use function unserialize;

class Psr6MemcacheModuleTest extends TestCase
{
public function testRedisCacheModule(): void
public function testMemcCacheModule(): CacheItemPoolInterface
{
$module = new CacheNamespaceModule('1', new Psr6MemcachedModule('localhost:11211:33,localhost:11211:66'));
$cache = (new Injector($module))->getInstance(CacheItemPoolInterface::class, Shared::class);
$this->assertInstanceOf(MemcachedAdapter::class, $cache);

return $cache;
}

/**
* @depends testMemcCacheModule
*/
public function testSerializable(CacheItemPoolInterface $cache): void
{
try {
$serializedCache = serialize($cache);
$this->assertIsString($serializedCache);
} catch (\Exception $e) {
$this->fail('Serialization of $cache failed: ' . $e->getMessage());
}
$this->assertInstanceOf(CacheItemPoolInterface::class, unserialize($serializedCache));
Comment on lines +30 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add exception handling for unserialize in the testSerializable method.

+        try {
+            $cache = unserialize($serializedCache);
+        } catch (\Exception $e) {
+            $this->fail('Deserialization of $cache failed: ' . $e->getMessage());
+        }
+        $this->assertInstanceOf(CacheItemPoolInterface::class, $cache);

Adding exception handling for the unserialize function will ensure that any errors during deserialization are caught and handled appropriately, improving the robustness of the test.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public function testSerializable(CacheItemPoolInterface $cache): void
{
try {
$serializedCache = serialize($cache);
$this->assertIsString($serializedCache);
} catch (\Exception $e) {
$this->fail('Serialization of $cache failed: ' . $e->getMessage());
}
$this->assertInstanceOf(CacheItemPoolInterface::class, unserialize($serializedCache));
public function testSerializable(CacheItemPoolInterface $cache): void
{
try {
$serializedCache = serialize($cache);
$this->assertIsString($serializedCache);
} catch (\Exception $e) {
$this->fail('Serialization of $cache failed: ' . $e->getMessage());
}
try {
$cache = unserialize($serializedCache);
} catch (\Exception $e) {
$this->fail('Deserialization of $cache failed: ' . $e->getMessage());
}
$this->assertInstanceOf(CacheItemPoolInterface::class, $cache);

}
}
Loading
Loading