Swoole\Table - Swoole Table Documentation

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.

Why using Swoole Table

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

    Basic usage of Swoole Table API, Access as Array:

    <?php
    
    $table = new Swoole\Table(1024);
    $table->column('id', Swoole\Table::TYPE_INT);
    $table->column('name', Swoole\Table::TYPE_STRING, 64);
    $table->column('num', Swoole\Table::TYPE_FLOAT);
    $table->create();
    
    $table['apple'] = array('id' => 145, 'name' => 'iPhone', 'num' => 3.1415);
    $table['google'] = array('id' => 358, 'name' => "AlphaGo", 'num' => 3.1415);
    $table['microsoft']['name'] = "Windows";
    $table['microsoft']['num'] = '1997.03';
    
    var_dump($table['apple']);
    var_dump($table['microsoft']);
    $table['google']['num'] = 500.90;
    var_dump($table['google']);
    

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

    Swoole\Table->__construct(int $table_size [, $conflict_proportion = 0.2 ])

    Create a Swoole Table with defined rows.

    int Swoole\Table->column(string $name, string $type [, int $size ])

    Set the data type and size of the columns.

    void Swoole\Table->create()

    Create the swoole memory table.

    void Swoole\Table->set(string $key, array $value)

    Update a row of the table by $row_key.

    int Swoole\Table->get(string $row_key, string $column_key)

    Get the value by $row_key and $column_key.

    void Swoole\Table->del(string $key)

    Delete a row by $row_key.

    bool Swoole\Table->exist(string $key)

    Check if a row is existed by $row_key.

    void Swoole\Table->incr(string $key, string $column [, int $incrby])

    Increment the value by $row_key and $column_key.

    void Swoole\Table->decr(string $key, string $column [, int $decrby])

    Decrement the value by $row_key and $column_key.

    int Swoole\Table->count()

    Count the rows in the table, or count all the elements in the table if $mode = 1.

    int Swoole\Table->getMemorySize()

    Count the rows in the table, or count all the elements in the table if $mode = 1.

    void Swoole\Table->destroy()

    Destroy the memory table.

    PCRE is needed for following function:

    void Swoole\Table->rewind()
    void Swoole\Table->next()
    array Swoole\Table->current()
    string Swoole\Table->key()
    bool Swoole\Table->valid()
    Iterator and Countable

    The iterator and countable depends on pcre-devel

    <?php
    foreach($table as $row)
    {
        var_dump($row);
    }
    echo count($table);