hyperf-chat/app/Command/RemoveWsCacheCommand.php

76 lines
1.9 KiB
PHP
Raw Normal View History

2020-11-08 17:10:05 +08:00
<?php
declare(strict_types=1);
2021-09-11 12:41:28 +08:00
2020-12-26 21:33:40 +08:00
/**
* This is my open source code, please do not use it for commercial applications.
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*
* @author Yuandong<837215079@qq.com>
* @link https://github.com/gzydong/hyperf-chat
*/
2020-11-08 17:10:05 +08:00
namespace App\Command;
2021-05-21 22:56:42 +08:00
use App\Cache\ServerRunID;
2021-05-22 14:47:46 +08:00
use App\Cache\SocketFdBindUser;
use App\Cache\SocketUserBindFds;
2020-11-08 17:10:05 +08:00
use Hyperf\Command\Annotation\Command;
2020-12-02 15:14:29 +08:00
use Hyperf\Command\Command as HyperfCommand;
2020-11-08 17:10:05 +08:00
use Psr\Container\ContainerInterface;
/**
* @Command
*/
class RemoveWsCacheCommand extends HyperfCommand
{
/**
* @var ContainerInterface
*/
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
parent::__construct('ws:remove-cache');
}
public function configure()
{
parent::configure();
$this->setDescription('清除 WebSocket 客户端 FD 与用户绑定的缓存信息');
}
public function handle()
{
2020-11-29 14:44:11 +08:00
$this->line('此过程可能耗时较长,请耐心等待!', 'info');
2020-12-01 17:47:25 +08:00
// 获取所有已停止运行的服务ID
2021-05-21 22:56:42 +08:00
$arr = ServerRunID::getInstance()->getServerRunIdAll(2);
2020-11-29 14:44:11 +08:00
foreach ($arr as $run_id => $value) {
2021-05-22 14:47:46 +08:00
$this->clear($run_id);
2020-11-08 17:10:05 +08:00
}
$this->line('缓存已清除!', 'info');
}
2021-05-22 14:47:46 +08:00
public function clear(string $run_id)
{
ServerRunID::getInstance()->rem($run_id);
SocketFdBindUser::getInstance()->delete($run_id);
$prefix = SocketUserBindFds::getInstance()->getCachePrefix($run_id);
$iterator = null;
while (true) {
$keys = redis()->scan($iterator, "{$prefix}*", 20);
if ($keys === false) return;
if (!empty($keys)) {
redis()->del(...$keys);
}
}
}
2020-11-08 17:10:05 +08:00
}