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,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool\Helper;
|
||||
namespace Kiri\Pool;
|
||||
|
||||
interface QueueInterface
|
||||
{
|
||||
@@ -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,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Pool\Helper;
|
||||
namespace Kiri\Pool;
|
||||
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
@@ -7,13 +7,13 @@
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Cache;
|
||||
namespace Kiri\Redis;
|
||||
|
||||
/**
|
||||
* Interface ICache
|
||||
* @package Kiri\Cache
|
||||
*/
|
||||
interface ICache
|
||||
interface CacheInterface
|
||||
{
|
||||
/**
|
||||
* @param $key
|
||||
@@ -1,27 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Cache\Base;
|
||||
namespace Kiri\Redis;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\RedisConnectException;
|
||||
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 Redis implements StopHeartbeatCheck
|
||||
class Helper implements StopHeartbeatCheck
|
||||
{
|
||||
|
||||
const DB_ERROR_MESSAGE = 'The system is busy, please try again later.';
|
||||
|
||||
|
||||
private ?\Redis $pdo = null;
|
||||
|
||||
public string $host;
|
||||
@@ -68,18 +65,6 @@ class Redis implements StopHeartbeatCheck
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Kiri\Server\Events\OnWorkerExit $exit
|
||||
* @return void
|
||||
*/
|
||||
public function onWorkerExit(OnWorkerExit $exit)
|
||||
{
|
||||
$this->stopHeartbeatCheck();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -7,21 +7,18 @@
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Cache;
|
||||
namespace Kiri\Redis;
|
||||
|
||||
use Exception;
|
||||
use Kiri;
|
||||
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\Pool\Pool;
|
||||
use Kiri\Server\Events\OnWorkerExit;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Swoole\Timer;
|
||||
|
||||
/**
|
||||
* Class Redis
|
||||
@@ -44,6 +41,9 @@ class Redis extends Component
|
||||
const REDIS_OPTION_POOL_MAX = 'max';
|
||||
|
||||
|
||||
private Kiri\Pool\Pool $pool;
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ConfigException
|
||||
@@ -53,7 +53,7 @@ class Redis extends Component
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$connections = Kiri::getDi()->get(PoolRedis::class);
|
||||
$this->pool = Kiri::getDi()->get(Pool::class);
|
||||
|
||||
$config = $this->get_config();
|
||||
|
||||
@@ -61,7 +61,7 @@ class Redis extends Component
|
||||
|
||||
$this->getEventProvider()->on(OnWorkerExit::class, [$this, 'destroy'], 0);
|
||||
|
||||
$connections->initConnections('Redis:' . $config['host'], true, $length);
|
||||
$this->pool->initConnections($config['host'], $length);
|
||||
}
|
||||
|
||||
|
||||
@@ -86,23 +86,23 @@ class Redis extends Component
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -142,8 +142,7 @@ SCRIPT;
|
||||
*/
|
||||
public function release()
|
||||
{
|
||||
$connections = Kiri::getDi()->get(PoolRedis::class);
|
||||
$connections->release($this->get_config(), true);
|
||||
$this->pool->clean($this->get_config()['host']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,8 +152,7 @@ SCRIPT;
|
||||
*/
|
||||
public function destroy()
|
||||
{
|
||||
$connections = Kiri::getDi()->get(PoolRedis::class);
|
||||
$connections->connection_clear($this->get_config(), true);
|
||||
$this->pool->clean($this->get_config()['host']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,19 +164,31 @@ SCRIPT;
|
||||
*/
|
||||
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.');
|
||||
$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);
|
||||
}
|
||||
$response = $client->{$name}(...$arguments);
|
||||
$this->release();
|
||||
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
|
||||
+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