redis 初始化 php 类,workerman onWorkerStart初始化redis连接疑问

这使用workerman的gateway框架的时候遇到的疑问

Events.php

/**

* This file is part of workerman.

*

* Licensed under The MIT License

* For full copyright and license information, please see the MIT-LICENSE.txt

* Redistributions of files must retain the above copyright notice.

*

* @author walkor

* @copyright walkor

* @link http://www.workerman.net/

* @license http://www.opensource.org/licenses/mit-license.php MIT License

*/

/**

* 用于检测业务代码死循环或者长时间阻塞等问题

* 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload

* 然后观察一段时间workerman.log看是否有process_timeout异常

*/

//declare(ticks=1);

use \GatewayWorker\Lib\Gateway;

use Workerman\Lib\Timer;

use \Server\Common;

use \Server\Cmd;

/**

* 主逻辑

* 主要是处理 onConnect onMessage onClose 三个方法

* onConnect 和 onClose 如果不需要可以不用实现并删除

*/

class Events

{

public static $redisCon = null ;

/**

* 当客户端连接时触发

* 如果业务不需此回调可以删除onConnect

*

* @param int $client_id 连接id

*/

public static function onConnect($client_id ) {

}

/**

* 当客户端发来消息时触发

* @param int $client_id 连接id

* @param mixed $message 具体消息

*/

public static function onMessage($client_id, $message) {

}

public static function onWorkerStart($businessWorker){

self::initRedis();

}

/**

* 当用户断开连接时触发

* @param int $client_id 连接id

*/

public static function onClose($client_id) {

echo "client_id is $client_id is close \n";

}

//redis的初始化

public static function initRedis(){

$redisConfig = require_once __DIR__ . "/Server/Config/Redis.config.php";

self::$redisCon = Server\Library\RedisDB::getInstance( $redisConfig , 0 );

}

}

RedisDB.php

namespace Server\Library ;

class RedisDB

{

static $_instance; //存储对象

public $handler;

private $config = null ;

public function __construct($redisConfig , $dbindex = 0){

if (!extension_loaded('redis')) {

throw new Exception("REDIS NOT SUPPORT", 1);

}

$this->handler = new \Redis();

//从配置读取

$this->handler->pconnect($redisConfig['hostname'], $redisConfig['port']);

$this->handler->auth($redisConfig['auth']);

}

public static function getInstance($redisConfig , $index = 0)

{

if (!isset(self::$_instance[$index]) or FALSE == (self::$_instance[$index] instanceof self)) {

self::$_instance[$index] = new self($redisConfig , $index);

}

return self::$_instance[$index];

}

}

start_businessworker.php

我开启了

$worker->count = 8;

疑问:

这初始化redis的连接的时候,我发现会new redis连接8次

id=1017 addr=192.168.2.188:56012 fd=9 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth

id=1018 addr=192.168.2.188:56013 fd=10 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth

id=1019 addr=192.168.2.188:56016 fd=11 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth

id=1020 addr=192.168.2.188:56025 fd=12 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth

id=1021 addr=192.168.2.188:56024 fd=13 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth

id=1022 addr=192.168.2.188:56028 fd=14 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth

id=1023 addr=192.168.2.188:56038 fd=15 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth

id=1024 addr=192.168.2.188:56039 fd=16 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth

但是我上面 初始化连接之后只是赋给一个类的静态变量。这样的话 那岂不是只有1个连接????

难道根据worker的id进行吗?

请指教

上面我redis连接使用了单例的模式!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值