当前位置:网站首页>SWOOLE高性能内存数据库的使用和配置教程
SWOOLE高性能内存数据库的使用和配置教程
2022-04-22 09:59:00 【CRMEB】
使用环境:thinkphp6.* + think-swoolev4.0
说明
由于 PHP 语言不支持多线程,因此 Swoole 使用多进程模式,在多进程模式下存在进程内存隔离,在工作进程内修改 global 全局变量和超全局变量时,在其他进程是无效的。
对应的解决方案有:
- 使用Redis数据库、关系型数据库Mysql
- 内存文件/dev/shm
首先数据库的操作都牵扯到IOD等待时间,因此推荐使用Table
属性说明
size 获取表格的最大行数
Swoole\Table->size;
memorySize
获取实际占用内存的尺寸,单位为字节
可以使用属性获取当前占用内存,利于调整配置。以免出现 :Unable to allocate memory 错误
Swoole\Table->memorySize;
配置
文件位置:config/swoole.php
return [
'tables' => [
//高性能内存数据库
'user' => [
'size' => 20480, //指定数据库内存大小
'columns' => [
['name' => 'fd', 'type' => Table::TYPE_INT],//内置字段,自行设置
['name' => 'type', 'type' => Table::TYPE_INT],
['name' => 'uid', 'type' => Table::TYPE_INT,'size'=>1024],
['name' => 'to_uid', 'type' => Table::TYPE_INT],
['name' => 'tourist', 'type' => Table::TYPE_INT]
]
]
],
];
需要在配置文件中增加如上配置:user 为表名,size 为数据库大小,columns 为数据库字段
可以添加多个数据库
注意事项:
size的代大小需要根据自身的使用情况而定,过小无法自动扩容的。需要重启服务。
修改内存数据库必须重启swoole才能生效
使用场景
- socket 通讯记录fd和用户的绑定关系
- 当作缓存来全局读取记录数据
- 可以当作计数器使用,原子级别。不会存在并发的问题框架中如何使用
use think\swoole\Table;
use Swoole\Table as SwooleTable;
//拿到实例化后的table对象
$make = app()->make(Table::class);
//获取user表示例
[添加链接描述](https://q.crmeb.com)/** @var SwooleTable $table **/
$table = $make->get('user');
//设置数据
$table->set('1',['fd'=>123,'type'=>1,'uid'=>1,'to_uid'=>0,'tourist'=>0]);
//读取key=1的数据
$table->get('1');
foreach ($table as $key => $value) {
var_dump($key);//设置的key
var_dump($value);//设置的value数据
}
最后
如果你觉得这篇文章对你有点用的话,麻烦请给我们的开源项目点点star:http://github.crmeb.net/u/defu不胜感激 !
免费获取源码地址:http://www.crmeb.com
PHP学习手册:https://doc.crmeb.com
技术交流论坛:https://q.crmeb.com
版权声明
本文为[CRMEB]所创,转载请带上原文链接,感谢
https://crmeb.blog.csdn.net/article/details/124337500
边栏推荐
- Leapftp: 425 failed to establish connection solution
- QT AxObject库的简单操作
- MySQL multi instance installation method I
- JS clich é about this, constructor, prototype
- Get browser URL
- 基于Transform的深度学习目标检测(DETR模型)
- 分表和分区的区别
- L3-007 ladder map (30 points) (condition DIJ)
- How to realize synchronous monitoring of etcd
- leapftp:425 failed to establish connection解决方法
猜你喜欢

Aardio - 【库】webp图片转换

Development of esp-01s in Arduino (1)

Tensorflow Experiment 4 -- Boston house price forecast

Various location codes applied in transformer model

Beyond iterm! Known as the next generation terminal artifact, powerful!

Oracle 11.0 under Windows Upgrade from 2.0.1 to Oracle 11.0 2.0.4

浙政钉扫码登录

二极管工作原理

辐射度量学基本概念
![[QT notes] use qpainter to draw various basic graphics](/img/7a/78e96d8689582d145586a4da3d3bb0.png)
[QT notes] use qpainter to draw various basic graphics
随机推荐
Leapftp: 425 failed to establish connection solution
QTabelWidget实例
获取浏览器网址 地址
SQL 创建数据库
P8 data broadcasting
Biden announced another $800 million in security assistance to Ukraine
Shell gets random number and echo - E '\ 033 [word background color; font color M string \ 033 [0m'
Heap overflow of kernel PWN basic tutorial
Linux 7 silent installation of Oracle 11g
WEB应用扫码获取浙政钉用户信息
Intermediary model
超越 iTerm!号称下一代 Terminal 终端神器,用完爱不释手!
Cross platform compilation
2022-04-21 MySQL InnoDB storage engine core processing
Beyond iterm! Known as the next generation terminal artifact, powerful!
oracle19. 3 upgrade to 19.6
DA14580BLE点亮LED
Basic concepts of radiometry
Ukui3 supports openeuler 22.03 LTS
[age appropriate friends of interesting programming questions] (detailed explanation of the original title of leetcode)