Files
kiri-core/kiri-engine/NoSql/Redis.php
T

229 lines
4.7 KiB
PHP
Raw Normal View History

2022-02-24 15:24:29 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/27 0027
* Time: 11:00
*/
declare(strict_types=1);
2025-12-18 22:53:28 +08:00
namespace Kiri\NoSql;
2022-02-24 15:24:29 +08:00
use Kiri;
2023-04-11 13:42:33 +08:00
use Kiri\Exception\RedisConnectException;
2022-02-24 15:24:29 +08:00
use Kiri\Pool\Pool;
2023-12-12 10:56:44 +08:00
use function config;
2022-02-24 15:24:29 +08:00
/**
2025-12-16 20:20:09 +08:00
* Class NoSql
2022-02-24 15:24:29 +08:00
* @package Kiri\Cache
* @mixin \Redis
*/
2023-05-26 11:16:45 +08:00
class Redis
2022-02-24 15:24:29 +08:00
{
2023-12-21 14:03:24 +08:00
public string $host = '';
public int $port = 6379;
public string $prefix = '';
public string $auth = '';
public int $databases = 0;
public int $timeout = 30;
2022-02-24 15:24:29 +08:00
2023-05-26 10:30:23 +08:00
/**
* @var int
*/
public int $read_timeout = -1;
2023-04-16 00:59:31 +08:00
2023-05-26 10:30:23 +08:00
/**
* @var array|int[]
*/
public array $pool = ['min' => 1, 'max' => 100];
2022-02-24 15:24:29 +08:00
2023-05-26 11:16:45 +08:00
/**
* 初始化
*/
public function __construct()
{
2023-12-12 10:56:44 +08:00
Kiri::configure($this, config('redis', []));
2023-05-26 11:16:45 +08:00
}
2023-05-26 10:30:23 +08:00
/**
* @return void
2023-12-12 15:35:38 +08:00
* @throws
2023-05-26 10:30:23 +08:00
*/
public function init(): void
{
}
/**
* @param $name
* @param $arguments
* @return mixed
* @throws
*/
public function __call($name, $arguments): mixed
{
if (method_exists($this, $name)) {
2023-11-23 16:33:06 +08:00
return $this->{$name}(...$arguments);
2023-05-26 10:30:23 +08:00
} else {
2023-11-23 16:33:06 +08:00
return $this->proxy($name, $arguments);
2023-05-26 10:30:23 +08:00
}
}
/**
* @param $key
* @param int $timeout
* @return bool
2023-12-12 15:35:38 +08:00
* @throws
2023-05-26 10:30:23 +08:00
*/
public function waite($key, int $timeout = 5): bool
{
$time = time();
while (!$this->setNx($key, '1')) {
if (time() - $time >= $timeout) {
return FALSE;
}
usleep(1000);
}
$this->expire($key, $timeout);
return TRUE;
}
/**
* @param $key
* @param int $timeout
* @return bool|int
2023-12-12 15:35:38 +08:00
* @throws
2023-05-26 10:30:23 +08:00
*/
public function lock($key, int $timeout = 5): bool|int
{
$script = <<<SCRIPT
2025-12-31 00:19:31 +08:00
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;
2023-05-26 10:30:23 +08:00
return $this->eval($script, ['{lock}:' . $key, $timeout], 1);
}
/**
* @param $key
* @return int
2023-12-12 15:35:38 +08:00
* @throws
2023-05-26 10:30:23 +08:00
*/
public function unlock($key): int
{
return $this->del('{lock}:' . $key);
}
/**
* @return void
* @throws
*/
public function destroy(): void
{
2024-05-01 02:06:14 +08:00
$this->pool()->close($this->getName());
2023-05-26 10:30:23 +08:00
}
/**
2026-06-24 20:11:12 +08:00
* 代理 Redis 方法调用,内置健康检查和连接回收
* 如果连接 ping 失败则关闭连接并移除,不归还池中防止污染
2023-05-26 10:30:23 +08:00
* @param $name
* @param $arguments
* @return mixed
2023-08-16 00:16:16 +08:00
* @throws
2023-05-26 10:30:23 +08:00
*/
public function proxy($name, $arguments): mixed
{
$client = $this->getClient();
try {
2023-11-23 16:33:06 +08:00
return $client->{$name}(...$arguments);
2023-05-26 10:30:23 +08:00
} catch (\Throwable $throwable) {
2025-12-31 00:19:31 +08:00
return $this->getLogger()->json_log($throwable, [], false);
2023-11-23 16:33:06 +08:00
} finally {
2023-11-25 17:46:47 +08:00
if ($client->ping('h') == 'h') {
2024-05-01 02:06:14 +08:00
$this->pool()->push($this->getName(), $client);
2026-06-24 20:11:12 +08:00
} else {
$client->close();
$this->pool()->abandon($this->getName());
2023-11-25 17:46:47 +08:00
}
2023-05-26 10:30:23 +08:00
}
}
2025-12-31 00:19:31 +08:00
/**
* @return Kiri\Error\StdoutLogger
*/
protected function getLogger(): Kiri\Error\StdoutLogger
{
return Kiri::getLogger();
}
2023-05-26 10:30:23 +08:00
/**
* @return \Redis
2023-12-12 15:35:38 +08:00
* @throws
2023-05-26 10:30:23 +08:00
*/
private function getClient(): \Redis
{
2024-05-01 02:06:14 +08:00
return $this->pool()->get($this->getName());
2023-05-26 10:30:23 +08:00
}
/**
* @return Pool
2023-12-12 15:35:38 +08:00
* @throws
2023-05-26 10:30:23 +08:00
*/
protected function pool(): Pool
{
$pool = Kiri::getPool();
2024-05-01 02:06:14 +08:00
if (!$pool->hasChannel($this->getName())) {
$pool->created($this->getName(), $this->pool['max'], [$this, 'connect']);
2023-05-26 10:30:23 +08:00
}
return $pool;
}
2024-05-01 02:06:14 +08:00
/**
* @return string
*/
private function getName(): string
{
return 'redis.' . $this->host;
}
2023-05-26 10:30:23 +08:00
/**
2023-08-16 00:16:16 +08:00
* @return \Redis
2023-12-12 15:35:38 +08:00
* @throws
2023-05-26 10:30:23 +08:00
*/
2023-08-16 00:16:16 +08:00
protected function connect(): \Redis
2023-05-26 10:30:23 +08:00
{
2023-08-16 00:16:16 +08:00
$redis = new \Redis();
if (!$redis->connect($this->host, $this->port, $this->timeout)) {
2025-12-16 20:20:09 +08:00
throw new RedisConnectException(sprintf('The NoSql Connect %s::%d Fail.', $this->host, $this->port));
2023-08-16 00:16:16 +08:00
}
if (!empty($this->auth) && !$redis->auth($this->auth)) {
2025-12-16 20:20:09 +08:00
throw new RedisConnectException(sprintf('NoSql Error: %s, Host %s, Auth %s', $redis->getLastError(), $this->host, $this->auth));
2023-08-16 00:16:16 +08:00
}
$redis->select($this->databases);
if ($this->read_timeout > 0) {
$redis->setOption(\Redis::OPT_READ_TIMEOUT, $this->read_timeout);
}
if (!empty($this->prefix)) {
$redis->setOption(\Redis::OPT_PREFIX, $this->prefix);
}
return $redis;
2023-05-26 10:30:23 +08:00
}
2022-02-24 15:24:29 +08:00
}