forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemcache.php
205 lines (181 loc) · 6.55 KB
/
memcache.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Memcache based session handler.
*
* This is based on the memcached code. It lacks some features, such as
* locking options, but appears to work in practice.
*
* Note: You may need to manually configure redundancy and fail-over
* if you specify multiple servers.
*
* @package core
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\session;
defined('MOODLE_INTERNAL') || die();
/**
* Memcache based session handler.
*
* @package core
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class memcache extends handler {
/** @var string $savepath save_path string */
protected $savepath;
/** @var array $servers list of servers parsed from save_path */
protected $servers;
/** @var int $acquiretimeout how long to wait for session lock */
protected $acquiretimeout = 120;
/**
* Creates new instance of handler.
*/
public function __construct() {
global $CFG;
if (empty($CFG->session_memcache_save_path)) {
$this->savepath = '';
} else {
$this->savepath = $CFG->session_memcache_save_path;
}
if (empty($this->savepath)) {
$this->servers = array();
} else {
$this->servers = util::connection_string_to_memcache_servers($this->savepath);
}
if (!empty($CFG->session_memcache_acquire_lock_timeout)) {
$this->acquiretimeout = (int)$CFG->session_memcache_acquire_lock_timeout;
}
}
/**
* Starts the session.
*
* @return bool success
*/
public function start() {
$default = ini_get('max_execution_time');
set_time_limit($this->acquiretimeout);
$result = parent::start();
set_time_limit($default);
return $result;
}
/**
* Inits session handler.
*/
public function init() {
if (!extension_loaded('memcache')) {
throw new exception('sessionhandlerproblem', 'error', '', null,
'memcache extension is not loaded');
}
$version = phpversion('memcache');
if (!$version or version_compare($version, '2.2') < 0) {
throw new exception('sessionhandlerproblem', 'error', '', null,
'memcache extension version must be at least 2.2');
}
if (empty($this->savepath)) {
throw new exception('sessionhandlerproblem', 'error', '', null,
'$CFG->session_memcache_save_path must be specified in config.php');
}
// Check in case anybody mistakenly includes tcp://, which you
// would do in the raw PHP config. We require the same format as
// for memcached (without tcp://). Otherwse the code that splits it into
// individual servers won't have worked properly.
if (strpos($this->savepath, 'tcp://') !== false) {
throw new exception('sessionhandlerproblem', 'error', '', null,
'$CFG->session_memcache_save_path should not contain tcp://');
}
ini_set('session.save_handler', 'memcache');
// The format of save_path is different for memcache (compared to memcached).
// We are using the same format in config.php to avoid confusion.
// It has to have tcp:// at the start of each entry.
$memcacheformat = preg_replace('~(^|,\s*)~','$1tcp://', $this->savepath);
ini_set('session.save_path', $memcacheformat);
}
/**
* Check the backend contains data for this session id.
*
* Note: this is intended to be called from manager::session_exists() only.
*
* @param string $sid PHP session ID
* @return bool true if session found.
*/
public function session_exists($sid) {
$result = false;
foreach ($this->get_memcaches() as $memcache) {
if ($result === false) {
$value = $memcache->get($sid);
if ($value !== false) {
$result = true;
}
}
$memcache->close();
}
return $result;
}
/**
* Gets the Memcache objects, one for each server.
* The connects must be closed manually after use.
*
* Note: the servers are not automatically synchronised
* when accessed via Memcache class, it needs to be
* done manually by accessing all configured servers.
*
* @return \Memcache[] Array of initialised memcache objects
*/
protected function get_memcaches() {
$result = array();
foreach ($this->servers as $server) {
$memcache = new \Memcache();
$memcache->addServer($server[0], $server[1]);
$result[] = $memcache;
}
return $result;
}
/**
* Kills all active sessions, the core sessions table is purged afterwards.
*/
public function kill_all_sessions() {
global $DB;
if (!$this->servers) {
return;
}
$memcaches = $this->get_memcaches();
// Note: this can be significantly improved by fetching keys from memcache,
// but we need to make sure we are not deleting somebody else's sessions.
$rs = $DB->get_recordset('sessions', array(), 'id DESC', 'id, sid');
foreach ($rs as $record) {
foreach ($memcaches as $memcache) {
$memcache->delete($record->sid);
}
}
$rs->close();
foreach ($memcaches as $memcache) {
$memcache->close();
}
}
/**
* Kills one session, the session record is removed afterwards.
*
* @param string $sid PHP session ID
*/
public function kill_session($sid) {
foreach ($this->get_memcaches() as $memcache) {
$memcache->delete($sid);
$memcache->close();
}
}
}