MDL-58018 core: Add support to open sessions without a lock
This commit is contained in:
committed by
Mark Nelson
parent
788dfb9c7d
commit
1c3b89b170
@@ -34,6 +34,9 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class handler {
|
||||
/** @var boolean $haslock does the session need and/or have a lock? */
|
||||
protected $haslock = false;
|
||||
|
||||
/**
|
||||
* Start the session.
|
||||
* @return bool success
|
||||
@@ -42,6 +45,49 @@ abstract class handler {
|
||||
return session_start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the session and release lock. If the session was not intentionally opened
|
||||
* with a write lock, then we will abort the session instead if able.
|
||||
*/
|
||||
public function write_close() {
|
||||
if ($this->has_writelock()) {
|
||||
session_write_close();
|
||||
$this->haslock = false;
|
||||
} else {
|
||||
$this->abort();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Release lock on the session without writing it.
|
||||
* May not be possible in older versions of PHP. If so, session may be written anyway
|
||||
* so that any locks are released.
|
||||
*/
|
||||
public function abort() {
|
||||
session_abort();
|
||||
$this->haslock = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is called after init() and before start() to indicate whether the session
|
||||
* opened should be writable or not. This is intentionally captured even if your
|
||||
* handler doesn't support non-locking sessions, so that behavior (upon session close)
|
||||
* matches closely between handlers.
|
||||
* @param bool $needslock true if needs to be open for writing
|
||||
*/
|
||||
public function set_needslock($needslock) {
|
||||
$this->haslock = $needslock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has this session been opened with a writelock? Your handler should call this during
|
||||
* start() if you support read-only sessions.
|
||||
* @return bool true if session is intended to have a write lock.
|
||||
*/
|
||||
public function has_writelock() {
|
||||
return $this->haslock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Init session handler.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user