public function __construct($config) { // 读取配置文件 $this->config = $config; // 检测配置文件 if ($this->checkConfigure()) { return Console::error(get_class($this) . ' [ ', $k, ' ] configure must'); } // 连接缓存 $this->connect(); }
/** * 连接 * @return [type] [description] */ public function connect() { // 判断是否存在redis if (!class_exists('swoole_table')) { return Console::error("Class swoole_table not exists"); } $this->cache_object = new \swoole_table($this->config['dbsize']); $this->cache_object->column('time', \swoole_table::TYPE_INT, 4); //1,2,4,8 $this->cache_object->column('data', \swoole_table::TYPE_STRING, 65535); $this->cache_object->create(); }
/** * 连接 * @return [type] [description] */ public function connect() { // 判断是否存在redis if (!class_exists('Redis')) { return Console::error("Class Redis not exists"); } $this->cache_object = new \Redis(); if ($this->cache_object->connect($this->config['host'], $this->config['port'])) { if (isset($this->config['auth']) && !empty($this->config['auth'])) { $this->cache_object->auth($this->config['auth']); } } else { return Console::error("Redis Connect fail"); } }
/** * 连接 * @return [type] [description] */ public function connect() { // 判断是否存在redis if (!class_exists('MongoClient')) { return Console::error("Class MongoClient not exists"); } // 实例化mongoclient try { $this->cache_object = new \MongoClient($this->config['server'], array('connect' => true)); // 获取db和collection $db = $this->config['db']; $collection = $this->config['collection']; $this->mongo = $this->cache_object->{$db}->{$collection}; } catch (MongoConnectionException $e) { return Console::error("Mongo Connect fail", $e->getMessage()); } }
/** * 关闭连接回调 * @param \swoole_server $serv swoole_server 对象 * @param intval $fd TCP客户端连接的文件描述符 * @param intval $from_id TCP连接所在的Reactor线程ID * @return null */ public function onClose(\swoole_server $serv, $fd, $from_id) { Console::close(str_pad('客户端连接「Fd # ' . $fd . '」「Fromid # ' . $from_id . '」', 58, ' ', STR_PAD_RIGHT), '断开成功'); Console::info(str_repeat('-', 57)); }
public function __construct() { parent::__construct(); // 初始化控制台 Console::initialize(); }