ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

swoole4-swoole创建Mysql连接池

2020-12-27 23:32:24  阅读:353  来源: 互联网

标签:function serv http request private swoole4 connection Mysql 连接池


swoole4-swoole创建Mysql连接池

<?php
/

  • Created by PhpStorm.
  • User: Administrator
  • Date: 2018/11/20
  • Time: 14:12
    /
    //编写mysql连接池,这个类只能被实例化一次(单例)
    class MysqlConnectionPool
    {
    private static $instance;//单例对象
    private $connection_num = 10;//连接数量
    private KaTeX parse error: Expected group after '_' at position 67: …ivate function _̲_construct() {…i=0; i < i< i<this->connection_num;$i++){
    $dsn = “mysql:host=127.0.0.1;dbnane=swoole”;
    t h i s − > c o n n e c t i o n o b j [ ] = n e w P d o ( this->connection_obj[] = new Pdo( this−>connectiono​bj[]=newPdo(dsn,‘root’,‘rootmysql123’);
    }
    }
    private function __clone()
    {
    // TODO: Implement __clone() method.
    }
    public static function getInstance()
    {
    if(is_null(self::KaTeX parse error: Expected '}', got 'EOF' at end of input: … self::instance = new self();
    }
    }
    }
    MysqlConnectionPool::getInstance();
    //创建swool的http服务器对象
    $serv = new swoole_http_server(‘0.0.0.0’,8000);
    //当浏览器链接点这个http服务器的时候,向浏览器发送helloworld
    s e r v − > o n ( ′ r e q u e s t ′ , f u n c t i o n ( serv->on('request', function( serv−>on(′request′,function(request,KaTeX parse error: Expected '}', got 'EOF' at end of input: response){ //request包含这个请求的所有信息,比如参数
    //$response包含返回给浏览器的所有信息,比如helloworld
    //(2.3)向浏览器发送helloworld
    $response->end(“hello world”);
    });
    //启动http服务器
    $serv->start();
    *

效果图在这里插入图片描述
**(4)完善mysql连接池
<?php
/

  • Created by PhpStorm.
  • User: Administrator
  • Date: 2018/11/20
  • Time: 14:12
    /
    //编写mysql连接池,这个类只能被实例化一次(单例)
    class MysqlConnectionPool
    {
    private static $instance;//单例对象
    private $connection_num = 20;//连接数量
    private $connection_obj = [];
    private KaTeX parse error: Expected group after '_' at position 78: …ivate function _̲_construct() {…i=0; i < i< i<this->connection_num;$i++){
    $dsn = “mysql:host=127.0.0.1;dbname=swoole”;
    t h i s − > c o n n e c t i o n o b j [ ] = n e w P d o ( this->connection_obj[] = new Pdo( this−>connectiono​bj[]=newPdo(dsn,‘root’,‘rootmysql123’);
    }
    }
    private function __clone()
    {
    // TODO: Implement __clone() method.
    }
    public static function getInstance()
    {
    if(is_null(self::KaTeX parse error: Expected '}', got 'EOF' at end of input: … self::instance = new self();
    }
    return self::KaTeX parse error: Expected 'EOF', got '}' at position 12: instance; }̲ //执行sql操作 pu…sql)
    {
    if($this->avil_connection_num==0){
    throw new Exception(“暂时没有可用的连接诶,请稍后”);
    }
    //执行sql语句
    p d o = a r r a y p o p ( pdo = array_pop( pdo=arrayp​op(this->connection_obj);
    //可用连接数减1
    $this->avil_connection_num --;
    //使用从连接池中取出的mysql连接执行查询,并且把数据取成关联数组
    $rows = p d o − > q u e r y ( pdo->query( pdo−>query(sql)->fetchAll(PDO::FETCH_ASSOC);
    //把mysql连接放回连接池,可用连接数+1
    array_push( t h i s − > c o n n e c t i o n o b j , this->connection_obj, this−>connectiono​bj,pdo);
    $this->avil_connection_num ++;
    return $rows;
    }
    }
    //创建swool的http服务器对象
    $serv = new swoole_http_server(‘0.0.0.0’,8000);
    //当浏览器链接点这个http服务器的时候,向浏览器发送helloworld
    s e r v − > o n ( ′ r e q u e s t ′ , f u n c t i o n ( serv->on('request', function( serv−>on(′request′,function(request,KaTeX parse error: Expected '}', got 'EOF' at end of input: response){ //request包含这个请求的所有信息,比如参数
    //$response包含返回给浏览器的所有信息,比如helloworld
    //向浏览器发送helloworld
    s t o p = f a l s e ; w h i l e ( ! stop = false; while (! stop=false;while(!stop){
    try{
    $sql = “SELECT * FROM user ORDER BY id DESC LIMIT 5”;
    r o w s = M y s q l C o n n e c t i o n P o o l : : g e t I n s t a n c e ( ) − > q u e r y ( rows = MysqlConnectionPool::getInstance()->query( rows=MysqlConnectionPool::getInstance()−>query(sql);
    r e s p o n s e − > e n d ( j s o n e n c o d e ( response->end(json_encode( response−>end(jsone​ncode(rows));
    $stop = true;
    }catch (Exception $e){
    usleep(100000);
    }
    }
    });
    //启动http服务器
    $serv->start();
    ***

标签:function,serv,http,request,private,swoole4,connection,Mysql,连接池
来源: https://blog.csdn.net/bsh2810/article/details/111828422

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有