/
MemcachedSession.php
190 lines (161 loc) · 7.84 KB
/
MemcachedSession.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
class MemcachedSession
{
private static $config = array(
'lifetime' => 0,
'random_read' => true,
'replicate' => true, // will copy the same data in all servers
'failover' => true, // if one fails then read from another server
'servers' => array(
'127.0.0.1',
)
);
private static $connections = array();
public static function config($config){
self::$config = array_merge(self::$config,$config);
session_set_save_handler(
array('MemcacheSession', 'open'),
array('MemcacheSession', 'close'),
array('MemcacheSession', 'read'),
array('MemcacheSession', 'write'),
array('MemcacheSession', 'destroy'),
array('MemcacheSession', 'gc')
);
}
public static function open(){
if (self::$config['lifetime']==0)
self::$config['lifetime'] = ini_get('session.gc_maxlifetime');
// the following prevents unexpected effects when using objects as save handlers
register_shutdown_function('session_write_close');
return true;
}
public static function read($id){
// if we're not replicating data we won't randomize because the first
// configured server must be the first try to read
if (self::$config['replicate'] and self::$config['random_read']){
// randomize servers order
shuffle(self::$config['servers']);
}
//echo '<pre>';
//var_dump(self::$config['servers']);
//echo '</pre>';
// try with one server by time
foreach (self::$config['servers'] as $ip){
//echo "<p>read $ip</p>";
if (class_exists('Memcached')){
if (!array_key_exists($ip,self::$connections)){ // non existing connection, creating a new one for this ip
self::$connections[$ip] = new Memcached();
self::$connections[$ip]->addServer($ip, 11211);
}
if(self::$connections[$ip] != false) {
$value = self::$connections[$ip]->get("sessions/{$id}"); // get value
$res = self::$connections[$ip]->getResultCode(); // will check result
if ($res == Memcached::RES_NOTFOUND) //key does not exists
return null;
elseif ($res == 0) // value found
return $value; // return
else{
self::$connections[$ip] = false; // error or timeout (try with the next server)
}
}
// unavailable server, skipping
}
else{
if (!array_key_exists($ip,self::$connections)){
self::$connections[$ip] = new Memcache();
if (!self::$connections[$ip]->connect($ip, 11211)){ //try connection
self::$connections[$ip] = false; // mark server as down, skipping
}
}
if(self::$connections[$ip] != false){
$value = self::$connections[$ip]->get("sessions/{$id}");
if ($value !== false) return unserialize($value);
else return null; // value not found (false also could mean server error, but we'll assume key does not exists)
}
// server down, skipping
}
// without failover we won't keep trying
if (!self::$config['failover']) return null;
}
return null; // no servers availables
}
public static function write($id, $data){
// try with one server by time
foreach (self::$config['servers'] as $ip){
$wrote = false;
//echo "<p>write $ip</p>";
if (class_exists('Memcached')){
if (!array_key_exists($ip,self::$connections)){ // non existing connection, creating a new one for this ip
self::$connections[$ip] = new Memcached();
self::$connections[$ip]->addServer($ip, 11211);
}
if( self::$connections[$ip] != false ) {
if (!self::$connections[$ip]->set("sessions/{$id}", $data, self::$config['lifetime'])){
self::$connections[$ip] = false; // error or timeout
}
else{
$wrote = true;
}
}
// unavailable server, skipping
}
else{
if (!array_key_exists($ip,self::$connections)){
self::$connections[$ip] = new Memcache();
if (!self::$connections[$ip]->connect($ip, 11211)){ //try connection
self::$connections[$ip] = false; // mark server as down, skipping
}
}
if(self::$connections[$ip] != false){
if (!self::$connections[$ip]->set("sessions/{$id}", serialize($data), 0, self::$config['lifetime'])){
self::$connections[$ip] = false; // error or timeout, mark server as down
}
else{
$wrote = true;
}
}
// unavailable server, skipping
}
// if we couldn't write and we do failover we'll try to write in the next server
// also we'll write in the next server if we're replicating
if (!((!$wrote and self::$config['failover']) or self::$config['replicate'])) break;
}
return true;
}
public static function destroy($id){
// try with one server by time
foreach (self::$config['servers'] as $ip){
if (class_exists('Memcached')){
if (!array_key_exists($ip,self::$connections)){ // non existing connection, creating a new one for this ip
self::$connections[$ip] = new Memcached();
self::$connections[$ip]->addServer($ip, 11211);
}
if(!self::$connections[$ip] == false) {
$value = self::$connections[$ip]->delete("sessions/{$id}"); // get value
if ($value==false){
$res = self::$connections[$ip]->getResultCode(); // will check result
if ($res != Memcached::RES_NOTFOUND){
self::$connections[$ip] = false; // error or timeout (try with the next server)
}
}
}
// unavailable server, skipping
}
else{
if (!array_key_exists($ip,self::$connections)){
self::$connections[$ip] = new Memcache();
if (!self::$connections[$ip]->connect($ip, 11211)){ //try connection
self::$connections[$ip] = false; // mark server as down, skipping
}
}
if(!self::$connections[$ip] == false){
self::$connections[$ip]->delete("sessions/{$id}");
}
}
// no need to continue if we are not replicating and we don't do failover
if (!self::$config['replicate'] and !self::$config['failover']) break;
}
return true;
}
public static function gc(){ return true; }
public static function close(){ return true; }
}