Пример #1
0
 /**
  * 创建数据库操作类的实例
  * 根据$type类型,获取配置文件中跟$type对应的数据库配置参数
  *
  * @param string $type 数据库配置全局变量$g_db中的键,包括:
  * shopMaster,shopSlave,tanMaster,tanSlave,ProductMaster,ProductSlave,shopHisMaster,
  * shopHisSlave,shopLogMaster,shopLogSlave,cartMaster,msgMaster,memberMaster,kongv2Master,
  * kongv2Slave,xinyuMaster,pmMaster,pmSlave
  * @return pdo or adodb
  * @throws Exception
  */
 public static function getInstance($type)
 {
     if (empty($type)) {
         throw new \Exception("数据库定义参数未定义!");
     }
     //判断数据库连接是否已实例化,若已经实例化,直接返回,否则实例化数据库连接
     if (!isset(self::$instances[$type])) {
         //获取数据库库的配置信息,对于多从库将执行一主多从机制
         $dbInfo = \PhpBaseConfig::get('database', $type);
         if (is_array($dbInfo) && isset($dbInfo['host']) && isset($dbInfo['username']) && isset($dbInfo['password']) && isset($dbInfo['database'])) {
             $port = isset($dbInfo['port']) ? $dbInfo['port'] : 3306;
             $charset = isset($dbInfo['charset']) ? $dbInfo['charset'] : "UTF8";
             //$dbInfo['persistent'] = 1 为长连接,否则为短连接
             $connectType = isset($dbInfo['persistent']) && $dbInfo['persistent'] == 1 ? true : false;
             try {
                 self::$instances[$type] = new \Util\Db\Pdo($dbInfo['host'], $dbInfo['database'], $dbInfo['username'], $dbInfo['password'], $port, $charset);
                 if ($connectType) {
                     self::$instances[$type]->pconnect();
                 } else {
                     self::$instances[$type]->connect();
                 }
             } catch (\Exception $e) {
                 throw $e;
             }
         } else {
             throw new \Exception("未找到数据库配置信息!");
         }
     }
     return self::$instances[$type];
 }
Пример #2
0
 public static function getRedisHandle()
 {
     if (!self::$redisHandle) {
         $redisConfigs = \PhpBaseConfig::get('redisconfig', 'jianzhi');
         $redisConfig = $redisConfigs['servers'][0];
         self::$redisHandle = new \redis();
         self::$redisHandle->connect($redisConfig['host'], $redisConfig['port']);
     }
     return self::$redisHandle;
 }