Skip to content

Commit 1b1c647

Browse files
committed
取消redis/mysql的单例模式,更好的适配swoole
1 parent 2debe2b commit 1b1c647

File tree

2 files changed

+40
-67
lines changed

2 files changed

+40
-67
lines changed

plugins/SRedis.php

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,24 @@
2222
if(!defined("SLIGHTPHP_PLUGINS_DIR"))define("SLIGHTPHP_PLUGINS_DIR",dirname(__FILE__));
2323
require_once(SLIGHTPHP_PLUGINS_DIR."/SConfig.php");
2424
class SRedis{
25-
private static $_config;
25+
private static $hosts=array();
26+
private static $options=array();
27+
private static $_configFile;
2628
/**
2729
* 当前使用的resouce
2830
*/
29-
private static $_resource;
30-
/**
31-
*
32-
*/
33-
private static $_resources;
34-
private static $_instances=array();
35-
3631
public function __construct(){
3732
}
3833

3934
static function setConfigFile($file){
40-
self::$_config = $file;
35+
self::$_configFile = $file;
4136
}
4237
/**
4338
* @param string $zone
4439
* @return array
4540
*/
4641
static function getConfig($zone=null,$type="host"){
47-
$config = SConfig::getConfig(self::$_config,$zone);
42+
$config = SConfig::getConfig(self::$_configFile,$zone);
4843
if(isset($config->$type)){
4944
return $config->$type;
5045
}elseif(isset($config->host)){
@@ -58,53 +53,41 @@ static function getConfig($zone=null,$type="host"){
5853
* @return array
5954
*/
6055
static function useConfig($zone,$type="host"){
61-
$key = $zone.":".$type;
62-
if(isset(self::$_instances[$key])){
63-
return self::$_instances[$key];
64-
}else{
65-
self::$_instances[$key] = new self;
66-
}
67-
$hosts=array();
68-
$options=array();
6956
$config = self::getConfig($zone,$type);
7057
if(empty($config)){
71-
trigger_error("the redis hosts is not set in config file(".self::$_config.")");
58+
trigger_error("the redis hosts is not set in config file(".self::$_configFile.")");
7259
return false;
7360
}
61+
self::$hosts=[];
62+
self::$options=[];
7463
if(is_array($config)){
75-
$hosts=$config;
64+
self::$hosts=$config;
7665
}else{
77-
$hosts[]=$config;
66+
self::$hosts[]=$config;
7867
}
7968
$config = self::getConfig($zone,"options");
8069
if(!empty($config)){
8170
if(is_object($config)){
8271
foreach ($config as $k=>$v){
83-
$options[$k]=$v;
72+
self::$options[$k]=$v;
8473
}
8574
}
8675
}
87-
self::$_resource = self::$_resources[$key] = new RedisArray($hosts,$options);
88-
return self::$_instances[$key];
8976
}
9077
public function __call($name,$args){
9178
try{
92-
if(self::$_resource) return call_user_func_array(array(self::$_resource,$name),$args);
79+
$redis = new RedisArray(self::$hosts,self::$options);
80+
return call_user_func_array(array($redis,$name),$args);
9381
}catch(RedisException $e){
94-
self::$_instances=null;
95-
self::$_resources=null;
96-
self::$_resource=null;
9782
trigger_error($e);
9883
}
9984
return false;
10085
}
10186
public static function __callStatic($name,$args){
10287
try{
103-
if(self::$_resource) return call_user_func_array(array(self::$_resource,$name),$args);
88+
$redis = new RedisArray(self::$hosts,self::$options);
89+
return call_user_func_array(array($redis,$name),$args);
10490
}catch(RedisException $e){
105-
self::$_instances=null;
106-
self::$_resources=null;
107-
self::$_resource=null;
10891
trigger_error($e);
10992
}
11093
return false;

plugins/db/Db.php

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* @subpackage SDb
2020
*/
2121
namespace SlightPHP;
22+
require_once(SLIGHTPHP_PLUGINS_DIR."/db/DbPDO.php");
23+
require_once(SLIGHTPHP_PLUGINS_DIR."/db/DbMysqli.php");
2224
class Db{
2325
/**
2426
*
@@ -27,12 +29,12 @@ class Db{
2729
private $params;
2830
private $_engine_name="pdo_mysql";
2931
private $_allow_engines=array(
30-
"mysqli",
31-
"pdo_mysql","pdo_sqlite","pdo_cubrid",
32-
"pdo_dblib","pdo_firebird","pdo_ibm",
33-
"pdo_informix","pdo_sqlsrv","pdo_oci",
34-
"pdo_odbc","pdo_pgsql","pdo_4d"
35-
);
32+
"mysqli",
33+
"pdo_mysql","pdo_sqlite","pdo_cubrid",
34+
"pdo_dblib","pdo_firebird","pdo_ibm",
35+
"pdo_informix","pdo_sqlsrv","pdo_oci",
36+
"pdo_odbc","pdo_pgsql","pdo_4d"
37+
);
3638
private $_key;
3739

3840
/**
@@ -53,9 +55,8 @@ class Db{
5355
*/
5456
private $error=array('code'=>0,'msg'=>"");
5557
/**
56-
* @var array $_globals
58+
*
5759
*/
58-
static $_globals;
5960
function __construct($engineName="mysql"){
6061
$this->__setEngine($engineName);
6162
}
@@ -346,34 +347,24 @@ private function __query($sql, $retry=false){
346347
trigger_error("{$this->_engine_name} ( $sql )");
347348
}
348349
//Connect
349-
if(!isset(Db::$_globals[$this->_key])){
350-
if(extension_loaded('pdo')){
351-
require_once(SLIGHTPHP_PLUGINS_DIR."/db/DbPDO.php");
352-
$this->engine = new \SlightPHP\DbPDO($this->params);
353-
}elseif(extension_loaded('mysqli')){
354-
require_once(SLIGHTPHP_PLUGINS_DIR."/db/DbMysqli.php");
355-
$this->engine = new \SlightPHP\DbMysqli($this->params);
356-
}else{
357-
trigger_error("pdo and mysqli extension not exists",E_USER_ERROR);
358-
unset(Db::$_globals[$this->_key]);
359-
return false;
360-
}
361-
$this->engine->init($this->params);
362-
if($this->engine->connect()===false){
363-
$this->error['code']=$this->engine->errno();
364-
$this->error['msg']=$this->engine->error();
365-
if(defined("DEBUG")){
366-
trigger_error("{$this->_engine_name} ( ".var_export($this->error,true).")");
367-
}
368-
unset(Db::$_globals[$this->_key]);
369-
return false;
370-
}else{
371-
Db::$_globals[$this->_key] = $this->engine;
372-
}
350+
if(extension_loaded('pdo')){
351+
$this->engine = new \SlightPHP\DbPDO($this->params);
352+
}elseif(extension_loaded('mysqli')){
353+
$this->engine = new \SlightPHP\DbMysqli($this->params);
373354
}else{
374-
$this->engine = Db::$_globals[$this->_key];
355+
trigger_error("pdo and mysqli extension not exists",E_USER_ERROR);
356+
return false;
375357
}
376-
358+
$this->engine->init($this->params);
359+
if($this->engine->connect()===false){
360+
$this->error['code']=$this->engine->errno();
361+
$this->error['msg']=$this->engine->error();
362+
if(defined("DEBUG")){
363+
trigger_error("{$this->_engine_name} ( ".var_export($this->error,true).")");
364+
}
365+
return false;
366+
}
367+
377368
$result = $this->engine->query($sql);
378369

379370
if($result){
@@ -389,7 +380,6 @@ private function __query($sql, $retry=false){
389380
}
390381
$this->error['code']=$this->engine->errno();
391382
$this->error['msg']=$this->engine->error();
392-
unset(Db::$_globals[$this->_key]);
393383

394384
if($retry===false && $this->engine->connectionError){
395385
$this->_reInit();

0 commit comments

Comments
 (0)