static function run() { $table = new swoole_table(1024 * 256); $table->column('index', swoole_table::TYPE_INT); $table->column('serid', swoole_table::TYPE_INT); $table->column('data', swoole_table::TYPE_STRING, 64); $table->create(); self::$table = $table; for ($i = 0; $i < self::$key_num; $i++) { $key = 'user_' . (self::$key_base + $i); $ret = self::$table->set($key, array('index' => $i, 'serid' => rand(1000, 9999), 'data' => "hello_world_{$i}")); if (!$ret) { echo "count {$i} failed."; break; } } for ($i = 0; $i < self::$worker_num; $i++) { $process = new swoole_process('UnitTest_Table::worker'); $process->start(); $workers[$i] = $process; } for ($i = 0; $i < self::$worker_num; $i++) { $exit = swoole_process::wait(); echo "worker[{$i}] exit\n"; } }
private function initSwooleTable() { $swoole_table = new \swoole_table(static::SWOOLE_TABLE_SIZE); $swoole_table->column("value", \swoole_table::TYPE_INT, 4); $swoole_table->create(); return $swoole_table; }
private function _initTable() { $this->subscribers = new \swoole_table(4096); $this->subscribers->column('channel', \swoole_table::TYPE_STRING, 128); if (!$this->subscribers->create()) { Cli::error("Failed to call swoole_table::create()."); } }
public function __construct($config) { if (empty($this->table)) { $table = new swoole_table(1024); $table->column('data', swoole_table::TYPE_STRING, 64); $table->create(); $this->table = $table; } }
function setHttpConf(array $Arr) { if ($Arr && is_array($Arr)) { $json = json_encode($Arr); $this->ConfigSWTable->set(self::HTTP_CONFIG_TABLE_KEY, [self::HTTP_CONFIG_TABLE_KEY => $json]); return true; } return false; }
/** * MasterPid命令时格式化输出 * ManagerPid命令时格式化输出 * WorkerId命令时格式化输出 * WorkerPid命令时格式化输出 * @var int */ $_maxMasterPidLength = 12; $_maxManagerPidLength = 12; $_maxWorkerIdLength = 12; $_maxWorkerPidLength = 12; /** * 创建一个websocket服务器 * 端口8888 */ $table = new swoole_table(1024); $table->column('cmd', swoole_table::TYPE_STRING, 32); $table->column('interval', swoole_table::TYPE_INT, 2); $table->column('count', swoole_table::TYPE_STRING, 10); $table->create(); $table->set('vmstat', array('cmd' => $vmstat_path, 'interval' => $interval, 'count' => $count)); $server = new swoole_websocket_server($host, $port); $server->table = $table; //将table保存在serv对象上 /** * 创建一个子进程 * 子进程创建成功后调用函数callback_vmstat */ $process = new swoole_process('callback_vmstat', true); $pid = $process->start(); /**
<?php /** * Created by PhpStorm. * User: renxiaogang * Date: 2014/12/31 * Time: 15:46 */ $table = new swoole_table(1024); var_dump($table->get('*****@*****.**')); var_dump($table->get('*****@*****.**')); var_dump($table->get('*****@*****.**'));
public function __construct() { $table = new \swoole_table(128); //$table->column('statistics', \swoole_table::TYPE_STRING, 64); $table->column('count', \swoole_table::TYPE_INT, 8); $table->column('spend', \swoole_table::TYPE_FLOAT); $table->column('get', \swoole_table::TYPE_INT, 8); $table->column('post', \swoole_table::TYPE_INT, 8); $table->column('options', \swoole_table::TYPE_INT, 8); $table->column('head', \swoole_table::TYPE_INT, 8); $table->column('put', \swoole_table::TYPE_INT, 8); $table->column('delete', \swoole_table::TYPE_INT, 8); $table->column('trace', \swoole_table::TYPE_INT, 8); $table->column('z200', \swoole_table::TYPE_INT, 8); $table->column('z201', \swoole_table::TYPE_INT, 8); $table->column('z202', \swoole_table::TYPE_INT, 8); $table->column('z204', \swoole_table::TYPE_INT, 8); $table->column('z303', \swoole_table::TYPE_INT, 8); $table->column('z404', \swoole_table::TYPE_INT, 8); $table->column('other', \swoole_table::TYPE_INT, 8); $table->create(); $this->table = $table; }
<?php $table = new swoole_table(1024); $table->column('id', swoole_table::TYPE_INT, 4); //1,2,4,8 $table->column('name', swoole_table::TYPE_STRING, 64); $table->column('num', swoole_table::TYPE_FLOAT); $table->create(); $worker = new swoole_process('child1', false, false); $worker->start(); // //child function child1($worker) { global $table; $s = microtime(true); $table->set('*****@*****.**', array('id' => 145, 'name' => 'rango', 'num' => 3.1415)); $table->set('*****@*****.**', array('id' => 358, 'name' => "Rango1234", 'num' => 3.1415)); $table->set('*****@*****.**', array('id' => 189, 'name' => 'rango3', 'num' => 3.1415)); $table->set('*****@*****.**', array('id' => 145, 'name' => 'rango', 'num' => 3.1415)); $table->set('*****@*****.**', array('id' => 358, 'name' => "Rango1234", 'num' => 3.1415)); echo "set - 5 use: " . (microtime(true) - $s) * 1000 . "ms\n"; sleep(100000); } //master sleep(1); $s = microtime(true); //for($i =0; $i < 1000; $i++) //{ // $arr = $table->get('*****@*****.**'); // var_dump($arr);
<?php /** * The script is used for simulating the usage of swoole_table() and guaranting its usability. */ $table = new swoole_table(1024); $table->column('name', swoole_table::TYPE_STRING, 64); $table->column('id', swoole_table::TYPE_INT, 4); //1,2,4,8 $table->column('num', swoole_table::TYPE_FLOAT); $table->create(); while (true) { $i = rand(1, 1000); $if = rand(0, 1); if ($if) { $table->set($i, ['id' => $i, 'name' => $i, 'num' => $i]); } else { $table->del($i); } var_dump('count ' . $table->count()); }
<?php $table = new swoole_table(1000); //table rows size $table->column('id', swoole_table::TYPE_INT, 4); //1,2,4,8 $table->column('name', swoole_table::TYPE_STRING, 64); $table->column('num', swoole_table::TYPE_FLOAT, 4); //4,8 $table->create(); exit; //memory size= 72 * (100000 + 20000) //20% conflict $key = '*****@*****.**'; $table->add($key, array('id' => 145, 'name' => 'rango', 'num' => 3.1415)); $value = $table->get($key); $table->set($key, array('id' => 120, 'num' => 1.414)); $table->del($key); $rows = $table->find(array('id' => 10), swoole_table::FIND_GT); $rows = $table->find(array('id' => 10), swoole_table::FIND_LT); $rows = $table->find(array('id' => 10), swoole_table::FIND_EQ); //default $rows = $table->find(array('id' => 10), swoole_table::FIND_NEQ); $rows = $table->find(array('name' => 'ran'), swoole_table::FIND_LEFTLIKE); $rows = $table->find(array('name' => 'go'), swoole_table::FIND_RIGHTLIKE); while ($row = $table->next()) { var_dump($row); }
public function Decr($key, $decrby = 1) { $this->table->decr($key, $decrby); }
<?php $table = new swoole_table(1024); $table->column('fd', swoole_table::TYPE_INT); $table->column('from_id', swoole_table::TYPE_INT); $table->column('data', swoole_table::TYPE_STRING, 64); $table->create(); $serv = new swoole_server('127.0.0.1', 9501); $serv->set(['dispatch_mode' => 1]); $serv->table = $table; $serv->on('connect', function ($serv, $fd, $from_id) { $info = $serv->connection_info($fd); $serv->send($fd, "INFO: fd={$fd}, from_id={$from_id}, addr={$info['remote_ip']}:{$info['remote_port']}\n"); }); $serv->on('receive', function ($serv, $fd, $from_id, $data) { $cmd = explode(" ", trim($data)); //get if ($cmd[0] == 'get') { //get self if (count($cmd) < 2) { $cmd[1] = $fd; } $get_fd = intval($cmd[1]); $info = $serv->table->get($get_fd); $serv->send($fd, var_export($info, true) . "\n"); } elseif ($cmd[0] == 'set') { $ret = $serv->table->set($fd, array('from_id' => $data, 'fd' => $fd, 'data' => $cmd[1])); if ($ret === false) { $serv->send($fd, "ERROR\n"); } else { $serv->send($fd, "OK\n");
<?php // #php test.php table_num key_num if ($argc != 3) { echo "wrong argv\r\n"; exit; } $row = $argv[1]; $key_num = $argv[2]; $table = new swoole_table($row); $table->column('name', swoole_table::TYPE_INT, 4); $table->column('count', swoole_table::TYPE_INT, 4); $table->create(); $process_num = 20; $fun = 'get_set'; for ($i = 0; $i < $process_num; $i++) { if ($fun == 'get_set') { $fun = 'set_get'; } else { $fun = 'get_set'; } $worker = new swoole_process($fun, false, false); $worker->start(); } /* $worker=new swoole_process('get_set',false,false);//正向 $worker->start(); */ //master process while (true) { for ($i = $row - 1; $i >= 0; $i--) {
*/ date_default_timezone_set("asia/shanghai"); require '../util/db.php'; require '../util/robot.php'; define('SUCCESS_CODE', 200); define('INVALID_CODE', -101); define('ERROR_CODE', 404); define('FORBID_CODE', 403); define('USERNAME_MAXLEN', 6); define('CONTENT_MAXLEN', 500); //创建websocket服务器对象,监听127.0.0.1:9009端口 $ws = new swoole_websocket_server("127.0.0.1", 9009); //机器人类 $robot = new Robot(); //在线用户详情记录 $table = new swoole_table(1024 * 10); $table->column('name', swoole_table::TYPE_STRING, 16); $table->column('fd', swoole_table::TYPE_INT); $table->column('dateline', swoole_table::TYPE_INT); $table->column('avatar', swoole_table::TYPE_INT); $table->column('ip', swoole_table::TYPE_STRING, 16); $table->create(); $ws->table_user = $table; $ws->on('open', function ($ws, $request) { $ws->push($request->fd, jsonResult('open', SUCCESS_CODE, "连接成功!\n")); }); $frames_data = array(); $ws->on('message', function ($ws, $frame) { global $robot, $frames_data; $frames_data[$frame->fd] = isset($frames_data[$frame->fd]) ? $frames_data[$frame->fd] : ''; $frames_data[$frame->fd] .= $frame->data;
<?php /** * Created by PhpStorm. * User: ClownFish * Date: 15-10-11 * Time: 下午9:16 */ $table = new swoole_table(1024); $table->column("id", swoole_table::TYPE_INT, 4); $table->column("name", swoole_table::TYPE_STRING, 64); $table->column('num', swoole_table::TYPE_FLOAT); $table->create(); $table->set('*****@*****.**', array('id' => 145, 'name' => 'rango', 'num' => 3.1415)); $process = new swoole_process('callback_function', false, false); $process->table = $table; $pid = $process->start(); echo "aa\n"; function callback_function(swoole_process $worker) { echo "bb\n"; print_r($worker->table->get("*****@*****.**")); } swoole_process::wait();
unset($dbConfig['adapter']); $class = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter; return new $class($dbConfig); }); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise */ $di->setShared('modelsMetadata', function () { return new MetaDataAdapter(); }); /** * Register the session flash service with the Twitter Bootstrap classes */ $di->set('flash', function () { return new Flash(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning')); }); /** * Start the session the first time some component request the session service */ $di->setShared('session', function () { $session = new SessionAdapter(); $session->start(); return $session; }); $di->setShared('table', function () { var_export("table shared \n"); $table = new swoole_table(1024); $table->column('param', swoole_table::TYPE_STRING, 1024); $table->create(); return $table; });
public static function init() { if (defined('SWOOLE_DEAMON')) { $table = new swoole_table(1024); $table->column('ctime', swoole_table::TYPE_INT, 4); //缓存创建时间戳 $table->column('etime', swoole_table::TYPE_INT, 4); //缓存失效时间戳 $table->column('file', swoole_table::TYPE_STRING, 64); //缓存文件路径 $table->create(); self::$table = $table; } else { self::$table = new cache\cacheModel('file'); self::$table->in('router'); } }
<?php $table = new swoole_table(1024); $table->column('name', swoole_table::TYPE_STRING, 64); $table->column('id', swoole_table::TYPE_INT, 4); //1,2,4,8 $table->column('num', swoole_table::TYPE_FLOAT); $table->create(); $table->set('*****@*****.**', array('id' => 145, 'name' => 'rango1', 'num' => 3.1415)); $table->set('*****@*****.**', array('id' => 358, 'name' => "Rango2", 'num' => 3.1415)); $table->set('*****@*****.**', array('id' => 189, 'name' => 'rango3', 'num' => 3.1415)); foreach ($table as $key => $value) { var_dump($key, $value); } echo "======================= Total Elements: {$table->count()} ============================\n"; $table->del('*****@*****.**'); // delete a exist element foreach ($table as $key => $value) { var_dump($key, $value); } echo "======================= Total Elements: {$table->count()} ============================\n"; $ret = $table->del('a invalid key'); // delete a invalid element var_dump($ret); foreach ($table as $key => $value) { var_dump($key, $value); } echo "======================= Total Elements: {$table->count()} ============================\n";
/** * MasterPid命令时格式化输出 * ManagerPid命令时格式化输出 * WorkerId命令时格式化输出 * WorkerPid命令时格式化输出 * @var int */ $_maxMasterPidLength = 12; $_maxManagerPidLength = 12; $_maxWorkerIdLength = 12; $_maxWorkerPidLength = 12; /** * 创建一个websocket服务器 * 端口8888 */ $table = new swoole_table(4); $table->column('cmd', swoole_table::TYPE_STRING, 32); $table->create(); $table->set('uptime', array('cmd' => $uptime_path)); $server = new swoole_websocket_server($host, $port); $server->table = $table; //将table保存在serv对象上 /** * websocket server配置 */ $server->set(array('worker_num' => 1, 'daemonize' => false, 'max_request' => 1000, 'dispatch_mode' => 1)); /** * websocket server start * 成功后回调 */ $server->on('start', function ($serv) use($_maxMasterPidLength, $_maxManagerPidLength, $_maxWorkerIdLength, $_maxWorkerPidLength) {
<?php $table = new swoole_table(1024); $table->column('id', swoole_table::TYPE_INT, 4); //1,2,4,8 $table->column('name', swoole_table::TYPE_STRING, 64); $table->column('num', swoole_table::TYPE_FLOAT); $table->create(); $worker = new swoole_process('child1', false, false); $worker->start(); //child function child1($worker) { global $table; $table->set('*****@*****.**', array('id' => 145, 'name' => 'rango', 'num' => 3.1415)); $table->set('*****@*****.**', array('id' => 358, 'name' => "Rango1234", 'num' => 3.1415)); $table->set('*****@*****.**', array('id' => 189, 'name' => 'rango3', 'num' => 3.1415)); sleep(100000); } //master sleep(1); $s = microtime(true); for ($i = 0; $i < 1000; $i++) { $arr = $table->get('*****@*****.**'); } echo "use: " . (microtime(true) - $s) * 1000 . "ms\n"; //var_dump($table->get('*****@*****.**')); sleep(100000);
/** * MasterPid命令时格式化输出 * ManagerPid命令时格式化输出 * WorkerId命令时格式化输出 * WorkerPid命令时格式化输出 * @var int */ $_maxMasterPidLength = 12; $_maxManagerPidLength = 12; $_maxWorkerIdLength = 12; $_maxWorkerPidLength = 12; /** * 创建一个websocket服务器 * 端口8888 */ $table = new swoole_table(4); $table->column('cmd', swoole_table::TYPE_STRING, 32); $table->column('param', swoole_table::TYPE_STRING, 4); $table->create(); $table->set('free', array('cmd' => $free_path, 'param' => $param)); $server = new swoole_websocket_server($host, $port); $server->table = $table; //将table保存在serv对象上 /** * websocket server配置 */ $server->set(array('worker_num' => 1, 'daemonize' => false, 'max_request' => 1000, 'dispatch_mode' => 1)); /** * websocket server start * 成功后回调 */