Alpha ZealPHP is early-stage and under active development. APIs may change between minor versions until v1.0. Feedback and bug reports welcome on GitHub.
API Index — Namespaces, Packages, Reports, Indices

Store
in package

StoreOpenSwoole\Table adapter

Cross-worker shared-memory key-value store backed by OpenSwoole\Table. Data is visible to ALL worker processes simultaneously with no locking needed (Table uses spinlocks internally per row).

IMPORTANT: Store::make() must be called BEFORE $app->run() (before workers are forked). The shared memory segment is inherited by all workers on fork.

Column types: \OpenSwoole\Table::TYPE_INT — 1, 2, 4, or 8 bytes \OpenSwoole\Table::TYPE_FLOAT — 8 bytes (double) \OpenSwoole\Table::TYPE_STRING — up to N bytes (specify max length)

Usage:

// Before $app->run():
Store::make('sessions', 4096, [
    'uid'  => [\OpenSwoole\Table::TYPE_STRING, 64],
    'room' => [\OpenSwoole\Table::TYPE_STRING, 32],
    'hits' => [\OpenSwoole\Table::TYPE_INT,    4],
]);

// Anywhere (all workers share the same data):
Store::set('sessions', $fd, ['uid' => 'alice', 'room' => 'general', 'hits' => 0]);
Store::get('sessions', $fd);        // ['uid' => 'alice', ...]
Store::incr('sessions', $fd, 'hits');
Store::del('sessions', $fd);
Store::count('sessions');

// Or use the Table object directly:
$t = Store::table('sessions');
foreach ($t as $key => $row) { ... }

Table of Contents

Properties

$tables  : array<string, Table>

Methods

count()  : int
Number of rows currently stored.
decr()  : int
Atomically decrement an integer column and return new value.
del()  : bool
Delete a row.
exists()  : bool
Check if a row exists.
get()  : mixed
Get a row. Returns array|false.
incr()  : int
Atomically increment an integer column and return new value.
make()  : Table
Create a named shared-memory table.
names()  : array<int, string>
List all registered store names.
set()  : bool
Set a row. $key must be a string.
table()  : Table|null
Get the raw Table object by name.

Properties

$tables

private static array<string, Table> $tables = []

Methods

count()

Number of rows currently stored.

public static count(string $table) : int
Parameters
$table : string
Return values
int

decr()

Atomically decrement an integer column and return new value.

public static decr(string $table, string $key, string $col[, int $by = 1 ]) : int
Parameters
$table : string
$key : string
$col : string
$by : int = 1
Return values
int

del()

Delete a row.

public static del(string $table, string $key) : bool
Parameters
$table : string
$key : string
Return values
bool

exists()

Check if a row exists.

public static exists(string $table, string $key) : bool
Parameters
$table : string
$key : string
Return values
bool

get()

Get a row. Returns array|false.

public static get(string $table, string $key[, string|null $field = null ]) : mixed
Parameters
$table : string
$key : string
$field : string|null = null

incr()

Atomically increment an integer column and return new value.

public static incr(string $table, string $key, string $col[, int $by = 1 ]) : int
Parameters
$table : string
$key : string
$col : string
$by : int = 1
Return values
int

make()

Create a named shared-memory table.

public static make(string $name[, int $maxRows = 1024 ][, array<string, array{0: int, 1: int}> $columns = [] ]) : Table

Call once before $app->run().

Parameters
$name : string

Logical name

$maxRows : int = 1024

Power-of-2 capacity (actual allocation is next power of 2 ≥ $maxRows)

$columns : array<string, array{0: int, 1: int}> = []

['colName' => [TYPE, size]]

Return values
Table

names()

List all registered store names.

public static names() : array<int, string>
Return values
array<int, string>

set()

Set a row. $key must be a string.

public static set(string $table, string $key, array<string, mixed> $row) : bool

Returns false when the table does not exist or when the table is full (OpenSwoole throws \OpenSwoole\Exception in that case; we catch it and return false so callers can handle overflow without a try/catch).

Parameters
$table : string
$key : string
$row : array<string, mixed>
Return values
bool

table()

Get the raw Table object by name.

public static table(string $name) : Table|null
Parameters
$name : string
Return values
Table|null
On this page