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

Create Serializable Redis Adapter and ApcuAdapter disabled in CLI #17

Merged
merged 8 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint"/>
<exclude name="SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousTraitNaming.SuperfluousSuffix"/>
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint"/>
</rule>
<!-- Additional Rules -->
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
Expand Down
8 changes: 7 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
phpVersion="7.4"
phpVersion="8.2"
findUnusedBaselineEntry="false"
findUnusedCode="false"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MethodSignatureMismatch errorLevel="suppress" />
<UnimplementedAbstractMethod errorLevel="suppress" />
</issueHandlers>
</psalm>
6 changes: 5 additions & 1 deletion src/LocalCacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use function sys_get_temp_dir;

use const PHP_SAPI;

/**
* Provide APCu cache adapter if available, otherwise file cache adapter
*
Expand All @@ -34,8 +36,10 @@ public function __construct(string $cacheDir = '', string $namespace = '')

public function get(): AbstractAdapter
{
return ApcuAdapter::isSupported() ?
return PHP_SAPI !== 'cli' && ApcuAdapter::isSupported() ?
koriym marked this conversation as resolved.
Show resolved Hide resolved
// @codeCoverageIgnoreStart
new ApcuAdapter($this->namespace) :
// @codeCoverageIgnoreEnd
new FilesystemAdapter($this->namespace, 0, $this->cacheDir);
}
}
2 changes: 2 additions & 0 deletions src/Php73BcSerializableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ trait Php73BcSerializableTrait
{
/**
* {@inheritDoc}
*
* @psalm-suppress MethodSignatureMustProvideReturnType
*/
final public function serialize()
{
Expand Down
17 changes: 13 additions & 4 deletions src/Psr6RedisModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

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\RedisConfig;
use Ray\PsrCacheModule\Annotation\RedisInstance;
use Ray\PsrCacheModule\Annotation\Shared;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Redis;
use RuntimeException;

use function class_exists;
use function explode;

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

protected function configure(): void
{
if (! class_exists(Redis::class)) {
// @codeCoverageIgnoreStart
throw new RuntimeException('Redis not installed.');
// @codeCoverageIgnoreEnd
}

$this->bind(Redis::class);
Copy link
Member

Choose a reason for hiding this comment

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

新しく実装された RedisAdapter はシリアライズ可能にするために Providerを注入されるようになりましたが、
ここでのアンターゲット束縛は上記とは関係ないという理解であっていますかね?

Redis を束縛する場合は RedisProvider を使わないとコネクション情報などを利用できないのでこのアンターゲット束縛自体は意味がないのかなと。

Copy link
Member Author

Choose a reason for hiding this comment

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

確かにここ不要です。suggest してもらっていいでしょうか?

NaokiTsuchiya marked this conversation as resolved.
Show resolved Hide resolved
koriym marked this conversation as resolved.
Show resolved Hide resolved
$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,
'redisProvider' => 'redis',
'namespace' => CacheNamespace::class,
]);
$this->bind()->annotatedWith(RedisConfig::class)->toInstance($this->server);
$this->bind('')->annotatedWith(RedisInstance::class)->toProvider(RedisProvider::class);
$this->bind(ProviderInterface::class)->annotatedWith('redis')->to(RedisProvider::class);
}
}
36 changes: 36 additions & 0 deletions src/RedisAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Ray\PsrCacheModule;

use Ray\Di\Di\Named;
use Ray\Di\ProviderInterface;
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 ProviderInterface<Redis> $redisProvider
*
* @CacheNamespace("namespace")
* @Named("redisProvider=redis")
*/
#[CacheNamespace('namespace')]
#[Named('redisProvider=redis')]
Copy link
Member

Choose a reason for hiding this comment

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

Provider注入であれば、 #Set を使うのはどうですかね?

Redis が Provider束縛であれば使えるんですかね。

public function __construct(ProviderInterface $redisProvider, string $namespace = '', int $defaultLifetime = 0, ?MarshallerInterface $marshaller = null)
{
$this->args = func_get_args();

parent::__construct($redisProvider->get(), $namespace, $defaultLifetime, $marshaller);
}
}
71 changes: 71 additions & 0 deletions tests/RedisAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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 Symfony\Contracts\Cache\ItemInterface;

use function serialize;
use function unserialize;

class RedisAdapterTest extends TestCase
{
/** @return array{0:string, 1: RedisAdapter} */
public function testSerialize(): array
{
$provider = new RedisProvider(['127.0.0.1', '6379']);
$adapter = new RedisAdapter($provider);
$adapter->get('foo', static function (ItemInterface $item) {
return 'foobar';
});
$foo = $adapter->get('foo', static function (ItemInterface $item) {
return '_no_serve_';
});
$this->assertSame('foobar', $foo);
$string = serialize($adapter);
$this->assertIsString($string);

return [$string, $adapter];
}

/**
* @param array{0:string, 1: RedisAdapter} $adapters
*
* @depends testSerialize
*/
public function testUnserialize(array $adapters): void
{
$this->assertInstanceOf(RedisAdapter::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(RedisAdapter::class, $adapter0);
$this->assertSame('foobar', $adapter0->get('foo', static function (ItemInterface $item) {
return '_no_serve_in_serialize';
}));
}

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);
}
}
Loading