繼上篇 2018年swoole實戰(zhàn)7-進(jìn)程詳解
本篇演示 swoole內(nèi)存操作模塊
swoole_table一個基于共享內(nèi)存和鎖實現(xiàn)的超高性能,并發(fā)數(shù)據(jù)結(jié)構(gòu)。用于解決多進(jìn)程/多線程數(shù)據(jù)共享和同步加鎖問題。既然稱之為table, 就像表格一個由行與列組成,這點(diǎn)與mysql的數(shù)據(jù)表類似
以下演示內(nèi)存table的基礎(chǔ)操作
新建 table.php
<?php
// 創(chuàng)建內(nèi)存表
$table = new swoole_table(1024);
// 在內(nèi)存表中增加列
$table->column('id', $table::TYPE_INT, 4);
$table->column('name', $table::TYPE_STRING, 64);
$table->column('age', $table::TYPE_INT, 3);
$table->create();
// 增加一行記錄
$table->set('one', ['id' => 1, 'name' => '章北海', 'age' => 30]);
// 另一種寫法
$table['two'] = [
'id' => 2,
'name' => '羅輯',
'age' => 33,
];
// 獲取記錄
$one = $table->get('one');
$two = $table->get('two');
var_dump($one);
var_dump($two);
// 數(shù)據(jù)自減
$table->decr('one', 'age', 2);
$one = $table->get('one');
var_dump($one);
// 刪除數(shù)據(jù)
$table->del('one');
$one = $table->get('one');
var_dump($one);
執(zhí)行結(jié)果:
? memory php table.php
array(3) {
["id"]=>
int(1)
["name"]=>
string(9) "章北海"
["age"]=>
int(30)
}
array(3) {
["id"]=>
int(2)
["name"]=>
string(6) "羅輯"
["age"]=>
int(33)
}
array(3) {
["id"]=>
int(1)
["name"]=>
string(9) "章北海"
["age"]=>
int(28)
}
bool(false)
如果覺得本文對你有所幫助,點(diǎn)個贊,或者賞杯咖啡錢,你的認(rèn)可對我很重要