'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. and will use Swoole table for storing users list.

But i am not sure what is the lifespan like for Swoole table. when the Swoole Server shuts down by accident, what will happen to the table created before? 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.



Solution 1:[1]

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.

I think you should use static getter, so that it will be available globally, consider below code as a sample.

<?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();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ahmard