Merge branch 'wip_MDL-46711_m28_memcachesrvs' of https://github.com/skodak/moodle

This commit is contained in:
Damyon Wiese 2014-08-19 16:31:06 +08:00
commit aba9019832

View file

@ -20,6 +20,9 @@
* This is based on the memcached code. It lacks some features, such as * This is based on the memcached code. It lacks some features, such as
* locking options, but appears to work in practice. * 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 * @package core
* @copyright 2014 The Open University * @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@ -37,6 +40,13 @@ defined('MOODLE_INTERNAL') || die();
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class memcache extends handler { 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. * Creates new instance of handler.
*/ */
@ -119,28 +129,39 @@ class memcache extends handler {
* @return bool true if session found. * @return bool true if session found.
*/ */
public function session_exists($sid) { public function session_exists($sid) {
if (!$this->servers) { $result = false;
return false;
foreach ($this->get_memcaches() as $memcache) {
if ($result === false) {
$value = $memcache->get($sid);
if ($value !== false) {
$result = true;
}
}
$memcache->close();
} }
$memcache = $this->get_memcache(); return $result;
$value = $memcache->get($sid);
$memcache->close();
return ($value !== false);
} }
/** /**
* Gets the memcache object with all the servers added to it. * Gets the Memcache objects, one for each server.
* The connects must be closed manually after use.
* *
* @return \Memcache Initialised memcache object * 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_memcache() { protected function get_memcaches() {
$memcache = new \Memcache(); $result = array();
foreach ($this->servers as $server) { foreach ($this->servers as $server) {
$memcache = new \Memcache();
$memcache->addServer($server[0], $server[1]); $memcache->addServer($server[0], $server[1]);
$result[] = $memcache;
} }
return $memcache; return $result;
} }
/** /**
@ -152,19 +173,23 @@ class memcache extends handler {
return; return;
} }
$memcache = $this->get_memcache(); $memcaches = $this->get_memcaches();
// Note: this can be significantly improved by fetching keys from memcache, // 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. // but we need to make sure we are not deleting somebody else's sessions.
$rs = $DB->get_recordset('sessions', array(), 'id DESC', 'id, sid'); $rs = $DB->get_recordset('sessions', array(), 'id DESC', 'id, sid');
foreach ($rs as $record) { foreach ($rs as $record) {
foreach ($memcaches as $memcache) {
$memcache->delete($record->sid); $memcache->delete($record->sid);
} }
}
$rs->close(); $rs->close();
foreach ($memcaches as $memcache) {
$memcache->close(); $memcache->close();
} }
}
/** /**
* Kills one session, the session record is removed afterwards. * Kills one session, the session record is removed afterwards.
@ -172,12 +197,9 @@ class memcache extends handler {
* @param string $sid PHP session ID * @param string $sid PHP session ID
*/ */
public function kill_session($sid) { public function kill_session($sid) {
if (!$this->servers) { foreach ($this->get_memcaches() as $memcache) {
return;
}
$memcache = $this->get_memcache();
$memcache->delete($sid); $memcache->delete($sid);
$memcache->close(); $memcache->close();
} }
}
} }