<?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();
1条答案
按热度按时间tzdcorbm1#
仅关闭服务器不会清除内存,您必须手动清除内存。
但是,如果整个程序崩溃,您将不需要清除内存。
Swoole表没有生命周期,它们就像常规数组,你定义了数据,就删除了它。
我认为你应该使用静态getter,这样它就可以在全球范围内使用,考虑下面的代码作为一个例子。