繁体   English   中英

swoole websocket服务器关闭时swoole table会自行销毁吗

[英]does swoole table destroy itself when swoole websocket server shuts down

我正在为 CentOS 7 主机上的聊天应用程序设置Swoole Web 套接字服务器。 并将使用 Swoole 表来存储用户列表。

但我不确定 Swoole 桌子的使用寿命是多少。 当 Swoole Server 意外关闭时,之前创建的表会怎样? 我需要自己销毁它以释放内存吗? 如果是,我怎样才能找到表格并将其删除?

swoole table的官方文档并没有真正提供太多细节,所以希望有经验的人可以给我一个简短的解释。

单独关闭服务器不会清理内存,你必须手动清理它。

但是如果整个程序崩溃了,你就不需要清除内存了。

Swoole 表没有生命周期,它们就像常规数组,你定义你的数据,你删除它。

我认为您应该使用静态 getter,以便它可以在全球范围内使用,请考虑以下代码作为示例。

<?php

use Swoole\Table;

class UserStorage
{
    private static Table $table;


    public static function getTable(): Table
    {
        if (!isset(self::$table)) {
            self::$table = new Swoole\Table(1024);
            self::$table->column('name', Swoole\Table::TYPE_STRING, 64);
            self::$table->create();

            return self::$table;
        }

        return self::$table;
    }
}

// Add data to table
UserStorage::getTable()->set('a', ['name' => 'Jane']);
// Get data
UserStorage::getTable()->get('a');
// Destroy the table
UserStorage::getTable()->destroy();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM