Skip to content

Commit

Permalink
Create serializable RedisAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym committed Apr 19, 2023
1 parent 65625a3 commit 9de40ee
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Psr6RedisModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Ray\PsrCacheModule;

use LogicException;
use Psr\Cache\CacheItemPoolInterface;
use Ray\Di\AbstractModule;
use Ray\Di\Scope;
Expand All @@ -12,8 +13,9 @@
use Ray\PsrCacheModule\Annotation\RedisConfig;
use Ray\PsrCacheModule\Annotation\RedisInstance;
use Ray\PsrCacheModule\Annotation\Shared;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Redis;

use function class_exists;
use function explode;

final class Psr6RedisModule extends AbstractModule
Expand All @@ -30,6 +32,11 @@ public function __construct(string $server, ?AbstractModule $module = null)

protected function configure(): void
{
if (! class_exists(Redis::class)) {
throw new LogicException('Redis not installed.');
}

$this->bind(Redis::class);
$this->bind(CacheItemPoolInterface::class)->annotatedWith(Local::class)->toConstructor(ApcuAdapter::class, ['namespace' => CacheNamespace::class])->in(Scope::SINGLETON);
$this->bind(CacheItemPoolInterface::class)->annotatedWith(Shared::class)->toConstructor(RedisAdapter::class, [
'redis' => RedisInstance::class,
Expand Down
35 changes: 35 additions & 0 deletions src/RedisAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Ray\PsrCacheModule;

use Ray\Di\Di\Named;
use Ray\PsrCacheModule\Annotation\CacheNamespace;
use Redis;
use Serializable;
use Symfony\Component\Cache\Adapter\RedisAdapter as OriginAdapter;
use Symfony\Component\Cache\Marshaller\MarshallerInterface;

use function func_get_args;

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

/**
* @param Redis $redis
*
* @CacheNamespace("namespace")
* @Named("redis=Ray\PsrCacheModule\Annotation\RedisInstance")
*/
#[CacheNamespace('namespace')]
#[Named('redis=Ray\PsrCacheModule\Annotation\RedisInstance')]
public function __construct($redis, string $namespace = '', int $defaultLifetime = 0, ?MarshallerInterface $marshaller = null)
{
$this->args = func_get_args();

parent::__construct($redis, $namespace, $defaultLifetime, $marshaller);
}
}
2 changes: 1 addition & 1 deletion tests/Psr6CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testArrayCacheModule(): void
public function testCacheDirModule(): void
{
$module = new CacheDirModule('/tmp');
$cacheDir = (new Injector($module))->getInstance('', CacheDir::class);
$cacheDir = (new Injector($module))->getInstance('', CacheDir::class); // @phpstan-ignore-line
$this->assertSame('/tmp', $cacheDir);
}

Expand Down
47 changes: 47 additions & 0 deletions tests/RedisAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Ray\PsrCacheModule;

use PHPUnit\Framework\TestCase;
use Ray\Di\AbstractModule;
use Ray\Di\Injector;
use Redis;
use Symfony\Component\Cache\Adapter\AbstractAdapter;

use function serialize;
use function unserialize;

class RedisAdapterTest extends TestCase
{
public function testSerialize(): string
{
$string = serialize(new RedisAdapter(new Redis()));
$this->assertIsString($string);

return $string;
}

/** @depends testSerialize */
public function testUnserialize(string $string): void
{
$this->assertInstanceOf(RedisAdapter::class, unserialize($string));
}

public function testCacheNamespaceModule(): void
{
$injector = new Injector(new class extends AbstractModule{
protected function configure(): void
{
$this->install(new CacheNamespaceModule('a'));
$this->install(new CacheDirModule('/tmp/a'));
$this->bind(AbstractAdapter::class)->to(RedisAdapter::class);
$this->bind(Redis::class);
$this->install(new Psr6RedisModule('127.0.0.1:6379:1'));
}
});
$adapter = $injector->getInstance(AbstractAdapter::class);
$this->assertInstanceOf(RedisAdapter::class, $adapter);
}
}

0 comments on commit 9de40ee

Please sign in to comment.