This commit is contained in:
2021-06-07 11:43:01 +08:00
parent b6ac2c75ee
commit e0b3954909
+12 -11
View File
@@ -20,6 +20,7 @@ use Snowflake\Snowflake;
* Class Redis * Class Redis
* @package Snowflake\Snowflake\Cache * @package Snowflake\Snowflake\Cache
* @see \Redis * @see \Redis
*
*/ */
class Redis extends Component class Redis extends Component
{ {
@@ -35,10 +36,10 @@ class Redis extends Component
*/ */
public function init() public function init()
{ {
Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, '____destroy']); Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'destroy']);
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, '____release']); Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'release']);
Event::on(Event::SERVER_WORKER_START, [$this, '____createPool']); Event::on(Event::SERVER_WORKER_START, [$this, 'createPool']);
Event::on(Event::SERVER_TASK_START, [$this, '____createPool']); Event::on(Event::SERVER_TASK_START, [$this, 'createPool']);
} }
@@ -46,7 +47,7 @@ class Redis extends Component
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public function ____createPool() public function createPool()
{ {
$connections = Snowflake::app()->getRedisFromPool(); $connections = Snowflake::app()->getRedisFromPool();
@@ -67,8 +68,8 @@ class Redis extends Component
*/ */
public function __call($name, $arguments): mixed public function __call($name, $arguments): mixed
{ {
if (method_exists($this, '____' . $name)) { if (method_exists($this, $name)) {
$data = $this->{'____' . $name}(...$arguments); $data = $this->{$name}(...$arguments);
} else { } else {
$data = $this->proxy()->{$name}(...$arguments); $data = $this->proxy()->{$name}(...$arguments);
} }
@@ -82,7 +83,7 @@ class Redis extends Component
* @return bool|int * @return bool|int
* @throws Exception * @throws Exception
*/ */
public function ____lock($key, $timeout = 5): bool|int public function lock($key, $timeout = 5): bool|int
{ {
$script = <<<SCRIPT $script = <<<SCRIPT
local _nx = redis.call('setnx',KEYS[1], ARGV[1]) local _nx = redis.call('setnx',KEYS[1], ARGV[1])
@@ -101,7 +102,7 @@ SCRIPT;
* @return int * @return int
* @throws Exception * @throws Exception
*/ */
public function ____unlock($key): int public function unlock($key): int
{ {
$redis = $this->proxy(); $redis = $this->proxy();
return $redis->del('{lock}:' . $key); return $redis->del('{lock}:' . $key);
@@ -112,7 +113,7 @@ SCRIPT;
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public function ____release() public function release()
{ {
$connections = Snowflake::app()->getRedisFromPool(); $connections = Snowflake::app()->getRedisFromPool();
$connections->release($this->get_config(), true); $connections->release($this->get_config(), true);
@@ -123,7 +124,7 @@ SCRIPT;
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public function ____destroy() public function destroy()
{ {
$connections = Snowflake::app()->getRedisFromPool(); $connections = Snowflake::app()->getRedisFromPool();
$connections->destroy($this->get_config(), true); $connections->destroy($this->get_config(), true);