This commit is contained in:
as2252258@163.com
2021-05-04 03:07:11 +08:00
parent 6230db7a4f
commit a8eebeb902
4 changed files with 19 additions and 18 deletions
+9 -9
View File
@@ -24,9 +24,9 @@ class Context extends BaseContext
*/
public static function setContext($id, $context, $key = null): mixed
{
// if (!static::inCoroutine()) {
// return static::setStatic($id, $context, $key);
// }
if (Coroutine::getCid() === -1) {
return static::setStatic($id, $context, $key);
}
return self::setCoroutine($id, $context, $key);
}
@@ -106,9 +106,9 @@ class Context extends BaseContext
*/
public static function getContext($id, $key = null): mixed
{
// if (!static::inCoroutine()) {
// return static::loadByStatic($id, $key);
// }
if (Coroutine::getCid() === -1) {
return static::loadByStatic($id, $key);
}
return static::loadByContext($id, $key);
}
@@ -154,7 +154,7 @@ class Context extends BaseContext
*/
public static function getAllContext(): mixed
{
if (static::inCoroutine()) {
if (Coroutine::getCid() === -1) {
return Coroutine::getContext() ?? [];
} else {
return static::$_contents ?? [];
@@ -170,7 +170,7 @@ class Context extends BaseContext
if (!static::hasContext($id, $key)) {
return;
}
if (!static::inCoroutine()) {
if (Coroutine::getCid() === -1) {
if (!empty($key)) {
unset(static::$_contents[$id][$key]);
} else {
@@ -192,7 +192,7 @@ class Context extends BaseContext
*/
public static function hasContext($id, $key = null): bool
{
if (!static::inCoroutine()) {
if (Coroutine::getCid() === -1) {
return static::searchByStatic($id, $key);
}
return static::searchByCoroutine($id, $key);
+6 -5
View File
@@ -8,6 +8,7 @@ use Exception;
use HttpServer\Http\Context;
use JetBrains\PhpStorm\Pure;
use Snowflake\Exception\ConfigException;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use Swoole\Timer;
@@ -149,7 +150,7 @@ abstract class Pool extends Component
if (isset($this->_items[$name]) && $this->_items[$name] instanceof Channel) {
return;
}
if (!Context::inCoroutine()) {
if (Coroutine::getCid() === -1) {
return;
}
$this->_items[$name] = new Channel((int)$max);
@@ -165,7 +166,7 @@ abstract class Pool extends Component
*/
protected function getFromChannel($name, mixed $callback): mixed
{
if (!Context::inCoroutine()) {
if (Coroutine::getCid() === -1) {
return $this->createClient($name, $callback);
}
if (!isset($this->_items[$name])) {
@@ -272,7 +273,7 @@ abstract class Pool extends Component
*/
public function size(string $name): mixed
{
if (!Context::inCoroutine()) {
if (Coroutine::getCid() === -1) {
return 0;
}
if (!isset($this->_items[$name])) {
@@ -288,7 +289,7 @@ abstract class Pool extends Component
*/
public function push(string $name, mixed $client)
{
if (!Context::inCoroutine()) {
if (Coroutine::getCid() === -1) {
return;
}
if (!isset($this->_items[$name])) {
@@ -307,7 +308,7 @@ abstract class Pool extends Component
*/
public function clean(string $name)
{
if (!Context::inCoroutine() || !isset($this->_items[$name])) {
if (Coroutine::getCid() === -1 || !isset($this->_items[$name])) {
return;
}
$channel = $this->_items[$name];
+1 -1
View File
@@ -179,7 +179,7 @@ class Logger extends Component
$files = glob(storage(null, $dirName) . '/*');
if (count($files) >= 15) {
$command = 'find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;';
if (Context::inCoroutine()) {
if (Coroutine::getCid() !== -1) {
Coroutine\System::exec($command);
} else {
\shell_exec($command);
+3 -3
View File
@@ -53,10 +53,10 @@ class Redis extends Pool
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
if (($redis = Context::getContext($coroutineName)) instanceof \Redis) {
return $redis;
if (!Context::hasContext($coroutineName)) {
return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config));
}
return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config));
return Context::getContext($coroutineName);
}