Files
kiri-core/System/Cache/Redis.php
T

159 lines
3.0 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/27 0027
* Time: 11:00
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
2021-08-11 01:04:57 +08:00
namespace Kiri\Cache;
2020-08-31 01:27:08 +08:00
2021-08-03 18:18:09 +08:00
use Annotation\Inject;
2020-08-31 01:27:08 +08:00
use Exception;
2021-08-03 18:18:09 +08:00
use Server\Events\OnWorkerExit;
use Server\Events\OnWorkerStop;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
use Kiri\Core\Json;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Kiri\Pool\Redis as PoolRedis;
2020-08-31 01:27:08 +08:00
/**
* Class Redis
2021-08-11 01:04:57 +08:00
* @package Kiri\Kiri\Cache
2021-08-03 18:18:09 +08:00
* @mixin \Redis
2020-08-31 01:27:08 +08:00
*/
class Redis extends Component
{
2021-08-02 16:38:50 +08:00
2020-08-31 01:27:08 +08:00
/**
2021-08-03 18:18:09 +08:00
* @var EventProvider
2020-08-31 01:27:08 +08:00
*/
2021-08-03 18:18:09 +08:00
#[Inject(EventProvider::class)]
public EventProvider $eventProvider;
2020-08-31 01:27:08 +08:00
2021-01-04 16:03:10 +08:00
/**
2021-03-08 18:52:25 +08:00
* @throws ConfigException
2021-04-01 18:35:16 +08:00
* @throws Exception
2021-01-04 16:03:10 +08:00
*/
2021-08-03 18:18:09 +08:00
public function init()
2021-01-04 16:03:10 +08:00
{
2021-08-11 01:04:57 +08:00
$connections = Kiri::getDi()->get(PoolRedis::class);
2021-01-04 16:03:10 +08:00
2021-01-04 16:21:31 +08:00
$config = $this->get_config();
2021-01-04 16:03:10 +08:00
2021-08-03 18:18:09 +08:00
$length = Config::get('connections.pool.max', 10);
$this->eventProvider->on(OnWorkerStop::class, [$this, 'destroy'], 0);
$this->eventProvider->on(OnWorkerExit::class, [$this, 'destroy'], 0);
2021-01-04 16:11:43 +08:00
2021-07-05 15:49:37 +08:00
$connections->initConnections('Redis:' . $config['host'], true, $length);
2021-01-04 16:03:10 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @param $name
* @param $arguments
* @return mixed
* @throws
*/
2020-12-17 14:09:14 +08:00
public function __call($name, $arguments): mixed
2020-08-31 01:27:08 +08:00
{
2021-06-29 12:01:06 +08:00
$time = microtime(true);
2021-06-07 11:43:01 +08:00
if (method_exists($this, $name)) {
$data = $this->{$name}(...$arguments);
2020-09-03 18:10:28 +08:00
} else {
2021-06-04 18:33:42 +08:00
$data = $this->proxy()->{$name}(...$arguments);
2021-08-03 18:18:09 +08:00
$this->release();
2020-08-31 01:27:08 +08:00
}
2021-06-29 12:01:06 +08:00
if (microtime(true) - $time >= 0.02) {
2021-06-29 14:40:12 +08:00
$this->warning('Redis:' . Json::encode([$name, $arguments]) . (microtime(true) - $time));
2021-06-29 12:01:06 +08:00
}
2020-09-03 18:10:28 +08:00
return $data;
2020-08-31 01:27:08 +08:00
}
2020-09-08 15:19:17 +08:00
/**
* @param $key
* @param int $timeout
2021-04-01 18:35:16 +08:00
* @return bool|int
2020-09-08 15:19:17 +08:00
* @throws Exception
*/
2021-06-29 12:01:06 +08:00
public function lock($key, int $timeout = 5): bool|int
2020-09-08 15:19:17 +08:00
{
2020-09-08 17:58:54 +08:00
$script = <<<SCRIPT
local _nx = redis.call('setnx',KEYS[1], ARGV[1])
if (_nx ~= 0) then
redis.call('expire',KEYS[1], ARGV[1])
return 1
end
return 0
SCRIPT;
2021-08-03 18:18:09 +08:00
return $this->eval($script, ['{lock}:' . $key, $timeout], 1);
2020-09-08 15:19:17 +08:00
}
/**
* @param $key
* @return int
* @throws Exception
*/
2021-06-07 11:43:01 +08:00
public function unlock($key): int
2020-09-08 15:19:17 +08:00
{
2021-08-03 18:18:09 +08:00
return $this->del('{lock}:' . $key);
2020-09-08 15:19:17 +08:00
}
2020-08-31 01:27:08 +08:00
/**
2021-03-08 18:52:25 +08:00
* @throws ConfigException
2021-04-01 18:35:16 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2021-06-07 11:43:01 +08:00
public function release()
2020-08-31 01:27:08 +08:00
{
2021-08-11 01:04:57 +08:00
$connections = Kiri::getDi()->get(PoolRedis::class);
2020-08-31 01:27:08 +08:00
$connections->release($this->get_config(), true);
}
/**
* 销毁连接池
2020-09-03 18:04:03 +08:00
* @throws ConfigException
2021-02-20 13:20:37 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2021-06-07 11:43:01 +08:00
public function destroy()
2020-08-31 01:27:08 +08:00
{
2021-08-11 01:04:57 +08:00
$connections = Kiri::getDi()->get(PoolRedis::class);
2021-08-17 19:01:37 +08:00
$connections->connection_clear($this->get_config(), true);
2020-08-31 01:27:08 +08:00
}
/**
* @return \Redis
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function proxy(): \Redis
2020-08-31 01:27:08 +08:00
{
2021-08-11 01:04:57 +08:00
$connections = Kiri::getDi()->get(PoolRedis::class);
2020-08-31 01:27:08 +08:00
2021-06-04 18:11:58 +08:00
$config = $this->get_config();
$client = $connections->get($config, true);
2021-08-17 19:16:07 +08:00
if (!($client instanceof Base\Redis)) {
2020-08-31 01:27:08 +08:00
throw new Exception('Redis connections more.');
}
return $client;
}
/**
* @return array
2020-08-31 13:49:40 +08:00
* @throws ConfigException
2020-08-31 01:27:08 +08:00
*/
public function get_config(): array
{
2021-06-04 18:11:58 +08:00
return Config::get('cache.redis', null, true);
2020-08-31 01:27:08 +08:00
}
}