改名
This commit is contained in:
+79
-50
@@ -18,54 +18,83 @@ class Channel extends Component
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
private static array $_channels = [];
|
private static array $_channels = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
private static array $_waitRecover = [];
|
||||||
* @param mixed $value
|
|
||||||
* @param string $name
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function push(mixed $value, string $name = ''): void
|
|
||||||
{
|
|
||||||
$channel = $this->channelInit($name);
|
|
||||||
if ($channel->count() >= 100) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$channel->enqueue($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
public function init()
|
||||||
* @param string $name
|
{
|
||||||
* @return bool|SplQueue
|
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'recover']);
|
||||||
*/
|
}
|
||||||
private function channelInit(string $name = ''): bool|SplQueue
|
|
||||||
{
|
|
||||||
if (!isset(static::$_channels[$name]) || !(static::$_channels[$name] instanceof SplQueue)) {
|
|
||||||
static::$_channels[$name] = new SplQueue();
|
|
||||||
}
|
|
||||||
return static::$_channels[$name];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* 回收对象
|
||||||
* 清空缓存
|
*/
|
||||||
*/
|
public function recover()
|
||||||
public function cleanAll()
|
{
|
||||||
{
|
foreach (Channel::$_waitRecover as $key => $value) {
|
||||||
/** @var SplQueue $channel */
|
if (empty($value)) {
|
||||||
foreach (static::$_channels as $channel) {
|
continue;
|
||||||
if (!($channel instanceof SplQueue)) {
|
}
|
||||||
continue;
|
$channel = $this->channelInit($key);
|
||||||
}
|
if ($channel->count() >= 100) {
|
||||||
while ($channel->count() > 0) {
|
continue;
|
||||||
$channel->dequeue();
|
}
|
||||||
}
|
foreach ($value as $item) {
|
||||||
}
|
$channel->enqueue($item);
|
||||||
static::$_channels = [];
|
}
|
||||||
}
|
}
|
||||||
|
Channel::$_waitRecover = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @param string $name
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function push(mixed $value, string $name = ''): void
|
||||||
|
{
|
||||||
|
if (!isset(Channel::$_waitRecover[$name])) {
|
||||||
|
Channel::$_waitRecover[$name] = [];
|
||||||
|
}
|
||||||
|
Channel::$_waitRecover[$name][] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return bool|SplQueue
|
||||||
|
*/
|
||||||
|
private function channelInit(string $name = ''): bool|SplQueue
|
||||||
|
{
|
||||||
|
if (!isset(static::$_channels[$name]) || !(static::$_channels[$name] instanceof SplQueue)) {
|
||||||
|
static::$_channels[$name] = new SplQueue();
|
||||||
|
}
|
||||||
|
return static::$_channels[$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* 清空缓存
|
||||||
|
*/
|
||||||
|
public function cleanAll()
|
||||||
|
{
|
||||||
|
/** @var SplQueue $channel */
|
||||||
|
foreach (static::$_channels as $channel) {
|
||||||
|
if (!($channel instanceof SplQueue)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
while ($channel->count() > 0) {
|
||||||
|
$channel->dequeue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static::$_channels = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -73,14 +102,14 @@ class Channel extends Component
|
|||||||
* @param Closure $closure
|
* @param Closure $closure
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function pop(string $name, Closure $closure): mixed
|
public function pop(string $name, Closure $closure): mixed
|
||||||
{
|
{
|
||||||
$channel = $this->channelInit($name);
|
$channel = $this->channelInit($name);
|
||||||
if ($channel->isEmpty()) {
|
if ($channel->isEmpty()) {
|
||||||
return call_user_func($closure);
|
return call_user_func($closure);
|
||||||
}
|
}
|
||||||
return clone $channel->dequeue();
|
return $channel->dequeue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user