当前位置:网站首页>SWOOLE高性能内存数据库的使用和配置教程
SWOOLE高性能内存数据库的使用和配置教程
2022-04-21 23:04:00 【CRMEB众邦科技】
使用环境:thnkphp6.* + think-swoolev4.0
说明
由于 PHP 语言不支持多线程,因此 Swoole 使用多进程模式,在多进程模式下存在进程内存隔离,在工作进程内修改 global 全局变量和超全局变量时,在其他进程是无效的。
对应的解决方案有:
1. 使用Redis数据库、关系型数据库Mysql
2. 内存文件/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才能生效
使用场景
1. socket 通讯记录fd和用户的绑定关系
2. 当作缓存来全局读取记录数据
3. 可以当作计数器使用,原子级别。不会存在并发的问题框架中如何使用
use think\swoole\Table;use Swoole\Table as SwooleTable;//拿到实例化后的table对象$make = app()->make(Table::class);//获取user表示例/** @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://my.oschina.net/u/5168284/blog/5517164
边栏推荐
- 2、Failed to connect to MySQL Server 8.0.28 after 10 attempts
- 自建vnc类软件注意事项
- VOS6. 0 installation and source code command
- Cleaning robot - infrared down looking sensor to detect whether there is a sudden change in ground distance
- 音视频基本概念和FFmpeg的简单入门
- Dynamic programming: complete knapsack problem
- Implementation of neural network attention mechanism by pytoch code
- 方法的重载
- April 22, 2022 Daily: a new face attribute editing framework based on transformer
- Is it really safe to outsource model training? New research: outsourcers may implant backdoors to control bank lending
猜你喜欢

2、Failed to connect to MySQL Server 8.0.28 after 10 attempts

Implementation of neural network attention mechanism by pytoch code

Apache Flink系列-④有状态函数

循环队列与扩容

OS实验三【进程通信】

将模型训练外包真的安全吗?新研究:外包商可能植入后门,控制银行放款

Comparison between member variables and local variables

7.4 introduction to robot kinematics | manipulator kinematics | two wheel differential chassis kinematics | wheel odometer

AI application theory - special production and manufacturing session (parts installation and quality inspection)

4. MySQL workbench create access user
随机推荐
[Central South University of forestry science and technology] [Chen] ninth week operation triangle exception handling
PHP array function extract usage details
"Inner universe" revolution link
YARN线上动态资源调优
Kubernetes -- configmap configuration management
YARN线上动态资源调优
AI application theory - special production and manufacturing session (parts installation and quality inspection)
[MQ] starting from scratch to realize mq-01-start of producers and consumers
1. MySQL workbench 8.0 installation
Detailed explanation of MySQL concurrency parameter adjustment
2022電視盒子加購榜排名,超一多半的用戶選購當貝盒子
【matlab中一些小技巧和快捷键使用总结】
1141: C language training - a hundred dollars and a hundred chickens_ Pruning cycle
Wiki. JS configure LDAP authentication
2022 hoisting machinery command test question simulation test question bank and answers
[sdoi2012] Chandelier
长沙好人
[RL] deeply understand the use of gradient descent in tabular learning (MC / TD)
Go language self-study series | golang type definition and type alias
循环队列与扩容