Optimized code.

This commit is contained in:
李铭昕 2022-11-22 12:24:36 +08:00
parent 73489e401f
commit cba5e42ca1
6 changed files with 19 additions and 9 deletions

View File

@ -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'];
}
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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']);

View File

@ -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']);