审查视图

lib/DbPool.php 1.4 KB
1  
邓超 authored
1 2
<?php
1  
邓超 authored
3 4
namespace Lib;
邓超 authored
5 6 7
use Swoole\Database\PDOConfig;
use Swoole\Database\PDOPool;
1  
邓超 authored
8 9 10 11 12 13 14
/**
 * db 池
 * @author:dc
 * @time 2023/2/13 15:03
 * Class DbPool
 * @package Lib
 */
1  
邓超 authored
15 16
class DbPool {
邓超 authored
17
    use DbQuery;
1  
邓超 authored
18 19

    /**
邓超 authored
20
     * @var \Swoole\Database\PDOPool
x  
邓超 authored
21
     */
邓超 authored
22
    static $pool = null;
x  
邓超 authored
23 24 25


    /**
邓超 authored
26 27
     * 实例
     * DbPool constructor.
1  
邓超 authored
28
     */
邓超 authored
29
    public function __construct()
1  
邓超 authored
30
    {
x  
邓超 authored
31
邓超 authored
32 33 34 35 36 37 38 39 40
        if(!static::$pool){
            $pdoconfig = (new PDOConfig)
                ->withHost(DB_HOST)
                ->withPort(DB_PORT)
                ->withDbName(DB_DATABASE)
                ->withCharset('utf8mb4')
                ->withUsername(DB_USER)
                ->withPassword(DB_PASSWORD)
                ->withOptions([
1  
邓超 authored
41 42
                    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
                    \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'",
1  
邓超 authored
43
                    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
邓超 authored
44
                ]);
1  
邓超 authored
45
邓超 authored
46
            static::$pool = new PDOPool($pdoconfig,1024);
1  
邓超 authored
47
        }
1  
邓超 authored
48
邓超 authored
49 50
        // 获取链接
        $this->client = static::$pool->get();
1  
邓超 authored
51 52 53 54 55

    }


1  
邓超 authored
56 57 58 59
    /**
     * 结束
     */
    public function __destruct(){
1  
邓超 authored
60
        $this->close();
1  
邓超 authored
61 62
    }
邓超 authored
63 64 65 66 67
    /**
     * 关闭链接
     * @author:dc
     * @time 2024/5/30 10:30
     */
1  
邓超 authored
68
    public function close(){
邓超 authored
69
        self::$pool->put($this->client);
1  
邓超 authored
70 71 72
        $this->client = null;
    }
1  
邓超 authored
73 74

1  
邓超 authored
75
}