Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

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: 583 lines (20 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

redis:: (17 methods):
  __construct()
  start()
  init()
  handler_open()
  handler_close()
  handler_read()
  compress()
  uncompress()
  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.

compress($value)   X-Ref
Compresses session data.

param: mixed $value
return: string

uncompress($value)   X-Ref
Uncompresses session data.

param: string $value
return: mixed

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