sebk69
Posted on March 3, 2023
Introduction
Swoole provide a table system based on memory.
These tables are stored in memory with very high speed access and accessible to every thread and processes.
Creating your first table
To create table, use Swoole\Table object :
$members = new Swoole\Table(500);
The parameter represent max entries managed by table. Adjust the size to avoid using too much memory.
We will now declare fields :
$members->column('firstname', Swoole\Table::TYPE_STRING);
$members->column('lastname', Swoole\Table::TYPE_STRING);
And create the table :
$members->create();
Insert/update data
We can now use the table.
Use set method to persist records :
foreach ([
['firstname' => 'john', 'lastname' => 'doe'],
['firstname' => 'paul', 'lastname' => 'carbon']
] as $key => $record) {
$members->set($key, $record);
}
And same for update :
$members->set(0, ['firstname' => 'john the second', 'lastname' => 'doe']);
Getting data
You can access data from key :
$key = 0;
$member = $members->get($key);
And if you need only one field :
$key = 0;
$field = 'firstname';
$firstname = $members->get($key, $field);
You can also iterate all records with foreach :
foreach ($members as $key => $record) {
echo 'member #' . $key . ' : ' . $record['firstname'] . ' ' . $record['lastname'];
}
Delete a record
To remove a record, use :
$key = 0;
$members->del($key);
Other posts on swoole
💖 💪 🙅 🚩
sebk69
Posted on March 3, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.