承接 small/swoole-db 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

small/swoole-db

Composer 安装命令:

composer require small/swoole-db

包简介

This package provide advanced features to manipulate openswoole tables

README 文档

README


    

This package provide advanced features to manipulate OpenSwoole\Table.

The heavy advantages of OpenSwoole\Table is heavy speed access : you can read about 2 million records in one second, and it is shared between processes.

This package intend to allow you to easy manage OpenSwoole\Table.

Registries

Table registry

Table registry is your entry point to small-swoole-db

You can :

  • create a table in memory and register it
  • get a table already registered
  • destroy a table in memory
  • persist a table to :
    • Json file
    • Redis
    • Mysql
  • Load a table previously persisted

Create a table

To create a table, use registry method :

use \Small\SwooleDb\Registry\TableRegistry;

$testTable = TableRegistry::getInstance()->createTable('testTable', 128);
  • The first param is the table name in registry. Use it to recall table from other parts of code.
  • The second param is the max number of rows in the table

Once the table is created in registry, you can add columns :

Only these type are accepted :

  • ColumnType::string
  • ColumnType::float
  • ColumnType::int

Note the size param is required, except for flot type

use \Small\SwooleDb\Registry\TableRegistry;
use \Small\SwooleDb\Core\Column;
use \Small\SwooleDb\Core\Enum\ColumnType;

TableRegistry::getInstance()->getTable('testTable')
    ->addColumn(new Column('firstname', ColumnType::string, 256))
    ->addColumn(new Column('credit', ColumnType::float))
    ->addColumn(new Column('rank', ColumnType::int, 8))

Now we have added the columns, we can create in memory :

use \Small\SwooleDb\Registry\TableRegistry;

$success = TableRegistry::getInstance()->getTable('testTable')->create();

The table is now ready to use as a OpenSwoole\Table object :

use \Small\SwooleDb\Registry\TableRegistry;

$table = TableRegistry::getInstance()->getTable('testTable')
$table->set(0, ['franck', 12.5, 11]);
$table->set(1, ['joe', 55.2, 26]);

Foreign key

You can link two tables throw foreign key.

use Small\SwooleDb\Registry\TableRegistry;

$table = TableRegistry::getInstance()->createTable('testSelectJoin', 5);
$table->addColumn(new Column('name', ColumnType::string, 255));
$table->addColumn(new Column('price', ColumnType::float));
$table->create();
$table->set(0, ['name' => 'john', 'price' => 12.5]);
$table->set(1, ['name' => 'paul', 'price' => 34.9]);

$table2 = TableRegistry::getInstance()->createTable('testSelectJoinPost', 5);
$table2->addColumn(new Column('message', ColumnType::string, 255));
$table2->addColumn(new Column('ownerId', ColumnType::int, 16));
$table2->create();
$table2->set(0, ['message' => 'ceci est un test', 'ownerId' => 0]);
$table2->set(1, ['message' => 'ceci est un autre test', 'ownerId' => 1]);
$table2->set(2, ['message' => 'ceci est une suite de test', 'ownerId' => 1]);

$table2->addForeignKey('messageOwner', 'testSelectJoin', 'ownerId');

See OpenSwoole documentation for Table

Destroy a table

Once you don't need anymore table, you can destroy it to free all associated memory.

use \Small\SwooleDb\Registry\TableRegistry;

$table = TableRegistry::getInstance()->destroy('testTable');

This will destroy table and remove it from registry

Persistence

When you need persistence for table, you can store table to a json file.

Soon, I will develop redis and mysql persistence but for now, use default persistence :

use \Small\SwooleDb\Registry\TableRegistry;

TableRegistry::getInstance()->persist('testTable');

This will store table definition in a json file stored in /var/lib/small-swoole-db/data/testTable.json

To reload table from disk (at server restart for example) :

use \Small\SwooleDb\Registry\TableRegistry;

TableRegistry::getInstance()->loadFromChannel('testTable');

This will restore table in registry and memory with all data from last persist.

ParamRegistry

If you want, you can use ParamRegistry to change :

  • location of /var/lib direcoty :
    use Small\SwooleDb\Registry\ParamRegistry;
    use Small\SwooleDb\Registry\Enum\ParamType;
    

ParamRegistry::getInstance()->set(ParamType::varLibDir, '/home/some-user');

- data dir name :

use Small\SwooleDb\Registry\ParamRegistry; use Small\SwooleDb\Registry\Enum\ParamType;

ParamRegistry::getInstance()->set(ParamType::dataDirName, 'persistence');


In this example, the testTable table will be stored in :

/home/some-user/testTable.json


### Selector

You can use selector to build complex requests.

Basically, you can select all records :

use Small\SwooleDb\Selector\TableSelector;

$selector = new TableSelector('testSelect'); $records = $selector->execute();

foreach ($records as $record) {

echo $record['testSelect']->getValue('name');

}


You can use *where* to add conditions :

use Small\SwooleDb\Selector\TableSelector; use Small\SwooleDb\Selector\Enum\ConditionElementType; use Small\SwooleDb\Selector\Enum\ConditionOperator;

$selector = new TableSelector('testSelect'); $selector->where()

->firstCondition(new Condition(
    new ConditionElement(ConditionElementType::var, 'price', 'testSelect'),
    ConditionOperator::superior,
    new ConditionElement(ConditionElementType::const, 15)
));

$records = $selector->execute();

foreach ($records as $record) {

echo $record['testSelect']->getValue('name');

}


You can also join result throw foreign keys :

use Small\SwooleDb\Selector\TableSelector;

$result = (new TableSelector('user'))

->join('post', 'messageOwner', 'message')
->execute()

foreach ($result as $record) {

echo $record['message']->getValue('body') . ' : by ' . $record['user']->getValue('name');

}


And select any of fields (note that you can change record and persist easily) :

use Small\SwooleDb\Selector\TableSelector; use Small\SwooleDb\Selector\Enum\ConditionElementType; use Small\SwooleDb\Selector\Enum\ConditionOperator;

$selector = (new TableSelector('user'))

->join('post', 'messageOwner', 'message')

;

$selector->where()

->firstCondition(new Condition(
    new ConditionElement(ConditionElementType::var, 'name', 'user'),
    ConditionOperator::equal,
    new ConditionElement(ConditionElementType::const, 'john')
))->andCondition(new Condition(
    new ConditionElement(ConditionElementType::var, 'subject', 'message'),
    ConditionOperator::like,
    new ConditionElement(ConditionElementType::const, '%hiring%')
))

;

$result = $selector->execute();

foreach ($result as $record) {

echo $record['message']->getValue('body') . ' : by ' . $record['user']->getValue('name');

$record['message']
    ->setValue('read', 1)
    ->persist()
;

}


## Testing

You must have docker installed to run tests.

To build unit-test container and run test suite, use command :

$ bin/test --build


Once the container is build, you can quickly run test using :

$ bin/test


small/swoole-db 适用场景与选型建议

small/swoole-db 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 267 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 small/swoole-db 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 small/swoole-db 我们能提供哪些服务?
定制开发 / 二次开发

基于 small/swoole-db 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 267
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 5
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-only
  • 更新时间: 2023-02-24