当前位置: 首页 > news >正文

wordpress多站点是什么网站开发外包合同范本

wordpress多站点是什么,网站开发外包合同范本,海外seo网站推广,3d云打印网站开发如何在 Swoole 中优雅的实现 MySQL 连接池一、为什么需要连接池 #xff1f;数据库连接池指的是程序和数据库之间保持一定数量的连接不断开#xff0c;并且各个请求的连接可以相互复用#xff0c;减少重复连接数据库带来的资源消耗#xff0c;一定程度上提高了程序的并发性…如何在 Swoole 中优雅的实现 MySQL 连接池一、为什么需要连接池 数据库连接池指的是程序和数据库之间保持一定数量的连接不断开并且各个请求的连接可以相互复用减少重复连接数据库带来的资源消耗一定程度上提高了程序的并发性能。二、连接池实现要点协程使用 MySQL 协程客户端。使用 MySQL 协程客户端是为了能在一个 Worker 阻塞的时候让出 CPU 时间片去处理其他的请求提高整个 Worker 的并发能力。连接池存储介质使用 \swoole\coroutine\channel 通道。使用 channel 能够设置等待时间等待其他的请求释放连接。并且在等待期间同样也可以让出 CPU 时间片去处理其他的请求。假设选择 array 或 splqueue无法等待其他的请求释放连接。那么在高并发下的场景下可能会出现连接池为空的现象。如果连接池为空了那么 pop 就直接返回 null 了导致连接不可用。注因此不建议选择 array 或 splqueue。三、连接池的具体实现use Swoole\Coroutine\Channel;use Swoole\Coroutine\MySQL;class MysqlPool{private $min; // 最小连接数private $max; // 最大连接数private $count; // 当前连接数private $connections; // 连接池protected $freeTime; // 用于空闲连接回收判断public static $instance;/*** MysqlPool constructor.*/public function __construct(){$this-min 10;$this-max 100;$this-freeTime 10 * 3600;$this-connections new Channel($this-max 1);}/*** return MysqlPool*/public static function getInstance(){if (is_null(self::$instance)) {self::$instance new self();}return self::$instance;}/*** 创建连接* return MySQL*/protected function createConnection(){$conn new MySQL();$conn-connect([host mysql,port 3306,user root,password root,database fastadmin,timeout 5]);return $conn;}/*** 创建连接对象* return array|null*/protected function createConnObject(){$conn $this-createConnection();return $conn ? [last_used_time time(), conn $conn] : null;}/*** 初始化连接* return $this*/public function init(){for ($i 0; $i $this-min; $i) {$obj $this-createConnObject();$this-count;$this-connections-push($obj);}return $this;}/*** 获取连接* param int $timeout* return mixed*/public function getConn($timeout 3){if ($this-connections-isEmpty()) {if ($this-count $this-max) {$this-count;$obj $this-createConnObject();} else {$obj $this-connections-pop($timeout);}} else {$obj $this-connections-pop($timeout);}return $obj[conn]-connected ? $obj[conn] : $this-getConn();}/*** 回收连接* param $conn*/public function recycle($conn){if ($conn-connected) {$this-connections-push([last_used_time time(), conn $conn]);}}/*** 回收空闲连接*/public function recycleFreeConnection(){// 每 2 分钟检测一下空闲连接swoole_timer_tick(2 * 60 * 1000, function () {if ($this-connections-length() intval($this-max * 0.5)) {// 请求连接数还比较多暂时不回收空闲连接return;}while (true) {if ($this-connections-isEmpty()) {break;}$connObj $this-connections-pop(0.001);$nowTime time();$lastUsedTime $connObj[last_used_time];// 当前连接数大于最小的连接数并且回收掉空闲的连接if ($this-count $this-min ($nowTime - $lastUsedTime $this-freeTime)) {$connObj[conn]-close();$this-count--;} else {$this-connections-push($connObj);}}});}}$httpServer new swoole_http_server(127.0.0.1,9501);$httpServer-set([work_num 1]);$httpServer-on(WorkerStart, function ($request, $response) {MysqlPool::getInstance()-init()-recycleFreeConnection();});$httpServer-on(Request, function ($request, $response){$conn MysqlPool::getInstance()-getConn();$conn-query(SELECT * FROM fa_admin WHERE id1);MysqlPool::getInstance()-recycle($conn);});$httpServer-start();四、总结定时维护空闲连接到最小值。使用用完数据库连接之后需要手动回收连接到连接池。使用 channel 作为连接池的存储介质。
http://www.zqtcl.cn/news/495962/

相关文章:

  • 深圳做二类学分的网站开发一平方米多少钱
  • 如何做原创小说网站建一个o2o网站
  • 东莞市住房建设网站互动科技 网站建设
  • 淄博网站建设高端网络seo线上培训多少钱
  • s网站优化工地模板图片
  • 手机网站使用微信支付神级网页设计网站
  • 网站建站大约多少钱如何引流被动加好友
  • 哪些网站可以查企业信息大城县有做网站的吗
  • 上海网站建设电影联wordpress 分类title
  • 杭州网站建设招标免费seo排名优化
  • 网站建设服务费是否无形资产百度一下你就知道官网下载安装
  • 网站付款链接怎么做在线设计商标logo
  • 阿里巴巴做网站多少钱特大新闻凌晨刚刚发生
  • 网站如何做se设计师网站pintset
  • 上海网站制作机构wordpress 优酷免广告
  • 关于网站建设的名言网站开发的技术难点
  • 免费云建站廊坊seo外包
  • 个人网站建设方案书用备案的衡水市网站制作
  • 教育网站的建设品牌营销型网站作用
  • 金凤区建设交通局网站做洗衣液的企业网站
  • 南阳网站优化手机咋做网站
  • 做网站多少钱一年没有网站做cpa怎么赚钱
  • 二手房发布网站怎么做建站哪家好用兴田德润
  • 网站开发有几种深圳网站制作长沙
  • 为什么一个网站外链那么多公司团建活动
  • 公司门户网站建设策划书wordpress清空数据
  • 大兴专注高端网站建设交互设计留学
  • 想要黑掉一个网站 要怎么做网页设计师培训机构有吗
  • 做网站网站应该注意什么关于建设网站的会议纪要
  • 什么网站建设最简单做毕业设计实物的网站