Skip to content

Commit 444f8d0

Browse files
committed
Merge pull request #12 from suyan/master
给sessiondb增加主从分离
2 parents 9c95700 + 0fb5afa commit 444f8d0

File tree

1 file changed

+72
-14
lines changed

1 file changed

+72
-14
lines changed

Extend/Driver/Session/SessionDb.class.php

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class SessionDb {
3838
/**
3939
* 数据库句柄
4040
*/
41-
protected $hander;
41+
protected $hander = array();
4242

4343
/**
4444
* 打开Session
@@ -49,8 +49,58 @@ class SessionDb {
4949
public function open($savePath, $sessName) {
5050
$this->lifeTime = C('SESSION_EXPIRE')?C('SESSION_EXPIRE'):ini_get('session.gc_maxlifetime');
5151
$this->sessionTable = C('SESSION_TABLE')?C('SESSION_TABLE'):C("DB_PREFIX")."session";
52-
$hander = mysql_connect(C('DB_HOST'),C('DB_USER'),C('DB_PWD'));
53-
$dbSel = mysql_select_db(C('DB_NAME'),$hander);
52+
//分布式数据库
53+
$host = explode(',',C('DB_HOST'));
54+
$port = explode(',',C('DB_PORT'));
55+
$name = explode(',',C('DB_NAME'));
56+
$user = explode(',',C('DB_USER'));
57+
$pwd = explode(',',C('DB_PWD'));
58+
if(1 == C('DB_DEPLOY_TYPE')){
59+
//读写分离
60+
if(C('DB_RW_SEPARATE')){
61+
$w = floor(mt_rand(0,C('DB_MASTER_NUM')-1));
62+
if(is_numeric(C('DB_SLAVE_NO'))){//指定服务器读
63+
$r = C('DB_SLAVE_NO');
64+
}else{
65+
$r = floor(mt_rand(C('DB_MASTER_NUM'),count($host)-1));
66+
}
67+
//主数据库链接
68+
$hander = mysql_connect(
69+
$host[$w].(isset($port[$w])?':'.$port[$w]:':'.$port[0]),
70+
isset($user[$w])?$user[$w]:$user[0],
71+
isset($pwd[$w])?$pwd[$w]:$pwd[0]
72+
);
73+
$dbSel = mysql_select_db(
74+
isset($name[$w])?$name[$w]:$name[0]
75+
,$hander);
76+
if(!$hander || !$dbSel)
77+
return false;
78+
$this->hander[0] = $hander;
79+
//从数据库链接
80+
$hander = mysql_connect(
81+
$host[$r].(isset($port[$r])?':'.$port[$r]:':'.$port[0]),
82+
isset($user[$r])?$user[$r]:$user[0],
83+
isset($pwd[$r])?$pwd[$r]:$pwd[0]
84+
);
85+
$dbSel = mysql_select_db(
86+
isset($name[$r])?$name[$r]:$name[0]
87+
,$hander);
88+
if(!$hander || !$dbSel)
89+
return false;
90+
$this->hander[1] = $hander;
91+
return true;
92+
}
93+
}
94+
//从数据库链接
95+
$r = floor(mt_rand(0,count($host)-1));
96+
$hander = mysql_connect(
97+
$host[$r].(isset($port[$r])?':'.$port[$r]:':'.$port[0]),
98+
isset($user[$r])?$user[$r]:$user[0],
99+
isset($pwd[$r])?$pwd[$r]:$pwd[0]
100+
);
101+
$dbSel = mysql_select_db(
102+
isset($name[$r])?$name[$r]:$name[0]
103+
,$hander);
54104
if(!$hander || !$dbSel)
55105
return false;
56106
$this->hander = $hander;
@@ -61,8 +111,12 @@ public function open($savePath, $sessName) {
61111
* 关闭Session
62112
* @access public
63113
*/
64-
public function close() {
65-
$this->gc(ini_get('session.gc_maxlifetime'));
114+
public function close() {
115+
if(is_array($this->hander)){
116+
$this->gc($this->lifeTime);
117+
return (mysql_close($this->hander[0]) && mysql_close($this->hander[1]));
118+
}
119+
$this->gc($this->lifeTime);
66120
return mysql_close($this->hander);
67121
}
68122

@@ -72,7 +126,8 @@ public function close() {
72126
* @param string $sessID
73127
*/
74128
public function read($sessID) {
75-
$res = mysql_query("SELECT session_data AS data FROM ".$this->sessionTable." WHERE session_id = '$sessID' AND session_expire >".time(),$this->hander);
129+
$hander = is_array($this->hander)?$this->hander[1]:$this->hander;
130+
$res = mysql_query("SELECT session_data AS data FROM ".$this->sessionTable." WHERE session_id = '$sessID' AND session_expire >".time(),$hander);
76131
if($res) {
77132
$row = mysql_fetch_assoc($res);
78133
return $row['data'];
@@ -87,9 +142,10 @@ public function read($sessID) {
87142
* @param String $sessData
88143
*/
89144
public function write($sessID,$sessData) {
145+
$hander = is_array($this->hander)?$this->hander[0]:$this->hander;
90146
$expire = time() + $this->lifeTime;
91-
mysql_query("REPLACE INTO ".$this->sessionTable." ( session_id, session_expire, session_data) VALUES( '$sessID', '$expire', '$sessData')",$this->hander);
92-
if(mysql_affected_rows($this->hander))
147+
mysql_query("REPLACE INTO ".$this->sessionTable." ( session_id, session_expire, session_data) VALUES( '$sessID', '$expire', '$sessData')",$hander);
148+
if(mysql_affected_rows($hander))
93149
return true;
94150
return false;
95151
}
@@ -100,8 +156,9 @@ public function write($sessID,$sessData) {
100156
* @param string $sessID
101157
*/
102158
public function destroy($sessID) {
103-
mysql_query("DELETE FROM ".$this->sessionTable." WHERE session_id = '$sessID'",$this->hander);
104-
if(mysql_affected_rows($this->hander))
159+
$hander = is_array($this->hander)?$this->hander[0]:$this->hander;
160+
mysql_query("DELETE FROM ".$this->sessionTable." WHERE session_id = '$sessID'",$hander);
161+
if(mysql_affected_rows($hander))
105162
return true;
106163
return false;
107164
}
@@ -112,20 +169,21 @@ public function destroy($sessID) {
112169
* @param string $sessMaxLifeTime
113170
*/
114171
public function gc($sessMaxLifeTime) {
115-
mysql_query("DELETE FROM ".$this->sessionTable." WHERE session_expire < ".time(),$this->hander);
116-
return mysql_affected_rows($this->hander);
172+
$hander = is_array($this->hander)?$this->hander[0]:$this->hander;
173+
mysql_query("DELETE FROM ".$this->sessionTable." WHERE session_expire < ".time(),$hander);
174+
return mysql_affected_rows($hander);
117175
}
118176

119177
/**
120178
* 打开Session
121179
* @access public
122180
*/
123181
public function execute() {
124-
session_set_save_handler(array(&$this,"open"),
182+
session_set_save_handler(array(&$this,"open"),
125183
array(&$this,"close"),
126184
array(&$this,"read"),
127185
array(&$this,"write"),
128186
array(&$this,"destroy"),
129187
array(&$this,"gc"));
130188
}
131-
}
189+
}

0 commit comments

Comments
 (0)