This commit is contained in:
2021-02-23 16:56:34 +08:00
parent 3cef44886d
commit 7ae8d4ffc2
10 changed files with 500 additions and 492 deletions
+1 -1
View File
@@ -107,7 +107,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
*/ */
public function getModel(): ActiveRecord public function getModel(): ActiveRecord
{ {
return Snowflake::app()->getObject()->getConnection([$this->model], false); return Snowflake::app()->getObject()->get($this->model, false);
} }
+1 -1
View File
@@ -899,7 +899,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
public static function populate(array $data): static public static function populate(array $data): static
{ {
$object = Snowflake::app()->getObject(); $object = Snowflake::app()->getObject();
$model = $object->getConnection([static::class], false); $model = $object->get(static::class);
$model->setAttributes($data); $model->setAttributes($data);
$model->setIsCreate(false); $model->setIsCreate(false);
$model->refresh(); $model->refresh();
+1 -1
View File
@@ -51,7 +51,7 @@ class CollectionIterator extends \ArrayIterator
protected function newModel($current): ActiveRecord protected function newModel($current): ActiveRecord
{ {
$object = Snowflake::app()->getObject(); $object = Snowflake::app()->getObject();
$model = $object->getConnection([$this->model], false); $model = $object->get($this->model, false);
return $model->setAttributes($current); return $model->setAttributes($current);
+258 -260
View File
@@ -29,308 +29,306 @@ use Snowflake\Snowflake;
*/ */
class Connection extends Component class Connection extends Component
{ {
const TRANSACTION_COMMIT = 'transaction::commit'; const TRANSACTION_COMMIT = 'transaction::commit';
const TRANSACTION_ROLLBACK = 'transaction::rollback'; const TRANSACTION_ROLLBACK = 'transaction::rollback';
public string $id = 'db'; public string $id = 'db';
public string $cds = ''; public string $cds = '';
public string $password = ''; public string $password = '';
public string $username = ''; public string $username = '';
public string $charset = 'utf-8'; public string $charset = 'utf-8';
public string $tablePrefix = ''; public string $tablePrefix = '';
public int $timeout = 1900; public int $timeout = 1900;
public int $maxNumber = 200; public int $maxNumber = 200;
/** /**
* @var bool * @var bool
* enable database cache * enable database cache
*/ */
public bool $enableCache = false; public bool $enableCache = false;
public string $cacheDriver = 'redis'; public string $cacheDriver = 'redis';
/** /**
* @var array * @var array
* *
* @example [ * @example [
* 'cds' => 'mysql:dbname=dbname;host=127.0.0.1', * 'cds' => 'mysql:dbname=dbname;host=127.0.0.1',
* 'username' => 'root', * 'username' => 'root',
* 'password' => 'root' * 'password' => 'root'
* ] * ]
*/ */
public array $slaveConfig = []; public array $slaveConfig = [];
private ?Schema $_schema = null; private ?Schema $_schema = null;
/** /**
* @throws Exception * @throws Exception
*/ */
public function init() public function init()
{ {
$event = Snowflake::app()->getEvent(); $event = Snowflake::app()->getEvent();
$event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']); $event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']);
$event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']); $event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']);
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public function enablingTransactions() public function enablingTransactions()
{ {
if (!Db::transactionsActive()) { if (!Db::transactionsActive()) {
return; return;
} }
$this->beginTransaction(); $this->beginTransaction();
$event = Snowflake::app()->getEvent(); $event = Snowflake::app()->getEvent();
$event->on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true); $event->on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true);
$event->on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true); $event->on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true);
} }
/** /**
* @param null $sql * @param null $sql
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
public function getConnect($sql = NULL): PDO public function getConnect($sql = NULL): PDO
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber); $connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber); $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
$connections->setTimeout($this->timeout); $connections->setTimeout($this->timeout);
return $this->getPdo($sql); return $this->getPdo($sql);
} }
/** /**
* 初始化 Channel * 初始化 Channel
* @throws ComponentException * @throws ComponentException
*/ */
public function fill() public function fill()
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber); $connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber); $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
} }
/** /**
* @param $sql * @param $sql
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
private function getPdo($sql): PDO private function getPdo($sql): PDO
{ {
if ($this->isWrite($sql)) { if ($this->isWrite($sql)) {
$connect = $this->masterInstance(); $connect = $this->masterInstance();
} else { } else {
$connect = $this->slaveInstance(); $connect = $this->slaveInstance();
} }
return $connect; return $connect;
} }
/** /**
* @return mixed * @return mixed
* @throws ReflectionException * @throws ReflectionException
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function getSchema(): mixed public function getSchema(): mixed
{ {
if ($this->_schema === null) { if ($this->_schema === null) {
$this->_schema = Snowflake::createObject([ $this->_schema = Snowflake::createObject([
'class' => Schema::class, 'class' => Schema::class,
'db' => $this 'db' => $this
]); ]);
} }
return $this->_schema; return $this->_schema;
} }
/** /**
* @param $sql * @param $sql
* @return bool * @return bool
*/ */
#[Pure] public function isWrite($sql): bool #[Pure] public function isWrite($sql): bool
{ {
if (empty($sql)) return false; if (empty($sql)) return false;
$prefix = strtolower(mb_substr($sql, 0, 6)); $prefix = strtolower(mb_substr($sql, 0, 6));
return in_array($prefix, ['insert', 'update', 'delete']); return in_array($prefix, ['insert', 'update', 'delete']);
} }
/** /**
* @return mixed * @return mixed
* @throws ComponentException * @throws ComponentException
*/ */
public function getCacheDriver(): mixed public function getCacheDriver(): mixed
{ {
if (!$this->enableCache) { if (!$this->enableCache) {
return null; return null;
} }
return Snowflake::app()->get($this->cacheDriver); return Snowflake::app()->get($this->cacheDriver);
} }
/** /**
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
public function masterInstance(): PDO public function masterInstance(): PDO
{ {
return $this->connections()->getConnection([ return $this->connections()->get([
'cds' => $this->cds, 'cds' => $this->cds, 'username' => $this->username, 'password' => $this->password
'username' => $this->username, ], true);
'password' => $this->password }
], true);
}
/** /**
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
public function slaveInstance(): PDO public function slaveInstance(): PDO
{ {
if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) { if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) {
return $this->masterInstance(); return $this->masterInstance();
} }
return $this->connections()->getConnection($this->slaveConfig, false); return $this->connections()->get($this->slaveConfig, false);
} }
/** /**
* @return \Snowflake\Pool\Connection * @return \Snowflake\Pool\Connection
* @throws ComponentException * @throws ComponentException
*/ */
private function connections(): \Snowflake\Pool\Connection private function connections(): \Snowflake\Pool\Connection
{ {
return Snowflake::app()->getConnections(); return Snowflake::app()->getConnections();
} }
/** /**
* @return $this * @return $this
* @throws Exception * @throws Exception
*/ */
public function beginTransaction(): static public function beginTransaction(): static
{ {
$this->connections()->beginTransaction($this->cds); $this->connections()->beginTransaction($this->cds);
return $this; return $this;
} }
/** /**
* @return $this|bool * @return $this|bool
* @throws Exception * @throws Exception
*/ */
public function inTransaction(): bool|static public function inTransaction(): bool|static
{ {
return $this->connections()->inTransaction($this->cds); return $this->connections()->inTransaction($this->cds);
} }
/** /**
* @throws Exception * @throws Exception
* 事务回滚 * 事务回滚
*/ */
public function rollback() public function rollback()
{ {
$this->connections()->rollback($this->cds); $this->connections()->rollback($this->cds);
} }
/** /**
* @throws Exception * @throws Exception
* 事务提交 * 事务提交
*/ */
public function commit() public function commit()
{ {
$this->connections()->commit($this->cds); $this->connections()->commit($this->cds);
} }
/** /**
* @param $sql * @param $sql
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
public function refresh($sql): PDO public function refresh($sql): PDO
{ {
if ($this->isWrite($sql)) { if ($this->isWrite($sql)) {
$instance = $this->masterInstance(); $instance = $this->masterInstance();
} else { } else {
$instance = $this->slaveInstance(); $instance = $this->slaveInstance();
} }
return $instance; return $instance;
} }
/** /**
* @param $sql * @param $sql
* @param array $attributes * @param array $attributes
* @return Command * @return Command
* @throws * @throws
*/ */
public function createCommand($sql = null, $attributes = []): Command public function createCommand($sql = null, $attributes = []): Command
{ {
$command = new Command(['db' => $this, 'sql' => $sql]); $command = new Command(['db' => $this, 'sql' => $sql]);
return $command->bindValues($attributes); return $command->bindValues($attributes);
} }
/** /**
* @return Select * @return Select
* @throws Exception * @throws Exception
*/ */
public function getBuild(): Select public function getBuild(): Select
{ {
return $this->getSchema()->getQueryBuilder(); return $this->getSchema()->getQueryBuilder();
} }
/** /**
* *
* 回收链接 * 回收链接
* @throws * @throws
*/ */
public function release() public function release()
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->release($this->cds, true); $connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false); $connections->release($this->slaveConfig['cds'], false);
} }
/** /**
* 临时回收 * 临时回收
* @throws ComponentException * @throws ComponentException
*/ */
public function recovery() public function recovery()
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->release($this->cds, true); $connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false); $connections->release($this->slaveConfig['cds'], false);
} }
/** /**
* *
* 回收链接 * 回收链接
* @throws * @throws
*/ */
public function clear_connection() public function clear_connection()
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->release($this->cds, true); $connections->release($this->cds, true);
$connections->release($this->slaveConfig['cds'], false); $connections->release($this->slaveConfig['cds'], false);
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public function disconnect() public function disconnect()
{ {
$connections = $this->connections(); $connections = $this->connections();
$connections->disconnect($this->cds); $connections->disconnect($this->cds);
$connections->disconnect($this->slaveConfig['cds']); $connections->disconnect($this->slaveConfig['cds']);
} }
} }
+169 -168
View File
@@ -10,6 +10,7 @@ use HttpServer\Http\Context;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use PDO; use PDO;
use Redis; use Redis;
use Snowflake\Exception\ComponentException;
use Snowflake\Pool\Timeout; use Snowflake\Pool\Timeout;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Coroutine\Channel; use Swoole\Coroutine\Channel;
@@ -22,199 +23,199 @@ use Swoole\Timer;
abstract class Pool extends Component abstract class Pool extends Component
{ {
/** @var Channel[] */ /** @var Channel[] */
private array $_items = []; private array $_items = [];
protected int $max = 60; protected int $max = 60;
use Timeout; use Timeout;
/** /**
* @param $driver * @param $driver
* @param $name * @param $name
* @param false $isMaster * @param false $isMaster
* @param int $max * @param int $max
*/ */
public function initConnections($driver, $name, $isMaster = false, $max = 60) public function initConnections($driver, $name, $isMaster = false, $max = 60)
{ {
$name = $this->name($driver, $name, $isMaster); $name = $this->name($driver, $name, $isMaster);
if (isset($this->_items[$name]) && $this->_items[$name] instanceof Channel) { if (isset($this->_items[$name]) && $this->_items[$name] instanceof Channel) {
return; return;
} }
if (!Context::inCoroutine()) { if (!Context::inCoroutine()) {
return; return;
} }
$this->_items[$name] = new Channel((int)$max); $this->_items[$name] = new Channel((int)$max);
$this->max = (int)$max; $this->max = (int)$max;
} }
/** /**
* @param $name * @param $name
* @param array $callback * @param array $callback
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
protected function get($name, array $callback): mixed protected function getFromChannel($name, array $callback): mixed
{ {
if (!Context::inCoroutine()) { if (!Context::inCoroutine()) {
return $this->createClient($name, $callback); return $this->createClient($name, $callback);
} }
if (!$this->hasItem($name)) { if (!$this->hasItem($name)) {
if ($this->creates === -1) { if ($this->creates === -1) {
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']); $this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
} }
if (!Context::hasContext('create::client::ing::' . $name)) { if (!Context::hasContext('create::client::ing::' . $name)) {
$this->push($name, $this->createClient($name, $callback)); $this->push($name, $this->createClient($name, $callback));
Context::deleteContext('create::client::ing::' . $name); Context::deleteContext('create::client::ing::' . $name);
} }
} }
$connection = $this->_items[$name]->pop(-1); $connection = $this->_items[$name]->pop(-1);
if (!$this->checkCanUse($name, $connection)) { if (!$this->checkCanUse($name, $connection)) {
return $this->createClient($name, $callback); return $this->createClient($name, $callback);
} else { } else {
return $connection; return $connection;
} }
} }
/** /**
* @param $cds * @param $cds
* @param $coroutineName * @param $coroutineName
* @param false $isBefore * @param false $isBefore
*/ * @throws ComponentException
public function printClients($cds, $coroutineName, $isBefore = false) */
{ public function printClients($cds, $coroutineName, $isBefore = false)
$this->warning(($isBefore ? 'before ' : '') . 'create client[address: ' . $cds . ', ' . env('workerId') . ', coroutine: ' . Coroutine::getCid() . ', has num: ' . $this->size($coroutineName) . ', has create: ' . $this->_create . ']'); {
} $this->warning(($isBefore ? 'before ' : '') . 'create client[address: ' . $cds . ', ' . env('workerId') . ', coroutine: ' . Coroutine::getCid() . ', has num: ' . $this->size($coroutineName) . ', has create: ' . $this->_create . ']');
}
abstract public function createClient(string $name, array $config): mixed; abstract public function createClient(string $name, mixed $config): mixed;
/** /**
* @param $driver * @param $driver
* @param $cds * @param $cds
* @param false $isMaster * @param false $isMaster
* @return string * @return string
*/ */
#[Pure] public function name($driver, $cds, $isMaster = false): string #[Pure] public function name($driver, $cds, $isMaster = false): string
{ {
if ($isMaster === true) { if ($isMaster === true) {
return $cds . '_master'; return $cds . '_master';
} else { } else {
return $cds . '_slave'; return $cds . '_slave';
} }
} }
/** /**
* @param $name * @param string $name
* @param $time * @param $client
* @param $client * @return mixed
* @return mixed * 检查连接可靠性
* 检查连接可靠性 */
* @throws Exception public function checkCanUse(string $name, mixed $client): mixed
*/ {
public function checkCanUse(string $name, mixed $client): mixed return true;
{ }
return true;
}
/** /**
* @param $name * @param $name
* @throws Exception * @throws Exception
*/ */
public function desc(string $name) public function desc(string $name)
{ {
throw new Exception('Undefined system processing function.'); throw new Exception('Undefined system processing function.');
} }
/** /**
* @param array $config * @param array $config
* @param $isMaster * @param $isMaster
* @throws Exception * @return mixed
*/ * @throws Exception
public function getConnection(array $config, bool $isMaster) */
{ public function get(mixed $config, bool $isMaster): mixed
throw new Exception('Undefined system processing function.'); {
} throw new Exception('Undefined system processing function.');
}
/** /**
* @param $name * @param $name
* @return bool * @return bool
*/ */
public function hasItem(string $name): bool public function hasItem(string $name): bool
{ {
if (isset($this->_items[$name])) { if (isset($this->_items[$name])) {
return !$this->_items[$name]->isEmpty(); return !$this->_items[$name]->isEmpty();
} }
return false; return false;
} }
/** /**
* @param $name * @param $name
* @return mixed * @return mixed
*/ */
public function size(string $name): mixed public function size(string $name): mixed
{ {
if (!Context::inCoroutine()) { if (!Context::inCoroutine()) {
return 0; return 0;
} }
if (!isset($this->_items[$name])) { if (!isset($this->_items[$name])) {
return 0; return 0;
} }
return $this->_items[$name]->length(); return $this->_items[$name]->length();
} }
/** /**
* @param $name * @param $name
* @param $client * @param $client
*/ */
public function push(string $name, mixed $client) public function push(string $name, mixed $client)
{ {
if (!Context::inCoroutine()) { if (!Context::inCoroutine()) {
return; return;
} }
if (!isset($this->_items[$name])) { if (!isset($this->_items[$name])) {
$this->_items[$name] = new Channel($this->max); $this->_items[$name] = new Channel($this->max);
} }
if (!$this->_items[$name]->isFull()) { if (!$this->_items[$name]->isFull()) {
$this->_items[$name]->push($client); $this->_items[$name]->push($client);
} }
} }
/** /**
* @param string $name * @param string $name
* @throws Exception * @throws Exception
*/ */
public function clean(string $name) public function clean(string $name)
{ {
if (!Context::inCoroutine()) { if (!Context::inCoroutine()) {
return; return;
} }
if (!isset($this->_items[$name])) { if (!isset($this->_items[$name])) {
return; return;
} }
$channel = $this->_items[$name]; $channel = $this->_items[$name];
while ($client = $channel->pop(0.001)) { while ($client = $channel->pop(0.001)) {
unset($client); unset($client);
$this->desc($name); $this->desc($name);
} }
} }
/** /**
* @return Channel[] * @return Channel[]
*/ */
protected function getChannels(): array protected function getChannels(): array
{ {
return $this->_items; return $this->_items;
} }
} }
+1 -1
View File
@@ -141,7 +141,7 @@ SCRIPT;
{ {
$connections = Snowflake::app()->getPool()->getRedis(); $connections = Snowflake::app()->getPool()->getRedis();
$client = $connections->getConnection($this->get_config(), true); $client = $connections->get($this->get_config(), true);
if (!($client instanceof \Redis)) { if (!($client instanceof \Redis)) {
throw new Exception('Redis connections more.'); throw new Exception('Redis connections more.');
} }
+13 -10
View File
@@ -7,6 +7,7 @@ use HttpServer\Http\Context;
use PDO; use PDO;
use Exception; use Exception;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Exception\ComponentException;
use Swoole\Coroutine; use Swoole\Coroutine;
use Snowflake\Abstracts\Pool; use Snowflake\Abstracts\Pool;
use Swoole\Timer; use Swoole\Timer;
@@ -129,12 +130,12 @@ class Connection extends Pool
/** /**
* @param array $config * @param mixed $config
* @param bool $isMaster * @param bool $isMaster
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function getConnection(array $config, $isMaster = false): mixed public function get(mixed $config, $isMaster = false): mixed
{ {
$coroutineName = $this->name('mysql', $config['cds'], $isMaster); $coroutineName = $this->name('mysql', $config['cds'], $isMaster);
if (!isset($this->hasCreate[$coroutineName])) { if (!isset($this->hasCreate[$coroutineName])) {
@@ -143,7 +144,7 @@ class Connection extends Pool
if (($pdo = Context::getContext($coroutineName)) instanceof PDO) { if (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
return $pdo; return $pdo;
} }
$connections = $this->get($coroutineName, $config); $connections = $this->getFromChannel($coroutineName, $config);
if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) { if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) {
$number > 0 && $connections->beginTransaction(); $number > 0 && $connections->beginTransaction();
} }
@@ -153,10 +154,11 @@ class Connection extends Pool
/** /**
* @param string $name * @param string $name
* @param array $config * @param mixed $config
* @return PDO * @return PDO
* @throws ComponentException
*/ */
public function createClient(string $name, array $config): PDO public function createClient(string $name, mixed $config): PDO
{ {
$this->printClients($config['cds'], $name, true); $this->printClients($config['cds'], $name, true);
// TODO: Implement createClient() method. // TODO: Implement createClient() method.
@@ -177,11 +179,12 @@ class Connection extends Pool
} }
/** /**
* @param $cds * @param $cds
* @param $coroutineName * @param $coroutineName
* @param false $isBefore * @param false $isBefore
*/ * @throws ComponentException
*/
public function printClients($cds, $coroutineName, $isBefore = false) public function printClients($cds, $coroutineName, $isBefore = false)
{ {
$this->warning(($isBefore ? 'before ' : '') . 'create client[address: ' . $cds . ', ' . env('workerId') . ', coroutine: ' . Coroutine::getCid() . ', has num: ' . $this->size($coroutineName) . ', has create: ' . $this->hasCreate[$coroutineName] . ']'); $this->warning(($isBefore ? 'before ' : '') . 'create client[address: ' . $cds . ', ' . env('workerId') . ', coroutine: ' . Coroutine::getCid() . ', has num: ' . $this->size($coroutineName) . ', has create: ' . $this->hasCreate[$coroutineName] . ']');
+43 -43
View File
@@ -17,54 +17,54 @@ class ObjectPool extends \Snowflake\Abstracts\Pool
{ {
/** /**
* set pool max length * set pool max length
*/ */
public function init() public function init()
{ {
$this->max = 100; $this->max = 100;
} }
/** /**
* @param array $config * @param array $config
* @param bool $isMaster * @param bool $isMaster
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function getConnection(array $config, bool $isMaster): mixed public function get(mixed $config, bool $isMaster = false): mixed
{ {
if (is_object($config[0])) { if (is_object($config)) {
$config[0] = get_class($config[0]) ; return $config;
} }
return $this->get(md5($config[0]), $config); return $this->getFromChannel(md5($config), $config);
} }
/** /**
* @param string $name * @param string $name
* @param array $config * @param mixed $config
* @return mixed * @return mixed
* @throws ReflectionException * @throws ReflectionException
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function createClient(string $name, array $config): mixed public function createClient(string $name, mixed $config): mixed
{ {
// TODO: Implement createClient() method. // TODO: Implement createClient() method.
return Snowflake::createObject(array_shift($config)); return Snowflake::createObject(array_shift($config));
} }
/** /**
* @param string $name * @param string $name
* @param $object * @param $object
*/ */
public function release(string $name, mixed $object) public function release(string $name, mixed $object)
{ {
if (method_exists($object, 'clean')) { if (method_exists($object, 'clean')) {
$object->clean(); $object->clean();
} }
$this->push($name, $object); $this->push($name, $object);
} }
} }
+6 -1
View File
@@ -37,7 +37,12 @@ class Pool extends \Snowflake\Abstracts\Pool
} }
public function createClient(string $name, array $config): mixed /**
* @param string $name
* @param mixed $config
* @return mixed
*/
public function createClient(string $name, mixed $config): mixed
{ {
// TODO: Implement createClient() method. // TODO: Implement createClient() method.
return null; return null;
+7 -6
View File
@@ -8,6 +8,7 @@ namespace Snowflake\Pool;
use HttpServer\Http\Context; use HttpServer\Http\Context;
use Redis as SRedis; use Redis as SRedis;
use RedisException; use RedisException;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\RedisConnectException; use Snowflake\Exception\RedisConnectException;
use Swoole\Coroutine; use Swoole\Coroutine;
use Exception; use Exception;
@@ -33,29 +34,29 @@ class Redis extends Pool
/** /**
* @param array $config * @param mixed $config
* @param bool $isMaster * @param bool $isMaster
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function getConnection(array $config, $isMaster = false): mixed public function get(mixed $config, $isMaster = false): mixed
{ {
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster); $coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
if (($redis = Context::getContext($coroutineName)) instanceof \Redis) { if (($redis = Context::getContext($coroutineName)) instanceof \Redis) {
return $redis; return $redis;
} }
return Context::setContext($coroutineName, $this->get($coroutineName, $config)); return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config));
} }
/** /**
* @param string $name * @param string $name
* @param array $config * @param mixed $config
* @return SRedis * @return SRedis
* @throws RedisConnectException * @throws RedisConnectException|ComponentException
*/ */
public function createClient(string $name, array $config): SRedis public function createClient(string $name, mixed $config): SRedis
{ {
$this->printClients($config['host'], $name, true); $this->printClients($config['host'], $name, true);
$redis = new SRedis(); $redis = new SRedis();