<?php // +--------------------------------------------------------------+ // // | Swoole高性能共享内存使用示例 | // // +--------------------------------------------------------------+ // // 参数为表格的最大行数,数值必须是2的N次方,否则底层会自动调整为接近的数字,且若小于1024则强制调整1024 // 提醒:最大行数和实际可存储行数是正相关但不完全一致,如本示例设置最大行数为1024,但实际最多只能存463行。 // 警告:必须根据流量和硬件情况设置参数,不能盲目设大,否则会造成内存耗尽而死机。 $table = new Swoole\Table(pow(2, 10)); // 提示:2^10=1024 // 定义表的结构 $table->column('uid', Swoole\Table::TYPE_INT); $table->column('name', Swoole\Table::TYPE_STRING, 64); // 第三个参数为字符串长度,设置行数据时超过指定数值则会自动截断 $table->column('age', Swoole\Table::TYPE_INT); // 向操作系统申请内存并创建表,如需在Server里使用Table,则Table->create()必须在Server->start()前执行 $table->create(); // 设置行数据(key/value形式) $table->set('UID_1', array('uid' => 1, 'name' => '张三', 'age' => 18)); $table->set('UID_2', array('uid' => 2, 'name' => '李四', 'age' => 19)); $table->set('UID_3', array('uid' => 3, 'name' => '王五', 'age' => 20)); // 遍历所有行数据 foreach ($table as $row) print_r($row); // 获取指定行数据 $key = 'UID_1'; if ($table->exist($key)) { var_dump($table->get($key)); // array(3) { ["uid"]=> int(1) ["name"]=> string(6) "张三" ["age"]=> int(18) } } // 删除指定行数据 $table->del($key); // 获取行数(由于上面代码删除了一行,所以最新行数为2) echo '行数:' . $table->count(); // 当前行数:2
Copyright © 2023 码农人生. All Rights Reserved