signed

QiShunwang

“诚信为本、客户至上”

swoole4-swoole创建Mysql连接池

2020/12/27 23:27:44   来源:

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>connectionobj[]=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 $avil_connection_num = 20;//可用连接 //构造方法连接mysql,创建20mysql连接 private function __construct() { for($i=0;$i<$this->connection_num;$i++){ $dsn = "mysql:host=127.0.0.1;dbname=swoole"; $this->connection_obj[] = new Pdo($dsn,'root','rootmysql123'); } } private function __clone() { // TODO: Implement __clone() method. } public static function getInstance() { if(is_null(self::$instance)){ self::$instance = new self(); } return self::$instance; } //执行sql操作 public function query($sql) { if($this->avil_connection_num==0){ throw new Exception("暂时没有可用的连接诶,请稍后"); } //执行sql语句 $pdo = array_pop($this->connection_obj); //可用连接数减1 $this->avil_connection_num --; //使用从连接池中取出的mysql连接执行查询,并且把数据取成关联数组 $rows = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC); //把mysql连接放回连接池,可用连接数+1 array_push($this->connection_obj,$pdo); $this->avil_connection_num ++; return $rows; } } //创建swool的http服务器对象 $serv = new swoole_http_server('0.0.0.0',8000); //当浏览器链接点这个http服务器的时候,向浏览器发送helloworld $serv->on('request', function($request,$response){ //$request包含这个请求的所有信息,比如参数 //$response包含返回给浏览器的所有信息,比如helloworld //向浏览器发送helloworld $stop = false; while (!$stop){ try{ $sql = "SELECT * FROM user ORDER BY id DESC LIMIT 5"; $rows = MysqlConnectionPool::getInstance()->query($sql); $response->end(json_encode($rows)); $stop = true; }catch (Exception $e){ usleep(100000); } } }); //启动http服务器 $serv->start();**