<?php namespace Lib; use Swoole\Database\RedisConfig; /** * redis 链接池 * @author:dc * @time 2023/2/10 17:04 * Class RedisPool */ class RedisPool { use RedisQuery; /** * @var \Swoole\Database\RedisPool */ static $pool = null; /** * 获取到的连接 * @var array */ public static $clientAll = []; /** * 连接的数量 * @var int */ public static $clientNumber = 1024; /** * RedisPool constructor. */ public function __construct() { if(empty(static::$pool)){ static::$pool = new \Swoole\Database\RedisPool((new RedisConfig) ->withHost(REDIS_HOST) ->withPort(REDIS_PORT) ->withAuth(REDIS_PASSWORD) ->withDbIndex(REDIS_DB) ->withTimeout(60) ,self::$clientNumber ); } } public function getClient() { $id = \co::getCid(); if(empty(static::$clientAll[$id])){ static::$clientAll[$id] = self::$pool->get(); } return static::$clientAll[$id]; } public function __destruct() { // $this->close(); } /** * 关闭链接 * @author:dc * @time 2024/5/30 10:30 */ public function close(){ $id = \co::getCid(); if (isset(static::$clientAll[$id])){ self::$pool->put(static::$clientAll[$id]); } unset(static::$clientAll[$id]); } }