我正在为我的聊天应用程序在斯沃尔 7主机上设置一个CentOS网络套接字服务器。并将使用Swoole表存储用户列表。
但是我不知道Swoole桌子的寿命是什么样子的。当Swoole意外关闭时,前面创建的表会发生什么情况?我需要自己销毁它来释放记忆吗?如果是的话,我怎样才能找到这张桌子并把它移走?
官方文件中的swoole表并没有提供太多的细节,所以希望有经验的人能给我一个简短的解释。
发布于 2022-05-11 07:11:20
仅关闭服务器并不能清除内存,必须手动清除。
但是,如果整个程序崩溃,则不需要清除内存。
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();https://stackoverflow.com/questions/71624486
复制相似问题