mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-30 02:37:58 +08:00
Optimized code.
This commit is contained in:
parent
73489e401f
commit
cba5e42ca1
2
src/cache/src/Aspect/CacheAheadAspect.php
vendored
2
src/cache/src/Aspect/CacheAheadAspect.php
vendored
@ -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'];
|
||||
}
|
||||
}
|
||||
|
6
src/cache/src/Driver/Driver.php
vendored
6
src/cache/src/Driver/Driver.php
vendored
@ -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;
|
||||
|
2
src/cache/src/Driver/DriverInterface.php
vendored
2
src/cache/src/Driver/DriverInterface.php
vendored
@ -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;
|
||||
}
|
||||
|
12
src/cache/src/Driver/RedisDriver.php
vendored
12
src/cache/src/Driver/RedisDriver.php
vendored
@ -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;
|
||||
}
|
||||
}
|
||||
|
4
src/cache/tests/Cases/RedisDriverTest.php
vendored
4
src/cache/tests/Cases/RedisDriverTest.php
vendored
@ -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']);
|
||||
|
2
src/cache/tests/Stub/ContainerStub.php
vendored
2
src/cache/tests/Stub/ContainerStub.php
vendored
@ -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']);
|
||||
|
Loading…
Reference in New Issue
Block a user