本文目录导读:

Swoole Table 是一个基于共享内存的高性能并发数据结构,非常适合在 Swoole 环境中存储和管理数据,以下是详细的使用方法:
基础使用
创建和定义表结构
<?php
// 创建表
$table = new Swoole\Table(1024); // 参数为最大行数
// 定义表结构
$table->column('id', Swoole\Table::TYPE_INT, 4); // 整数类型
$table->column('name', Swoole\Table::TYPE_STRING, 64); // 字符串类型
$table->column('score', Swoole\Table::TYPE_FLOAT); // 浮点类型
// 创建表
$table->create();
基本操作
<?php
// 设置数据
$table->set('user_1', [
'id' => 1,
'name' => '张三',
'score' => 95.5
]);
// 获取数据
$data = $table->get('user_1');
print_r($data);
// 更新数据
$table->set('user_1', [
'name' => '李四',
'score' => 98.5
]);
// 删除数据
$table->del('user_1');
// 检查键是否存在
if ($table->exist('user_1')) {
echo "存在";
}
// 获取表大小
$size = $table->count();
// 清空表
$table->destroy();
原子操作
Swoole Table 支持原子操作,适合并发场景:
<?php
$table = new Swoole\Table(1024);
$table->column('counter', Swoole\Table::TYPE_INT);
$table->column('string_data', Swoole\Table::TYPE_STRING, 64);
$table->create();
// 原子自增
$table->incr('counter', 'counter', 1); // key, column, step
// 原子自减
$table->decr('counter', 'counter', 1);
// 原子替换字符串(需要 TYPE_STRING 类型)
$table->set('str', ['string_data' => 'hello']);
$table->set('str', ['string_data' => 'world']); // 直接覆盖
协程环境中的使用
<?php
use Swoole\Coroutine;
use Swoole\Table;
$table = new Table(1024);
$table->column('data', Table::TYPE_STRING, 256);
$table->create();
// 在协程中并发操作
Coroutine\run(function () use ($table) {
// 创建多个协程并发写入
for ($i = 0; $i < 10; $i++) {
Coroutine::create(function () use ($table, $i) {
$table->set("key_$i", ['data' => "value_$i"]);
echo "写入 key_$i\n";
});
}
// 等待所有写入完成
Co::sleep(0.1);
// 读取数据
for ($i = 0; $i < 10; $i++) {
$result = $table->get("key_$i");
if ($result) {
echo "读取 key_$i: " . $result['data'] . "\n";
}
}
});
实际应用示例
示例:在线用户管理
<?php
class OnlineUserManager
{
private $table;
public function __construct()
{
// 创建用户表
$this->table = new Swoole\Table(1024);
$this->table->column('user_id', Swoole\Table::TYPE_INT);
$this->table->column('username', Swoole\Table::TYPE_STRING, 64);
$this->table->column('login_time', Swoole\Table::TYPE_INT);
$this->table->column('last_active', Swoole\Table::TYPE_INT);
$this->table->column('ip', Swoole\Table::TYPE_STRING, 45);
$this->table->create();
}
public function userLogin($userId, $username, $ip)
{
$time = time();
$this->table->set($userId, [
'user_id' => $userId,
'username' => $username,
'login_time' => $time,
'last_active' => $time,
'ip' => $ip
]);
}
public function updateActivity($userId)
{
if ($this->table->exist($userId)) {
$this->table->set($userId, [
'last_active' => time()
]);
}
}
public function userLogout($userId)
{
$this->table->del($userId);
}
public function getUserInfo($userId)
{
return $this->table->get($userId);
}
public function getOnlineCount()
{
return $this->table->count();
}
public function getOnlineUsers()
{
$users = [];
foreach ($this->table as $key => $value) {
$users[] = $value;
}
return $users;
}
}
// 使用示例
$manager = new OnlineUserManager();
$manager->userLogin(1, '张三', '192.168.1.100');
$manager->userLogin(2, '李四', '192.168.1.101');
echo "在线用户数: " . $manager->getOnlineCount() . PHP_EOL;
$info = $manager->getUserInfo(1);
echo "用户信息: " . json_encode($info) . PHP_EOL;
print_r($manager->getOnlineUsers());
示例:简单计数器服务
<?php
class CounterService
{
private $table;
public function __construct()
{
$this->table = new Swoole\Table(1024);
$this->table->column('count', Swoole\Table::TYPE_INT);
$this->table->create();
// 初始化
$this->table->set('total', ['count' => 0]);
}
public function increment()
{
return $this->table->incr('total', 'count', 1);
}
public function getCount()
{
$result = $this->table->get('total');
return $result ? $result['count'] : 0;
}
public function reset()
{
$this->table->set('total', ['count' => 0]);
}
}
// 在 Swoole 服务中使用
$server = new Swoole\Http\Server('0.0.0.0', 9501);
$counter = new CounterService();
$server->on('Request', function ($request, $response) use ($counter) {
$count = $counter->increment();
$response->end("当前计数: $count");
});
$server->start();
注意事项
- 内存限制:Table 的内存是在共享内存中分配的,需要合理设置大小
- 行数限制:创建时需要指定最大行数,超出后会覆盖旧数据(取决于
conflict_proportion) - 数据类型:支持 INT、STRING、FLOAT 三种类型
- 字符串长度:STRING 类型需要指定最大长度
- 性能特点:TABLE 适用于小数据量的快速读写,不适合存储大型数据
高级配置
<?php
// 参数说明
$table = new Swoole\Table(
1024, // 最大行数
0.5, // conflict_proportion 冲突比例,默认 0.5
0, // 内存大小(单位:字节),0 表示自动计算
4 // 每个 Key 的额外内存
);
$table->column('data1', Swoole\Table::TYPE_STRING, 128);
$table->create();
这种高性能的内存表非常适合用于用户 session、计数器、缓存等场景,能显著提升应用性能。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播