Skip to content

Commit

Permalink
MDL-57655 session: Adds igbinary serializer to Redis session handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mwehr authored and stronk7 committed Jan 24, 2017
1 parent ae8e8e6 commit 8867159
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 3 additions & 0 deletions config-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@
// $CFG->session_redis_prefix = ''; // Optional, default is don't set one.
// $CFG->session_redis_acquire_lock_timeout = 120;
// $CFG->session_redis_lock_expire = 7200;
// Use the igbinary serializer instead of the php default one. Note that phpredis must be compiled with
// igbinary support to make the setting to work. Also, if you change the serializer you have to flush the database!
// $CFG->session_redis_serializer_use_igbinary = false; // Optional, default is PHP builtin serializer.
//
// Memcache session handler (requires memcached server and memcache extension):
// $CFG->session_handler_class = '\core\session\memcache';
Expand Down
10 changes: 8 additions & 2 deletions lib/classes/session/redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class redis extends handler {
protected $prefix = '';
/** @var int $acquiretimeout how long to wait for session lock in seconds */
protected $acquiretimeout = 120;
/** @var int $serializer The serializer to use */
protected $serializer = \Redis::SERIALIZER_PHP;
/**
* @var int $lockexpire how long to wait in seconds before expiring the lock automatically
* so that other requests may continue execution, ignored if PECL redis is below version 2.2.0.
Expand Down Expand Up @@ -91,6 +93,10 @@ public function __construct() {
$this->acquiretimeout = (int)$CFG->session_redis_acquire_lock_timeout;
}

if (!empty($CFG->session_redis_serializer_use_igbinary) && defined('\Redis::SERIALIZER_IGBINARY')) {
$this->serializer = \Redis::SERIALIZER_IGBINARY; // Set igbinary serializer if phpredis supports it.
}

// The following configures the session lifetime in redis to allow some
// wriggle room in the user noticing they've been booted off and
// letting them log back in before they lose their session entirely.
Expand Down Expand Up @@ -150,7 +156,7 @@ public function init() {
if (!$this->connection->connect($this->host, $this->port, 1)) {
throw new RedisException('Unable to connect to host.');
}
if (!$this->connection->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP)) {
if (!$this->connection->setOption(\Redis::OPT_SERIALIZER, $this->serializer)) {
throw new RedisException('Unable to set Redis PHP Serializer option.');
}

Expand Down Expand Up @@ -389,4 +395,4 @@ public function kill_session($sid) {

$this->handler_destroy($sid);
}
}
}

0 comments on commit 8867159

Please sign in to comment.