3. Data Sharing - swooletw/laravel-swoole GitHub Wiki
Data Sharing
As mentioned previously, Laravel applications run in different worker processes. There's one important concept to keep in mind: Variables are not shared across different processes
.
Each worker has their own variables and memory allocations. Keeping Laravel in memory doesn't mean we can share data among different processes.
There are, though, many options to share resources between processes:
- Databases like MySQL and Redis
- APCu - APC User Cache
- Swoole Table
- Any other I/O based alternatives
Swoole Table
In swoole_http.php
, you can customize your own Swoole Table:
use Swoole\Table;
'tables' => [
// define your table name here
'table_name' => [
// table rows number
'size' => 1024,
// column name, column type and column type size are optional for int and float type
'columns' => [
['name' => 'column_name1', 'type' => Table::TYPE_INT],
['name' => 'column_name2', 'type' => Table::TYPE_STRING, 'size' => 1024],
]
],
]
There are three column types of Swoole Table:
- TYPE_INT: 1,2,4,8
- TYPE_FLOAT: 8
- TYPE_STRING: the nth power of 2
Usage
<?php
use SwooleTW\Http\Table\Facades\SwooleTable as Table;
class Foo
{
// get a table by its name
$table = Table::get('table_name');
// update a row of the table by key
$table->set('key', ['col_name' => 'value']);
// get a row of the table by key
$table->get('key', 'col_name');
// delete a row of the table by key
$table->del('key');
// check if a row is existed by key
$table->exist('key');
// count the rows in the table
$table->count();
}
Check more Swoole Table usages here: https://www.swoole.co.uk/docs/modules/swoole-table