Store & Counter

OpenSwoole adapters for cross-worker shared memory. Must be created before $app->run() so all forked workers inherit the same memory segment.

πŸ—ƒοΈ

Store β€” OpenSwoole\Table

Row-based shared memory with per-row spinlocks. Any worker can read/write any row concurrently. Iterate all rows across workers.

πŸ”’

Counter β€” OpenSwoole\Atomic

Lock-free integer. Safe for concurrent increment/decrement from all workers. Useful for metrics, rate limiting, and request counting.

GET Store β€” set / get / count
// Before app->run():
Store::make('demo_table', 128, [
    'name'  => [\OpenSwoole\Table::TYPE_STRING, 64],
    'score' => [\OpenSwoole\Table::TYPE_INT,    4],
]);

// In any route (any worker):
Store::set('demo_table', 'user_1', ['name' => 'alice', 'score' => 100]);
$row = Store::get('demo_table', 'user_1');
// β†’ ['name' => 'alice', 'score' => 100]

echo Store::count('demo_table'); // total rows across all workers
LIVE OUTPUT Click Run β†’
GET Store β€” atomic incr/decr
// Atomically increment a counter column
Store::set('demo_table', 'page_hits', ['score' => 0]);
$new = Store::incr('demo_table', 'page_hits', 'score');
// β†’ 1 (atomic, safe under concurrent workers)
LIVE OUTPUT Click Run β†’
GET Counter β€” increment across requests
// Before app->run():
$requestCounter = new Counter(0);

// In any route:
$app->route('/demo/counter/increment', function() use ($requestCounter) {
    $new = $requestCounter->increment();
    return ['total_requests' => $new, 'pid' => getmypid()];
    // Every worker shares the same atomic integer
});
LIVE OUTPUT Click Run β†’

Store API reference

MethodReturns
Store::make($name, $maxRows, $columns)OpenSwoole\Table
Store::set($table, $key, $row)bool
Store::get($table, $key, $field?)array|mixed|false
Store::del($table, $key)bool
Store::exists($table, $key)bool
Store::incr($table, $key, $col, $by=1)int (new value)
Store::decr($table, $key, $col, $by=1)int (new value)
Store::count($table)int
Store::table($name)OpenSwoole\Table (iterate with foreach)