Skip to content

Commit

Permalink
取消redis/mysql的单例模式,更好的适配swoole
Browse files Browse the repository at this point in the history
  • Loading branch information
hetao29 committed Mar 19, 2024
1 parent 2debe2b commit 1b1c647
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 67 deletions.
47 changes: 15 additions & 32 deletions plugins/SRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,24 @@
if(!defined("SLIGHTPHP_PLUGINS_DIR"))define("SLIGHTPHP_PLUGINS_DIR",dirname(__FILE__));
require_once(SLIGHTPHP_PLUGINS_DIR."/SConfig.php");
class SRedis{
private static $_config;
private static $hosts=array();
private static $options=array();
private static $_configFile;
/**
* 当前使用的resouce
*/
private static $_resource;
/**
*
*/
private static $_resources;
private static $_instances=array();

public function __construct(){
}

static function setConfigFile($file){
self::$_config = $file;
self::$_configFile = $file;
}
/**
* @param string $zone
* @return array
*/
static function getConfig($zone=null,$type="host"){
$config = SConfig::getConfig(self::$_config,$zone);
$config = SConfig::getConfig(self::$_configFile,$zone);
if(isset($config->$type)){
return $config->$type;
}elseif(isset($config->host)){
Expand All @@ -58,53 +53,41 @@ static function getConfig($zone=null,$type="host"){
* @return array
*/
static function useConfig($zone,$type="host"){
$key = $zone.":".$type;
if(isset(self::$_instances[$key])){
return self::$_instances[$key];
}else{
self::$_instances[$key] = new self;
}
$hosts=array();
$options=array();
$config = self::getConfig($zone,$type);
if(empty($config)){
trigger_error("the redis hosts is not set in config file(".self::$_config.")");
trigger_error("the redis hosts is not set in config file(".self::$_configFile.")");
return false;
}
self::$hosts=[];
self::$options=[];
if(is_array($config)){
$hosts=$config;
self::$hosts=$config;
}else{
$hosts[]=$config;
self::$hosts[]=$config;
}
$config = self::getConfig($zone,"options");
if(!empty($config)){
if(is_object($config)){
foreach ($config as $k=>$v){
$options[$k]=$v;
self::$options[$k]=$v;
}
}
}
self::$_resource = self::$_resources[$key] = new RedisArray($hosts,$options);
return self::$_instances[$key];
}
public function __call($name,$args){
try{
if(self::$_resource) return call_user_func_array(array(self::$_resource,$name),$args);
$redis = new RedisArray(self::$hosts,self::$options);
return call_user_func_array(array($redis,$name),$args);
}catch(RedisException $e){
self::$_instances=null;
self::$_resources=null;
self::$_resource=null;
trigger_error($e);
}
return false;
}
public static function __callStatic($name,$args){
try{
if(self::$_resource) return call_user_func_array(array(self::$_resource,$name),$args);
$redis = new RedisArray(self::$hosts,self::$options);
return call_user_func_array(array($redis,$name),$args);
}catch(RedisException $e){
self::$_instances=null;
self::$_resources=null;
self::$_resource=null;
trigger_error($e);
}
return false;
Expand Down
60 changes: 25 additions & 35 deletions plugins/db/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* @subpackage SDb
*/
namespace SlightPHP;
require_once(SLIGHTPHP_PLUGINS_DIR."/db/DbPDO.php");
require_once(SLIGHTPHP_PLUGINS_DIR."/db/DbMysqli.php");
class Db{
/**
*
Expand All @@ -27,12 +29,12 @@ class Db{
private $params;
private $_engine_name="pdo_mysql";
private $_allow_engines=array(
"mysqli",
"pdo_mysql","pdo_sqlite","pdo_cubrid",
"pdo_dblib","pdo_firebird","pdo_ibm",
"pdo_informix","pdo_sqlsrv","pdo_oci",
"pdo_odbc","pdo_pgsql","pdo_4d"
);
"mysqli",
"pdo_mysql","pdo_sqlite","pdo_cubrid",
"pdo_dblib","pdo_firebird","pdo_ibm",
"pdo_informix","pdo_sqlsrv","pdo_oci",
"pdo_odbc","pdo_pgsql","pdo_4d"
);
private $_key;

/**
Expand All @@ -53,9 +55,8 @@ class Db{
*/
private $error=array('code'=>0,'msg'=>"");
/**
* @var array $_globals
*
*/
static $_globals;
function __construct($engineName="mysql"){
$this->__setEngine($engineName);
}
Expand Down Expand Up @@ -346,34 +347,24 @@ private function __query($sql, $retry=false){
trigger_error("{$this->_engine_name} ( $sql )");
}
//Connect
if(!isset(Db::$_globals[$this->_key])){
if(extension_loaded('pdo')){
require_once(SLIGHTPHP_PLUGINS_DIR."/db/DbPDO.php");
$this->engine = new \SlightPHP\DbPDO($this->params);
}elseif(extension_loaded('mysqli')){
require_once(SLIGHTPHP_PLUGINS_DIR."/db/DbMysqli.php");
$this->engine = new \SlightPHP\DbMysqli($this->params);
}else{
trigger_error("pdo and mysqli extension not exists",E_USER_ERROR);
unset(Db::$_globals[$this->_key]);
return false;
}
$this->engine->init($this->params);
if($this->engine->connect()===false){
$this->error['code']=$this->engine->errno();
$this->error['msg']=$this->engine->error();
if(defined("DEBUG")){
trigger_error("{$this->_engine_name} ( ".var_export($this->error,true).")");
}
unset(Db::$_globals[$this->_key]);
return false;
}else{
Db::$_globals[$this->_key] = $this->engine;
}
if(extension_loaded('pdo')){
$this->engine = new \SlightPHP\DbPDO($this->params);
}elseif(extension_loaded('mysqli')){
$this->engine = new \SlightPHP\DbMysqli($this->params);
}else{
$this->engine = Db::$_globals[$this->_key];
trigger_error("pdo and mysqli extension not exists",E_USER_ERROR);
return false;
}

$this->engine->init($this->params);
if($this->engine->connect()===false){
$this->error['code']=$this->engine->errno();
$this->error['msg']=$this->engine->error();
if(defined("DEBUG")){
trigger_error("{$this->_engine_name} ( ".var_export($this->error,true).")");
}
return false;
}

$result = $this->engine->query($sql);

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

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

0 comments on commit 1b1c647

Please sign in to comment.