Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

Redis based session handler.

Copyright: 2015 Russell Smith <mr-russ@smith2001.net>
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 492 lines (17 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

redis:: (15 methods):
  __construct()
  start()
  init()
  handler_open()
  handler_close()
  handler_read()
  handler_write()
  handler_destroy()
  handler_gc()
  unlock_session()
  lock_session()
  time()
  session_exists()
  kill_all_sessions()
  kill_session()


Class: redis  - X-Ref

Redis based session handler.

The default Redis session handler does not handle locking in 2.2.7, so we have written a php session handler
that uses locking.  The places where locking is used was modeled from the memcached code that is used in Moodle
https://github.com/php-memcached-dev/php-memcached/blob/master/php_memcached_session.c

__construct()   X-Ref
Create new instance of handler.


start()   X-Ref
Start the session.

return: bool success

init()   X-Ref
Init session handler.


handler_open($savepath, $sessionname)   X-Ref
Update our session search path to include session name when opened.

param: string $savepath  unused session save path. (ignored)
param: string $sessionname Session name for this session. (ignored)
return: bool true always as we will succeed.

handler_close()   X-Ref
Close the session completely. We also remove all locks we may have obtained that aren't expired.

return: bool true on success.  false on unable to unlock sessions.

handler_read($id)   X-Ref
Read the session data from storage

param: string $id The session id to read from storage.
return: string The session data for PHP to process.

handler_write($id, $data)   X-Ref
Write the serialized session data to our session store.

param: string $id session id to write.
param: string $data session data
return: bool true on write success, false on failure

handler_destroy($id)   X-Ref
Handle destroying a session.

param: string $id the session id to destroy.
return: bool true if the session was deleted, false otherwise.

handler_gc($maxlifetime)   X-Ref
Garbage collect sessions.  We don't we any as Redis does it for us.

param: integer $maxlifetime All sessions older than this should be removed.
return: bool true, as Redis handles expiry for us.

unlock_session($id)   X-Ref
Unlock a session.

param: string $id Session id to be unlocked.

lock_session($id)   X-Ref
Obtain a session lock so we are the only one using it at the moment.

param: string $id The session id to lock.
return: bool true when session was locked, exception otherwise.

time()   X-Ref
Return the current time.

return: int the current time as a unixtimestamp.

session_exists($sid)   X-Ref
Check the backend contains data for this session id.

Note: this is intended to be called from manager::session_exists() only.

param: string $sid
return: bool true if session found.

kill_all_sessions()   X-Ref
Kill all active sessions, the core sessions table is purged afterwards.


kill_session($sid)   X-Ref
Kill one session, the session record is removed afterwards.

param: string $sid