From cba5e42ca169c25dbc22b1974cb252d4d2db8ec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Tue, 22 Nov 2022 12:24:36 +0800 Subject: [PATCH] Optimized code. --- src/cache/src/Aspect/CacheAheadAspect.php | 2 +- src/cache/src/Driver/Driver.php | 6 ++++++ src/cache/src/Driver/DriverInterface.php | 2 ++ src/cache/src/Driver/RedisDriver.php | 12 +++++++----- src/cache/tests/Cases/RedisDriverTest.php | 4 ++-- src/cache/tests/Stub/ContainerStub.php | 2 +- 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/cache/src/Aspect/CacheAheadAspect.php b/src/cache/src/Aspect/CacheAheadAspect.php index 99fd9271c..20af866d7 100644 --- a/src/cache/src/Aspect/CacheAheadAspect.php +++ b/src/cache/src/Aspect/CacheAheadAspect.php @@ -47,7 +47,7 @@ class CacheAheadAspect extends AbstractAspect return $result['data']; } - if (! $driver->set($key . ':lock', '1', ['NX', 'EX' => $annotation->lockSeconds])) { + if (! $driver->getConnection()->set($key . ':lock', '1', ['NX', 'EX' => $annotation->lockSeconds])) { return $result['data']; } } diff --git a/src/cache/src/Driver/Driver.php b/src/cache/src/Driver/Driver.php index b8113991f..125c6fbad 100644 --- a/src/cache/src/Driver/Driver.php +++ b/src/cache/src/Driver/Driver.php @@ -11,6 +11,7 @@ declare(strict_types=1); */ namespace Hyperf\Cache\Driver; +use Hyperf\Cache\Exception\InvalidArgumentException; use Hyperf\Contract\PackerInterface; use Hyperf\Utils\InteractsWithTime; use Hyperf\Utils\Packer\PhpSerializerPacker; @@ -50,6 +51,11 @@ abstract class Driver implements DriverInterface $this->packer = $container->get($packerClass); } + public function getConnection(): mixed + { + throw new InvalidArgumentException('Cannot support method getConnection.'); + } + protected function getCacheKey(string $key) { return $this->prefix . $key; diff --git a/src/cache/src/Driver/DriverInterface.php b/src/cache/src/Driver/DriverInterface.php index eb52711ba..89178bf3f 100644 --- a/src/cache/src/Driver/DriverInterface.php +++ b/src/cache/src/Driver/DriverInterface.php @@ -28,4 +28,6 @@ interface DriverInterface extends CacheInterface * Clean up data of the same prefix. */ public function clearPrefix(string $prefix): bool; + + public function getConnection(): mixed; } diff --git a/src/cache/src/Driver/RedisDriver.php b/src/cache/src/Driver/RedisDriver.php index 8079a09a2..265e24491 100644 --- a/src/cache/src/Driver/RedisDriver.php +++ b/src/cache/src/Driver/RedisDriver.php @@ -12,15 +12,12 @@ declare(strict_types=1); namespace Hyperf\Cache\Driver; use Hyperf\Cache\Exception\InvalidArgumentException; +use Hyperf\Redis\Redis; use Psr\Container\ContainerInterface; -use Redis; class RedisDriver extends Driver implements KeyCollectorInterface { - /** - * @var Redis - */ - protected $redis; + protected Redis $redis; public function __construct(ContainerInterface $container, array $config) { @@ -154,4 +151,9 @@ class RedisDriver extends Driver implements KeyCollectorInterface { return (bool) $this->redis->sRem($this->getCacheKey($collector), ...$key); } + + public function getConnection(): mixed + { + return $this->redis; + } } diff --git a/src/cache/tests/Cases/RedisDriverTest.php b/src/cache/tests/Cases/RedisDriverTest.php index 70ecbe4b7..7f726657e 100644 --- a/src/cache/tests/Cases/RedisDriverTest.php +++ b/src/cache/tests/Cases/RedisDriverTest.php @@ -85,7 +85,7 @@ class RedisDriverTest extends TestCase $this->assertTrue($bool); $this->assertSame('yyy', $result); - $redis = $container->get(\Redis::class); + $redis = $container->get(Redis::class); $this->assertSame(1, $redis->ttl('c:xxx')); $dv = new DateInterval('PT5S'); @@ -223,7 +223,7 @@ class RedisDriverTest extends TestCase }); $poolFactory = new PoolFactory($container); - $container->shouldReceive('get')->with(\Redis::class)->andReturn(new Redis($poolFactory)); + $container->shouldReceive('get')->with(Redis::class)->andReturn(new Redis($poolFactory)); $container->shouldReceive('make')->with(RedisProxy::class, Mockery::any())->andReturnUsing(function ($_, $args) use ($poolFactory) { return new RedisProxy($poolFactory, $args['pool']); diff --git a/src/cache/tests/Stub/ContainerStub.php b/src/cache/tests/Stub/ContainerStub.php index fa45dee33..880d29666 100644 --- a/src/cache/tests/Stub/ContainerStub.php +++ b/src/cache/tests/Stub/ContainerStub.php @@ -111,7 +111,7 @@ class ContainerStub }); $poolFactory = new PoolFactory($container); - $container->shouldReceive('get')->with(\Redis::class)->andReturn(new Redis($poolFactory)); + $container->shouldReceive('get')->with(Redis::class)->andReturn(new Redis($poolFactory)); $container->shouldReceive('make')->with(RedisProxy::class, Mockery::any())->andReturnUsing(function ($_, $args) use ($poolFactory) { return new RedisProxy($poolFactory, $args['pool']);