centos 当swoole websocket服务器关闭时,swoole表是否自行销毁

prdp8dxp  于 2022-11-07  发布在  其他
关注(0)|答案(1)|浏览(125)

我正在CentOS 7主机上为我的聊天应用程序设置一个Swoole网络套接字服务器。并将使用Swoole表来存储用户列表。
但是我不确定Swoole表的生命周期是什么样的。当Swoole服务器意外关闭时,之前创建的表会发生什么?我需要自己销毁它来释放内存吗?如果需要,我该如何找到并删除它?
swoole table的官方文件并没有提供太多的细节,所以希望有经验的人能给我一个简短的解释。

tzdcorbm

tzdcorbm1#

仅关闭服务器不会清除内存,您必须手动清除内存。
但是,如果整个程序崩溃,您将不需要清除内存。
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();

相关问题