/
yrmcds_session.php
168 lines (157 loc) · 4.15 KB
/
yrmcds_session.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
<?php
/**
* Session handler using yrmcds extension.
*
* @author Yamamoto, Hirotaka
* @version 1.5.0
* @copyright (C) 2013-2016 Cybozu.
* @license 2-clause BSD
* @package session
*/
namespace yrmcds;
/**
* This exception will be thrown if \yrmcds\SessionHandler
* cannot acquire the session lock.
*/
class TimeoutException extends \RuntimeException {}
/**
* A session handler class based on \yrmcds\Client .
*
* This class requires PHP5.4+ because this implements
* \SessionHandlerInterface available since PHP5.4.
*
* This class only works with yrmcds because server-side locking
* is not implemented in memcached.
*
* To set the session handler, pass an instace of this class
* to `session_set_save_handler()`.
*
* @see http://www.php.net/manual/en/function.session-set-save-handler.php
*/
class SessionHandler implements \SessionHandlerInterface {
/**
* @internal
*/
private $client;
/**
* @internal
*/
private $expire;
/**
* @internal
*/
private $timeout;
/**
* @internal
*/
private $locked;
/**
* Constructor.
*
* Inactive sessions will expire in $expire seconds.
*
* If $timeout is 0, sessions will not be locked.
*
* If $timeout > 0, starting a session may throw \yrmcds\TimeoutException
* if the client cannot acquire the lock within $timeout seconds.
*
* @param \yrmcds\Client $client Connection to session storage.
* @param int $expire Session expiration time.
* @param int $timeout Lock wait timeout seconds.
*/
public function __construct(\yrmcds\Client $client,
$expire = 3600, $timeout = 6) {
$this->client = $client;
$this->expire = $expire;
$this->timeout = $timeout;
$this->locked = FALSE;
}
/**
* @internal
*/
public function open($savePath, $sessionName) {
return TRUE;
}
/**
* @internal
*/
public function close() {
// \yrmcds\Client will automatically unlock session objects.
return TRUE;
}
/**
* @internal
*/
private function wait_lock($id) {
$start = time();
while( TRUE ) {
$serial = $this->client->lockGet($id);
while( TRUE ) {
$r = $this->client->recv();
if( $r->serial == $serial )
break;
}
if( $r->status == \yrmcds\STATUS_LOCKED ) {
if( (time() - $start) > $this->timeout )
throw new TimeoutException("Session lock timeout");
time_nanosleep(0, 50000000); // 50 msec
continue;
}
return $r;
}
}
/**
* @internal
*/
public function read($id) {
if( $this->timeout != 0 ) {
$r = $this->wait_lock($id);
if( $r->status == \yrmcds\STATUS_OK ) {
$this->locked = TRUE;
$this->client->touch($id, $this->expire);
$this->client->recv();
}
} else {
$serial = $this->client->getTouch($id, $this->expire);
while( TRUE ) {
$r = $this->client->recv();
if( $r->serial == $serial )
break;
}
}
return (string)$r->data;
}
/**
* @internal
*/
public function write($id, $data) {
try {
if( strlen($data) == 0 ) {
$this->client->delete($id, TRUE);
return TRUE;
}
if( $this->locked ) {
$this->client->replaceUnlock(
$id, $data, 0, $this->expire, TRUE);
} else {
$this->client->set($id, $data, 0, $this->expire, 0, TRUE);
}
return TRUE;
} catch( \Exception $e ) {
return FALSE;
}
}
/**
* @internal
*/
public function destroy($id) {
$this->client->delete($id, TRUE);
return TRUE;
}
/**
* @internal
*/
public function gc($maxlifetime) {
return TRUE;
}
}