Swoole\Table - Swoole Table Documentation

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Swoole table is a high performance memory management module, implemented based on shared memory and spin lock. It can be used by multiple processes: worker process or task worker process.

One row can be modified and accessed by multiple threads or processes. Global variables can be shared or used by multiple Swoole Worker processes.

Swoole table provides a two dimensions memory table for developers.

For performance reason, from v4.7.0, you can't use object of type Swoole\Table as array.

Methods

Why using Swoole Table

  • High performance, the single thread read/write speed is more than 2 millions per second.
  • Share variables across multiple threads or processes.
  • Store counters in your application.

Basic usage of Swoole Table, Access as array:

For performance reason, from v4.7.0, you can't use object of type Swoole\Table as array.

<?php

$table = new Swoole\Table(1024);
$table->column('name', Swoole\Table::TYPE_STRING, 64);
$table->column('id', Swoole\Table::TYPE_INT, 4);       //1,2,4,8
$table->column('num', Swoole\Table::TYPE_FLOAT);
$table->create();

$table->set('a', array('id' => 1, 'name' => 'swoole-co-uk', 'num' => 3.1415));
$table->set('b', array('id' => 2, 'name' => "swoole-uk", 'num' => 3.1415));
$table->set('[email protected]', array('id' => 3, 'name' => 'swoole', 'num' => 3.1415));

var_dump($table->get('a'));
var_dump($table->get('b', 'name'));

More Swoole Table Example: /docs/modules/swoole-table-example

Iterator and Countable

The iterator and countable depends on pcre-devel

<?php
foreach($table as $row) {
    var_dump($row);
}
echo count($table);
Last updated on August 31, 2022