diff --git a/modules/gallery/config/session.php b/modules/gallery/config/session.php index 7ecee58e..c873a9e0 100644 --- a/modules/gallery/config/session.php +++ b/modules/gallery/config/session.php @@ -63,4 +63,13 @@ $config['regenerate'] = 0; /** * Percentage probability that the gc (garbage collection) routine is started. */ -$config['gc_probability'] = 2; \ No newline at end of file +$config['gc_probability'] = 2; + +/** + * Session server parameter, used by Redis driver. + */ +$config['server'] = [ + 'host' => 'redis', + 'port' => 6379, + 'persistent' => false, +]; diff --git a/system/libraries/drivers/Session/Redis.php b/system/libraries/drivers/Session/Redis.php new file mode 100644 index 00000000..4f36d5fb --- /dev/null +++ b/system/libraries/drivers/Session/Redis.php @@ -0,0 +1,129 @@ +config = Kohana::config('session'); + + if ( ! empty($this->config['encryption'])) + { + // Load encryption + $this->encrypt = Encrypt::instance(); + } + + if ( ! extension_loaded('redis')) + throw new Exception('The redis PHP extension must be loaded to use this driver.'); + + $this->backend = new Redis; + + if (empty($this->config['server'])) throw new Cache_Exception('Define the "server" settings in your session config'); + + $server = $this->config['server']; + + if (empty($server['host'])) throw new Cache_Exception('Missing Redis host'); + $method = !empty($server['persistent']) ? 'pconnect' : 'connect'; + + if (empty($server['port'])) + { + $this->backend->$method($server['host'], $server['port']); + } + else + { + $this->backend->$method($server['host']); + } + + Kohana_Log::add('debug', 'Session Redis Driver Initialized'); + } + + public function open($path, $name) + { + return TRUE; + } + + public function close() + { + if (empty($this->config['server']['persistent'])) + { + $this->backend->close(); + } + + return TRUE; + } + + public function read($id) + { + $data = $this->backend->get($id); + + if (!strlen($data)) + { + // No current session + $this->session_id = NULL; + + return ''; + } + + // Set the current session id + $this->session_id = $id; + + return ($this->encrypt === NULL) ? base64_decode($data) : $this->encrypt->decode($data); + } + + public function write($id, $data) + { + if ( ! Session::$should_save) + return TRUE; + + $data = $this->backend->set( + $id, + ($this->encrypt === NULL) ? base64_encode($data) : $this->encrypt->encode($data), + ini_get('session.gc_maxlifetime') + ); + + return TRUE; + } + + public function destroy($id) + { + $this->backend->del($id); + + // Session id is no longer valid + $this->session_id = NULL; + + return TRUE; + } + + public function regenerate() + { + // Generate a new session id + session_regenerate_id(); + + // Return new session id + return session_id(); + } + + public function gc($maxlifetime) + { + // Delete all expired sessions + + return TRUE; + } + +} // End Session Redis Driver