modify plugin name
This commit is contained in:
+3
-3
@@ -441,12 +441,12 @@ if (!function_exists('redis')) {
|
||||
|
||||
|
||||
/**
|
||||
* @return \Kiri\Cache\Redis|Redis
|
||||
* @return \Kiri\Redis\Redis|Redis
|
||||
* @throws Exception
|
||||
*/
|
||||
function redis(): \Kiri\Cache\Redis|Redis
|
||||
function redis(): \Kiri\Redis\Redis|Redis
|
||||
{
|
||||
return Kiri::getDi()->get(\Kiri\Cache\Redis::class);
|
||||
return Kiri::getDi()->get(\Kiri\Redis\Redis::class);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,141 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/5/2 0002
|
||||
* Time: 14:51
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Cache;
|
||||
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Swoole\Coroutine\System;
|
||||
|
||||
/**
|
||||
* Class File
|
||||
* @package Kiri\Cache
|
||||
*/
|
||||
class File extends Component implements ICache
|
||||
{
|
||||
public string $path;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $val
|
||||
* @return string|int
|
||||
*/
|
||||
public function set($key, $val): string|int
|
||||
{
|
||||
if (is_array($val) || is_object($val)) {
|
||||
$val = swoole_serialize($val);
|
||||
}
|
||||
$tmpFile = $this->getCacheKey($key);
|
||||
if (!$this->exists($tmpFile)) {
|
||||
touch($tmpFile);
|
||||
}
|
||||
return System::writeFile($tmpFile, $val, LOCK_EX);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param array $hashKeys
|
||||
* @return array|bool
|
||||
*/
|
||||
public function hMGet($key, array $hashKeys): array|bool
|
||||
{
|
||||
$hash = $this->get($key);
|
||||
if (!is_array($hash)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$nowHash = [];
|
||||
foreach ($hashKeys as $hashKey) {
|
||||
$nowHash[$hashKey] = $hash[$hashKey] ?? null;
|
||||
}
|
||||
return $nowHash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param array $val
|
||||
* @return bool|int|string
|
||||
*/
|
||||
public function hMSet($key, array $val): bool|int|string
|
||||
{
|
||||
$hash = $this->get($key);
|
||||
if (!is_array($hash)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$merge = array_merge($hash, $val);
|
||||
return $this->set($key, $merge);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $hashKey
|
||||
* @return string|int|bool
|
||||
*/
|
||||
public function hGet(string $key, string $hashKey): string|int|bool
|
||||
{
|
||||
$hash = $this->get($key);
|
||||
if (!is_array($hash)) {
|
||||
return false;
|
||||
}
|
||||
return $hash[$hashKey] ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $hashKey
|
||||
* @param $hashValue
|
||||
* @return bool|int|string
|
||||
*/
|
||||
public function hSet($key, $hashKey, $hashValue): bool|int|string
|
||||
{
|
||||
$hash = $this->get($key);
|
||||
if (!is_array($hash)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$hash[$hashKey] = $hashValue;
|
||||
|
||||
return $this->set($key, $hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return bool
|
||||
*/
|
||||
#[Pure] public function exists($key): bool
|
||||
{
|
||||
return file_exists($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return mixed|bool
|
||||
*/
|
||||
public function get($key): mixed
|
||||
{
|
||||
$tmpFile = $this->getCacheKey($key);
|
||||
if (!$this->exists($tmpFile)) {
|
||||
return false;
|
||||
}
|
||||
$content = file_get_contents($tmpFile);
|
||||
return swoole_unserialize($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
private function getCacheKey($key): string
|
||||
{
|
||||
return storage($key, 'cache');
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ use Kiri;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Context;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Swoole\Error;
|
||||
use Throwable;
|
||||
|
||||
@@ -20,7 +22,19 @@ use Throwable;
|
||||
class Connection extends Component
|
||||
{
|
||||
|
||||
use Alias;
|
||||
|
||||
private Pool $pool;
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->pool = $this->getContainer()->get(Pool::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -83,15 +97,15 @@ class Connection extends Component
|
||||
public function get(mixed $config): ?PDO
|
||||
{
|
||||
$coroutineName = $config['cds'];
|
||||
|
||||
if (($connection = Context::getContext($coroutineName)) instanceof PDO) {
|
||||
return $connection;
|
||||
}
|
||||
//
|
||||
// if (($connection = Context::getContext($coroutineName)) instanceof PDO) {
|
||||
// return $connection;
|
||||
// }
|
||||
|
||||
$minx = Config::get('databases.pool.min', 1);
|
||||
|
||||
/** @var PDO $connections */
|
||||
$connections = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $minx);
|
||||
$connections = $this->pool->get($coroutineName, $this->create($coroutineName, $config), $minx);
|
||||
if (Context::hasContext('begin_' . $coroutineName)) {
|
||||
$connections->beginTransaction();
|
||||
}
|
||||
@@ -121,7 +135,7 @@ class Connection extends Component
|
||||
*/
|
||||
public function addItem(string $name, PDO $PDO)
|
||||
{
|
||||
$this->getPool()->push($name, $PDO);
|
||||
$this->pool->push($name, $PDO);
|
||||
}
|
||||
|
||||
|
||||
@@ -132,7 +146,7 @@ class Connection extends Component
|
||||
*/
|
||||
public function initConnections($name, $max)
|
||||
{
|
||||
$this->getPool()->initConnections($name, $max);
|
||||
$this->pool->initConnections($name, $max);
|
||||
}
|
||||
|
||||
|
||||
@@ -148,7 +162,7 @@ class Connection extends Component
|
||||
return;
|
||||
}
|
||||
|
||||
$this->getPool()->push($coroutineName, $client);
|
||||
$this->pool->push($coroutineName, $client);
|
||||
Context::remove($coroutineName);
|
||||
}
|
||||
|
||||
@@ -169,7 +183,7 @@ class Connection extends Component
|
||||
*/
|
||||
public function connection_clear($name)
|
||||
{
|
||||
$this->getPool()->clean($name);
|
||||
$this->pool->clean($name);
|
||||
}
|
||||
|
||||
|
||||
@@ -202,17 +216,8 @@ class Connection extends Component
|
||||
public function disconnect($coroutineName)
|
||||
{
|
||||
Context::remove($coroutineName);
|
||||
$this->getPool()->clean($coroutineName);
|
||||
$this->pool->clean($coroutineName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Pool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPool(): Pool
|
||||
{
|
||||
return Kiri::getDi()->get(Pool::class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,11 +5,10 @@ namespace Kiri\Pool;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Context;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Context;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Pool\Helper\SplQueue;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Coroutine\Channel;
|
||||
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool\Helper;
|
||||
|
||||
interface QueueInterface
|
||||
{
|
||||
|
||||
|
||||
public function isEmpty(): bool;
|
||||
|
||||
public function push(mixed $data, float $timeout = -1): bool;
|
||||
|
||||
|
||||
public function pop(float $timeout = -1): mixed;
|
||||
|
||||
|
||||
public function stats(): array;
|
||||
|
||||
|
||||
public function close(): bool;
|
||||
|
||||
|
||||
public function length(): int;
|
||||
|
||||
|
||||
public function isFull(): bool;
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool;
|
||||
|
||||
interface QueueInterface
|
||||
{
|
||||
|
||||
|
||||
public function isEmpty(): bool;
|
||||
|
||||
public function push(mixed $data, float $timeout = -1): bool;
|
||||
|
||||
|
||||
public function pop(float $timeout = -1): mixed;
|
||||
|
||||
|
||||
public function stats(): array;
|
||||
|
||||
|
||||
public function close(): bool;
|
||||
|
||||
|
||||
public function length(): int;
|
||||
|
||||
|
||||
public function isFull(): bool;
|
||||
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace Kiri\Pool;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Context;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri;
|
||||
|
||||
/**
|
||||
* Class RedisClient
|
||||
* @package Kiri\Pool
|
||||
*/
|
||||
class Redis extends Component
|
||||
{
|
||||
|
||||
use Alias;
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $config
|
||||
* @param bool $isMaster
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(mixed $config, bool $isMaster = false): mixed
|
||||
{
|
||||
$coroutineName = $config['host'];
|
||||
if (Context::hasContext($coroutineName)) {
|
||||
return Context::getContext($coroutineName);
|
||||
}
|
||||
|
||||
$pool = $config['pool'] ?? ['min' => 1, 'max' => 100];
|
||||
|
||||
$clients = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $pool['min'] ?? 1);
|
||||
return Context::setContext($coroutineName, $clients);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $config
|
||||
* @return Closure
|
||||
*/
|
||||
public function create(string $name, mixed $config): Closure
|
||||
{
|
||||
return static function () use ($name, $config) {
|
||||
return Kiri::getDi()->create(\Kiri\Cache\Base\Redis::class, [$config]);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param bool $isMaster
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function release(array $config, bool $isMaster = false)
|
||||
{
|
||||
$coroutineName = $config['host'];
|
||||
if (!Context::hasContext($coroutineName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->getPool()->push($coroutineName, Context::getContext($coroutineName));
|
||||
Context::remove($coroutineName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param bool $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function destroy(array $config, bool $isMaster = false)
|
||||
{
|
||||
$this->getPool()->clean($config['host']);
|
||||
Context::remove($config['host']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param bool $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function connection_clear(array $config, bool $isMaster = false)
|
||||
{
|
||||
$this->getPool()->clean($config['host']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Pool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPool(): Pool
|
||||
{
|
||||
return Kiri::getDi()->get(Pool::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $isMaster
|
||||
* @param $max
|
||||
* @throws Exception
|
||||
*/
|
||||
public function initConnections($name, $isMaster, $max)
|
||||
{
|
||||
$this->getPool()->initConnections($name, $max);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,101 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool\Helper;
|
||||
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class SplQueue implements QueueInterface
|
||||
{
|
||||
|
||||
private \SplQueue $channel;
|
||||
|
||||
|
||||
public int $errCode = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $max
|
||||
*/
|
||||
#[Pure] public function __construct(public int $max)
|
||||
{
|
||||
$this->channel = new \SplQueue();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
// TODO: Implement isEmpty() method.
|
||||
return $this->channel->count() < 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param float $timeout
|
||||
* @return bool
|
||||
*/
|
||||
public function push(mixed $data, float $timeout = -1): bool
|
||||
{
|
||||
// TODO: Implement push() method.
|
||||
$this->channel->enqueue($data);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param float $timeout
|
||||
* @return mixed
|
||||
*/
|
||||
public function pop(float $timeout = -1): mixed
|
||||
{
|
||||
// TODO: Implement pop() method.
|
||||
return $this->channel->dequeue();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function stats(): array
|
||||
{
|
||||
// TODO: Implement stats() method.
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function close(): bool
|
||||
{
|
||||
// TODO: Implement close() method.
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function length(): int
|
||||
{
|
||||
// TODO: Implement length() method.
|
||||
return $this->channel->count();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isFull(): bool
|
||||
{
|
||||
// TODO: Implement isFull() method.
|
||||
return $this->channel->count() >= $this->max;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool;
|
||||
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class SplQueue implements QueueInterface
|
||||
{
|
||||
|
||||
private \SplQueue $channel;
|
||||
|
||||
|
||||
public int $errCode = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $max
|
||||
*/
|
||||
#[Pure] public function __construct(public int $max)
|
||||
{
|
||||
$this->channel = new \SplQueue();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
// TODO: Implement isEmpty() method.
|
||||
return $this->channel->count() < 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @param float $timeout
|
||||
* @return bool
|
||||
*/
|
||||
public function push(mixed $data, float $timeout = -1): bool
|
||||
{
|
||||
// TODO: Implement push() method.
|
||||
$this->channel->enqueue($data);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param float $timeout
|
||||
* @return mixed
|
||||
*/
|
||||
public function pop(float $timeout = -1): mixed
|
||||
{
|
||||
// TODO: Implement pop() method.
|
||||
return $this->channel->dequeue();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function stats(): array
|
||||
{
|
||||
// TODO: Implement stats() method.
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function close(): bool
|
||||
{
|
||||
// TODO: Implement close() method.
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function length(): int
|
||||
{
|
||||
// TODO: Implement length() method.
|
||||
return $this->channel->count();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isFull(): bool
|
||||
{
|
||||
// TODO: Implement isFull() method.
|
||||
return $this->channel->count() >= $this->max;
|
||||
}
|
||||
}
|
||||
@@ -1,65 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/11/8 0008
|
||||
* Time: 16:35
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Cache;
|
||||
|
||||
/**
|
||||
* Interface ICache
|
||||
* @package Kiri\Cache
|
||||
*/
|
||||
interface ICache
|
||||
{
|
||||
/**
|
||||
* @param $key
|
||||
* @param $val
|
||||
* @return string|int
|
||||
*/
|
||||
public function set($key, $val): string|int;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return string|int|bool
|
||||
*/
|
||||
public function get($key): mixed;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param array $hashKeys
|
||||
* @return array|bool|null
|
||||
*/
|
||||
public function hMGet($key, array $hashKeys): array|bool|null;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param array $val
|
||||
* @return mixed
|
||||
*/
|
||||
public function hMSet($key, array $val): mixed;
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $hashKey
|
||||
* @return string|int|bool
|
||||
*/
|
||||
public function hGet(string $key, string $hashKey): string|int|bool;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $hashKey
|
||||
* @param $hashValue
|
||||
* @return mixed
|
||||
*/
|
||||
public function hSet($key, $hashKey, $hashValue): mixed;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($key): bool;
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/11/8 0008
|
||||
* Time: 16:35
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Redis;
|
||||
|
||||
/**
|
||||
* Interface ICache
|
||||
* @package Kiri\Cache
|
||||
*/
|
||||
interface CacheInterface
|
||||
{
|
||||
/**
|
||||
* @param $key
|
||||
* @param $val
|
||||
* @return string|int
|
||||
*/
|
||||
public function set($key, $val): string|int;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return string|int|bool
|
||||
*/
|
||||
public function get($key): mixed;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param array $hashKeys
|
||||
* @return array|bool|null
|
||||
*/
|
||||
public function hMGet($key, array $hashKeys): array|bool|null;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param array $val
|
||||
* @return mixed
|
||||
*/
|
||||
public function hMSet($key, array $val): mixed;
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $hashKey
|
||||
* @return string|int|bool
|
||||
*/
|
||||
public function hGet(string $key, string $hashKey): string|int|bool;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $hashKey
|
||||
* @param $hashValue
|
||||
* @return mixed
|
||||
*/
|
||||
public function hSet($key, $hashKey, $hashValue): mixed;
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($key): bool;
|
||||
}
|
||||
@@ -1,186 +1,171 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Cache\Base;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\RedisConnectException;
|
||||
use Kiri;
|
||||
use Kiri\Pool\StopHeartbeatCheck;
|
||||
use Kiri\Server\Events\OnWorkerExit;
|
||||
use RedisException;
|
||||
use Swoole\Timer;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Redis implements StopHeartbeatCheck
|
||||
{
|
||||
|
||||
const DB_ERROR_MESSAGE = 'The system is busy, please try again later.';
|
||||
|
||||
|
||||
private ?\Redis $pdo = null;
|
||||
|
||||
public string $host;
|
||||
|
||||
public int $port;
|
||||
|
||||
public int $database = 0;
|
||||
|
||||
public string $auth = '';
|
||||
|
||||
public string $prefix = '';
|
||||
|
||||
public int $timeout = 30;
|
||||
|
||||
public int $read_timeout = 30;
|
||||
|
||||
public array $pool = [];
|
||||
|
||||
private int $_timer = -1;
|
||||
|
||||
private int $_last = 0;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->host = $config['host'];
|
||||
$this->port = $config['port'];
|
||||
$this->database = $config['databases'];
|
||||
$this->auth = $config['auth'];
|
||||
$this->prefix = $config['prefix'];
|
||||
$this->timeout = $config['timeout'];
|
||||
$this->read_timeout = $config['read_timeout'];
|
||||
$this->pool = $config['pool'];
|
||||
}
|
||||
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->heartbeat_check();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Kiri\Server\Events\OnWorkerExit $exit
|
||||
* @return void
|
||||
*/
|
||||
public function onWorkerExit(OnWorkerExit $exit)
|
||||
{
|
||||
$this->stopHeartbeatCheck();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function heartbeat_check(): void
|
||||
{
|
||||
|
||||
if ($this->_timer === -1) {
|
||||
$this->_timer = Timer::tick(1000, fn() => $this->waite());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private function waite(): void
|
||||
{
|
||||
try {
|
||||
if ($this->_timer === -1) {
|
||||
Kiri::getDi()->get(Logger::class)->critical('timer end');
|
||||
$this->stopHeartbeatCheck();
|
||||
}
|
||||
if (time() - $this->_last > intval($this->pool['tick'] ?? 60)) {
|
||||
$this->stopHeartbeatCheck();
|
||||
|
||||
$this->pdo = null;
|
||||
}
|
||||
} catch (\Throwable $throwable) {
|
||||
error($throwable);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function stopHeartbeatCheck(): void
|
||||
{
|
||||
if ($this->_timer > -1) {
|
||||
Timer::clear($this->_timer);
|
||||
}
|
||||
$this->_timer = -1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $arguments
|
||||
* @return mixed
|
||||
* @throws RedisConnectException|RedisException
|
||||
*/
|
||||
public function __call(string $name, array $arguments)
|
||||
{
|
||||
if (!method_exists($this, $name)) {
|
||||
return $this->_pdo()->{$name}(...$arguments);
|
||||
}
|
||||
return $this->{$name}(...$arguments);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return \Redis
|
||||
* @throws RedisConnectException
|
||||
* @throws RedisException
|
||||
*/
|
||||
public function _pdo(): \Redis
|
||||
{
|
||||
if ($this->_timer === -1) {
|
||||
$this->heartbeat_check();
|
||||
}
|
||||
$this->_last = time();
|
||||
if (!($this->pdo instanceof \Redis) || !$this->pdo->ping('isOk')) {
|
||||
$this->pdo = $this->newClient();
|
||||
}
|
||||
return $this->pdo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return \Redis
|
||||
* @throws RedisConnectException
|
||||
*/
|
||||
private function newClient(): \Redis
|
||||
{
|
||||
$redis = new \Redis();
|
||||
if (!$redis->connect($this->host, $this->port, $this->timeout)) {
|
||||
throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $this->host, $this->port));
|
||||
}
|
||||
if (!empty($this->auth) && !$redis->auth($this->auth)) {
|
||||
throw new RedisConnectException(sprintf('Redis Error: %s, Host %s, Auth %s', $redis->getLastError(), $this->host, $this->auth));
|
||||
}
|
||||
if ($this->read_timeout < 0) {
|
||||
$this->read_timeout = 0;
|
||||
}
|
||||
$redis->select($this->database);
|
||||
if ($this->read_timeout > 0) {
|
||||
$redis->setOption(\Redis::OPT_READ_TIMEOUT, $this->read_timeout);
|
||||
}
|
||||
$redis->setOption(\Redis::OPT_PREFIX, $this->prefix);
|
||||
return $redis;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace Kiri\Redis;
|
||||
|
||||
use Exception;
|
||||
use Kiri;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Exception\RedisConnectException;
|
||||
use Kiri\Pool\StopHeartbeatCheck;
|
||||
use Kiri\Server\Events\OnWorkerExit;
|
||||
use RedisException;
|
||||
use Swoole\Timer;
|
||||
use function error;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Helper implements StopHeartbeatCheck
|
||||
{
|
||||
|
||||
private ?\Redis $pdo = null;
|
||||
|
||||
public string $host;
|
||||
|
||||
public int $port;
|
||||
|
||||
public int $database = 0;
|
||||
|
||||
public string $auth = '';
|
||||
|
||||
public string $prefix = '';
|
||||
|
||||
public int $timeout = 30;
|
||||
|
||||
public int $read_timeout = 30;
|
||||
|
||||
public array $pool = [];
|
||||
|
||||
private int $_timer = -1;
|
||||
|
||||
private int $_last = 0;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->host = $config['host'];
|
||||
$this->port = $config['port'];
|
||||
$this->database = $config['databases'];
|
||||
$this->auth = $config['auth'];
|
||||
$this->prefix = $config['prefix'];
|
||||
$this->timeout = $config['timeout'];
|
||||
$this->read_timeout = $config['read_timeout'];
|
||||
$this->pool = $config['pool'];
|
||||
}
|
||||
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->heartbeat_check();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function heartbeat_check(): void
|
||||
{
|
||||
|
||||
if ($this->_timer === -1) {
|
||||
$this->_timer = Timer::tick(1000, fn() => $this->waite());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private function waite(): void
|
||||
{
|
||||
try {
|
||||
if ($this->_timer === -1) {
|
||||
Kiri::getDi()->get(Logger::class)->critical('timer end');
|
||||
$this->stopHeartbeatCheck();
|
||||
}
|
||||
if (time() - $this->_last > intval($this->pool['tick'] ?? 60)) {
|
||||
$this->stopHeartbeatCheck();
|
||||
|
||||
$this->pdo = null;
|
||||
}
|
||||
} catch (\Throwable $throwable) {
|
||||
error($throwable);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function stopHeartbeatCheck(): void
|
||||
{
|
||||
if ($this->_timer > -1) {
|
||||
Timer::clear($this->_timer);
|
||||
}
|
||||
$this->_timer = -1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $arguments
|
||||
* @return mixed
|
||||
* @throws RedisConnectException|RedisException
|
||||
*/
|
||||
public function __call(string $name, array $arguments)
|
||||
{
|
||||
if (!method_exists($this, $name)) {
|
||||
return $this->_pdo()->{$name}(...$arguments);
|
||||
}
|
||||
return $this->{$name}(...$arguments);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return \Redis
|
||||
* @throws RedisConnectException
|
||||
* @throws RedisException
|
||||
*/
|
||||
public function _pdo(): \Redis
|
||||
{
|
||||
if ($this->_timer === -1) {
|
||||
$this->heartbeat_check();
|
||||
}
|
||||
$this->_last = time();
|
||||
if (!($this->pdo instanceof \Redis) || !$this->pdo->ping('isOk')) {
|
||||
$this->pdo = $this->newClient();
|
||||
}
|
||||
return $this->pdo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return \Redis
|
||||
* @throws RedisConnectException
|
||||
*/
|
||||
private function newClient(): \Redis
|
||||
{
|
||||
$redis = new \Redis();
|
||||
if (!$redis->connect($this->host, $this->port, $this->timeout)) {
|
||||
throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $this->host, $this->port));
|
||||
}
|
||||
if (!empty($this->auth) && !$redis->auth($this->auth)) {
|
||||
throw new RedisConnectException(sprintf('Redis Error: %s, Host %s, Auth %s', $redis->getLastError(), $this->host, $this->auth));
|
||||
}
|
||||
if ($this->read_timeout < 0) {
|
||||
$this->read_timeout = 0;
|
||||
}
|
||||
$redis->select($this->database);
|
||||
if ($this->read_timeout > 0) {
|
||||
$redis->setOption(\Redis::OPT_READ_TIMEOUT, $this->read_timeout);
|
||||
}
|
||||
$redis->setOption(\Redis::OPT_PREFIX, $this->prefix);
|
||||
return $redis;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,191 +1,201 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/27 0027
|
||||
* Time: 11:00
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Cache;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Core\Json;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri;
|
||||
use Kiri\Pool\Redis as PoolRedis;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Server\Events\OnWorkerExit;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Swoole\Timer;
|
||||
|
||||
/**
|
||||
* Class Redis
|
||||
* @package Kiri\Cache
|
||||
* @mixin \Redis
|
||||
*/
|
||||
class Redis extends Component
|
||||
{
|
||||
|
||||
|
||||
const REDIS_OPTION_HOST = 'host';
|
||||
const REDIS_OPTION_PORT = 'port';
|
||||
const REDIS_OPTION_PREFIX = 'prefix';
|
||||
const REDIS_OPTION_AUTH = 'auth';
|
||||
const REDIS_OPTION_DATABASES = 'databases';
|
||||
const REDIS_OPTION_TIMEOUT = 'timeout';
|
||||
const REDIS_OPTION_POOL = 'pool';
|
||||
const REDIS_OPTION_POOL_TICK = 'tick';
|
||||
const REDIS_OPTION_POOL_MIN = 'min';
|
||||
const REDIS_OPTION_POOL_MAX = 'max';
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ConfigException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$connections = Kiri::getDi()->get(PoolRedis::class);
|
||||
|
||||
$config = $this->get_config();
|
||||
|
||||
$length = Config::get('cache.redis.pool.max', 10);
|
||||
|
||||
$this->getEventProvider()->on(OnWorkerExit::class, [$this, 'destroy'], 0);
|
||||
|
||||
$connections->initConnections('Redis:' . $config['host'], true, $length);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $arguments
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public function __call($name, $arguments): mixed
|
||||
{
|
||||
$time = microtime(true);
|
||||
if (method_exists($this, $name)) {
|
||||
$data = $this->{$name}(...$arguments);
|
||||
} else {
|
||||
$data = $this->proxy($name, $arguments);
|
||||
}
|
||||
if (microtime(true) - $time >= 0.02) {
|
||||
$this->logger->warning('Redis:' . Json::encode([$name, $arguments]) . (microtime(true) - $time));
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param int $timeout
|
||||
* @return bool
|
||||
*/
|
||||
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
|
||||
* @throws Exception
|
||||
*/
|
||||
public function lock($key, int $timeout = 5): bool|int
|
||||
{
|
||||
$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;
|
||||
return $this->eval($script, ['{lock}:' . $key, $timeout], 1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return int
|
||||
* @throws Exception
|
||||
*/
|
||||
public function unlock($key): int
|
||||
{
|
||||
return $this->del('{lock}:' . $key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function release()
|
||||
{
|
||||
$connections = Kiri::getDi()->get(PoolRedis::class);
|
||||
$connections->release($this->get_config(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁连接池
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function destroy()
|
||||
{
|
||||
$connections = Kiri::getDi()->get(PoolRedis::class);
|
||||
$connections->connection_clear($this->get_config(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $arguments
|
||||
* @return mixed
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function proxy($name, $arguments): mixed
|
||||
{
|
||||
$connections = Kiri::getDi()->get(PoolRedis::class);
|
||||
|
||||
$config = $this->get_config();
|
||||
|
||||
$client = $connections->get($config, true);
|
||||
if (!($client instanceof Base\Redis)) {
|
||||
throw new Exception('Redis connections more.');
|
||||
}
|
||||
$response = $client->{$name}(...$arguments);
|
||||
$this->release();
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function get_config(): array
|
||||
{
|
||||
return Config::get('cache.redis', null, true);
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/27 0027
|
||||
* Time: 11:00
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Redis;
|
||||
|
||||
use Exception;
|
||||
use Kiri;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Core\Json;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Pool\Pool;
|
||||
use Kiri\Server\Events\OnWorkerExit;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
/**
|
||||
* Class Redis
|
||||
* @package Kiri\Cache
|
||||
* @mixin \Redis
|
||||
*/
|
||||
class Redis extends Component
|
||||
{
|
||||
|
||||
|
||||
const REDIS_OPTION_HOST = 'host';
|
||||
const REDIS_OPTION_PORT = 'port';
|
||||
const REDIS_OPTION_PREFIX = 'prefix';
|
||||
const REDIS_OPTION_AUTH = 'auth';
|
||||
const REDIS_OPTION_DATABASES = 'databases';
|
||||
const REDIS_OPTION_TIMEOUT = 'timeout';
|
||||
const REDIS_OPTION_POOL = 'pool';
|
||||
const REDIS_OPTION_POOL_TICK = 'tick';
|
||||
const REDIS_OPTION_POOL_MIN = 'min';
|
||||
const REDIS_OPTION_POOL_MAX = 'max';
|
||||
|
||||
|
||||
private Kiri\Pool\Pool $pool;
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ConfigException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->pool = Kiri::getDi()->get(Pool::class);
|
||||
|
||||
$config = $this->get_config();
|
||||
|
||||
$length = Config::get('cache.redis.pool.max', 10);
|
||||
|
||||
$this->getEventProvider()->on(OnWorkerExit::class, [$this, 'destroy'], 0);
|
||||
|
||||
$this->pool->initConnections($config['host'], $length);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $arguments
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public function __call($name, $arguments): mixed
|
||||
{
|
||||
$time = microtime(true);
|
||||
if (method_exists($this, $name)) {
|
||||
$data = $this->{$name}(...$arguments);
|
||||
} else {
|
||||
$data = $this->proxy($name, $arguments);
|
||||
}
|
||||
if (microtime(true) - $time >= 0.02) {
|
||||
$this->logger->warning('Redis:' . Json::encode([$name, $arguments]) . (microtime(true) - $time));
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param int $timeout
|
||||
* @return bool
|
||||
*/
|
||||
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
|
||||
* @throws Exception
|
||||
*/
|
||||
public function lock($key, int $timeout = 5): bool|int
|
||||
{
|
||||
$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;
|
||||
return $this->eval($script, ['{lock}:' . $key, $timeout], 1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return int
|
||||
* @throws Exception
|
||||
*/
|
||||
public function unlock($key): int
|
||||
{
|
||||
return $this->del('{lock}:' . $key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function release()
|
||||
{
|
||||
$this->pool->clean($this->get_config()['host']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁连接池
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function destroy()
|
||||
{
|
||||
$this->pool->clean($this->get_config()['host']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $arguments
|
||||
* @return mixed
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function proxy($name, $arguments): mixed
|
||||
{
|
||||
$client = $this->getClient();
|
||||
try {
|
||||
$response = $client->{$name}(...$arguments);
|
||||
} catch (\Throwable $throwable) {
|
||||
$response = $this->logger->addError($throwable->getMessage());
|
||||
} finally {
|
||||
$this->pool->push($this->get_config()['host'], $client);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Helper
|
||||
* @throws ConfigException
|
||||
*/
|
||||
private function getClient(): Helper
|
||||
{
|
||||
$config = $this->get_config();
|
||||
return $this->pool->get($config['host'], static function () use ($config) {
|
||||
return new Helper($config);
|
||||
}, 10);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function get_config(): array
|
||||
{
|
||||
return Config::get('cache.redis', null, true);
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -12,7 +12,7 @@ namespace Gii;
|
||||
use Database\Connection;
|
||||
use Database\Db;
|
||||
use Exception;
|
||||
use Kiri\Cache\Redis;
|
||||
use Kiri\Redis\Redis;
|
||||
use Kiri;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user