简体   繁体   English

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

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

i am setting up a Swoole web socket server for my chat application on CentOS 7 host machine.我正在为 CentOS 7 主机上的聊天应用程序设置Swoole Web 套接字服务器。 and will use Swoole table for storing users list.并将使用 Swoole 表来存储用户列表。

But i am not sure what is the lifespan like for Swoole table.但我不确定 Swoole 桌子的使用寿命是多少。 when the Swoole Server shuts down by accident, what will happen to the table created before?当 Swoole Server 意外关闭时,之前创建的表会怎样? Do i need to destroy it myself to free up memory?我需要自己销毁它以释放内存吗? if yes, how can i find the table and remove it?如果是,我怎样才能找到表格并将其删除?

the official document of swoole table doesn't really provide much details on it, so hopefully someone has an experience can give me a short explanation on it. swoole table的官方文档并没有真正提供太多细节,所以希望有经验的人可以给我一个简短的解释。

Shutting down of the server alone will not clear up the memory, you have to clear it up manually.单独关闭服务器不会清理内存,你必须手动清理它。

But you won't be needing to clear the memory if the entirety of the program crashed.但是如果整个程序崩溃了,你就不需要清除内存了。

Swoole tables doesn't have lifespan, they're like regular arrays, you define your data, you delete it. Swoole 表没有生命周期,它们就像常规数组,你定义你的数据,你删除它。

I think you should use static getter, so that it will be available globally, consider below code as a sample.我认为您应该使用静态 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