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.

Methods

  • Swoole\Table->__construct
  • Swoole\Table->column
  • Swoole\Table->create
  • Swoole\Table->destroy
  • Swoole\Table->set
  • Swoole\Table->get
  • Swoole\Table->count
  • Swoole\Table->del
  • Swoole\Table->exists
  • Swoole\Table->incr
  • Swoole\Table->decr
  • Swoole\Table->getMemorySize
  • Swoole\Table->offsetExists
  • Swoole\Table->offsetGet
  • Swoole\Table->offsetSet
  • Swoole\Table->offsetUnset
  • Swoole\Table->rewind
  • Swoole\Table->next
  • Swoole\Table->current
  • Swoole\Table->key
  • Swoole\Table->valid
  • Swoole\Table\Row->offsetExists
  • Swoole\Table\Row->offsetGet
  • Swoole\Table\Row->offsetSet
  • Swoole\Table\Row->offsetUnset

    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, 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

    Iterator and Countable

    The iterator and countable depends on pcre-devel

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