审查视图

lib/RedisPool.php 1.1 KB
1  
邓超 authored
1 2
<?php
1  
邓超 authored
3 4
namespace Lib;
邓超 authored
5 6
use Swoole\Database\RedisConfig;
1  
邓超 authored
7
/**
邓超 authored
8
 * redis 链接池
1  
邓超 authored
9 10 11 12 13 14
 * @author:dc
 * @time 2023/2/10 17:04
 * Class RedisPool
 */
class RedisPool {
邓超 authored
15
    use RedisQuery;
1  
邓超 authored
16 17

    /**
邓超 authored
18
     * @var \Swoole\Database\RedisPool
1  
邓超 authored
19
     */
邓超 authored
20
    static $pool = null;
1  
邓超 authored
21
x  
邓超 authored
22 23

    /**
邓超 authored
24
     * RedisPool constructor.
x  
邓超 authored
25
     */
邓超 authored
26
    public function __construct()
1  
邓超 authored
27
    {
邓超 authored
28 29 30 31 32 33 34
        if(empty(static::$pool)){

            static::$pool = new \Swoole\Database\RedisPool((new RedisConfig)
                ->withHost(REDIS_HOST)
                ->withPort(REDIS_PORT)
                ->withAuth(REDIS_PASSWORD)
                ->withDbIndex(REDIS_DB)
x  
邓超 authored
35
                ->withTimeout(60)
邓超 authored
36
                ,1024
1  
邓超 authored
37
            );
1  
邓超 authored
38
1  
邓超 authored
39
        }
1  
邓超 authored
40
邓超 authored
41 42
        // 获取一个连接,放入当前实例
        $this->client = static::$pool->get();
1  
邓超 authored
43 44
    }
1  
邓超 authored
45 46 47 48 49



    public function __destruct()
    {
1  
邓超 authored
50
       $this->close();
1  
邓超 authored
51 52 53 54
    }


    /**
1  
邓超 authored
55 56 57 58 59
     * 关闭
     * @author:dc
     * @time 2023/3/16 13:42
     */
    public function close(){
1  
邓超 authored
60
邓超 authored
61
        self::$pool->put($this->client);
1  
邓超 authored
62
1  
邓超 authored
63
        $this->client = null;
x  
邓超 authored
64
1  
邓超 authored
65
    }
1  
邓超 authored
66 67 68 69 70 71 72 73







}