Reworked sessions and added Redis handler
Requires phpredis to be installed.
This commit is contained in:
parent
650c16efae
commit
df0ef6d959
3 changed files with 92 additions and 131 deletions
|
@ -132,91 +132,67 @@ class Session extends Object
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
// Sets up our configuration variables
|
// Sets up our configuration variables
|
||||||
$session = $this->config->pickles['session'];
|
if (isset($this->config->pickles['session']))
|
||||||
|
{
|
||||||
|
$session = $this->config->pickles['session'];
|
||||||
|
$version = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->config->pickles['sessions']))
|
||||||
|
{
|
||||||
|
$session = $this->config->pickles['sessions'];
|
||||||
|
$version = 2;
|
||||||
|
}
|
||||||
|
|
||||||
$datasources = $this->config->datasources;
|
$datasources = $this->config->datasources;
|
||||||
|
|
||||||
$datasource = false;
|
$this->handler = 'files';
|
||||||
$table = 'sessions';
|
$datasource = false;
|
||||||
|
$table = 'sessions';
|
||||||
|
|
||||||
if (is_array($session))
|
if (isset($datasources[$session]))
|
||||||
{
|
{
|
||||||
if (isset($session['handler']) && in_array($session['handler'], array('files', 'memcache', 'mysql')))
|
$datasource = $datasources[$session];
|
||||||
{
|
$this->handler = $datasource['type'];
|
||||||
$this->handler = $session['handler'];
|
|
||||||
|
|
||||||
if ($this->handler != 'files')
|
if (isset($datasource['hostname'], $datasource['port']))
|
||||||
{
|
|
||||||
if (isset($session['datasource']))
|
|
||||||
{
|
|
||||||
$datasource = $session['datasource'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($session['table']))
|
|
||||||
{
|
|
||||||
$table = $session['table'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if ($session === true || $session == 'files')
|
|
||||||
{
|
{
|
||||||
$this->handler = 'files';
|
$host = 'tcp://' . $datasource['hostname'] . ':' . $datasource['port'];
|
||||||
}
|
|
||||||
elseif ($session == 'memcache')
|
|
||||||
{
|
|
||||||
$this->handler = 'memcache';
|
|
||||||
$datasource = 'memcache';
|
|
||||||
}
|
|
||||||
elseif ($session == 'mysql')
|
|
||||||
{
|
|
||||||
$this->handler = 'mysql';
|
|
||||||
$datasource = 'mysql';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($this->handler)
|
switch ($this->handler)
|
||||||
{
|
{
|
||||||
case 'files':
|
|
||||||
ini_set('session.save_handler', 'files');
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'memcache':
|
case 'memcache':
|
||||||
$hostname = 'localhost';
|
|
||||||
$port = 11211;
|
|
||||||
|
|
||||||
if ($datasource !== false && isset($datasources[$datasource]))
|
|
||||||
{
|
|
||||||
$hostname = $datasources[$datasource]['hostname'];
|
|
||||||
$port = $datasources[$datasource]['port'];
|
|
||||||
}
|
|
||||||
|
|
||||||
ini_set('session.save_handler', 'memcache');
|
ini_set('session.save_handler', 'memcache');
|
||||||
ini_set('session.save_path', 'tcp://' . $hostname . ':' . $port . '?persistent=1&weight=1&timeout=1&retry_interval=15');
|
ini_set('session.save_path', $host . '?persistent=1&weight=1&timeout=1&retry_interval=15');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// @todo memcached
|
||||||
|
|
||||||
case 'mysql':
|
case 'mysql':
|
||||||
if ($datasource !== false && isset($datasources[$datasource]))
|
// Sets our access time and time to live
|
||||||
{
|
$this->accessed_at = time();
|
||||||
// Sets our access time and time to live
|
$this->time_to_live = ini_get('session.gc_maxlifetime');
|
||||||
$this->accessed_at = time();
|
|
||||||
$this->time_to_live = ini_get('session.gc_maxlifetime');
|
|
||||||
|
|
||||||
$this->datasource = $datasource;
|
$this->datasource = $datasource;
|
||||||
$this->table = $table;
|
$this->table = $table;
|
||||||
|
|
||||||
// Gets a database instance
|
// Gets a database instance
|
||||||
$this->db = Database::getInstance($this->datasource);
|
$this->db = Database::getInstance($this->datasource);
|
||||||
|
|
||||||
// Initializes the session
|
// Initializes the session
|
||||||
$this->initialize();
|
$this->initialize();
|
||||||
}
|
break;
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new Exception('Unable to determine which datasource to use');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
case 'redis':
|
||||||
|
ini_set('session.save_handler', 'redis');
|
||||||
|
ini_set('session.save_path', $host . '?weight=1' . (isset($datasource['database']) ? '&database=' . $datasource['database'] : ''));
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
case 'files':
|
||||||
|
ini_set('session.save_handler', 'files');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
106
jar.php
106
jar.php
|
@ -7516,91 +7516,67 @@ class Session extends Object
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
// Sets up our configuration variables
|
// Sets up our configuration variables
|
||||||
$session = $this->config->pickles['session'];
|
if (isset($this->config->pickles['session']))
|
||||||
|
{
|
||||||
|
$session = $this->config->pickles['session'];
|
||||||
|
$version = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->config->pickles['sessions']))
|
||||||
|
{
|
||||||
|
$session = $this->config->pickles['sessions'];
|
||||||
|
$version = 2;
|
||||||
|
}
|
||||||
|
|
||||||
$datasources = $this->config->datasources;
|
$datasources = $this->config->datasources;
|
||||||
|
|
||||||
$datasource = false;
|
$this->handler = 'files';
|
||||||
$table = 'sessions';
|
$datasource = false;
|
||||||
|
$table = 'sessions';
|
||||||
|
|
||||||
if (is_array($session))
|
if (isset($datasources[$session]))
|
||||||
{
|
{
|
||||||
if (isset($session['handler']) && in_array($session['handler'], array('files', 'memcache', 'mysql')))
|
$datasource = $datasources[$session];
|
||||||
{
|
$this->handler = $datasource['type'];
|
||||||
$this->handler = $session['handler'];
|
|
||||||
|
|
||||||
if ($this->handler != 'files')
|
if (isset($datasource['hostname'], $datasource['port']))
|
||||||
{
|
|
||||||
if (isset($session['datasource']))
|
|
||||||
{
|
|
||||||
$datasource = $session['datasource'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($session['table']))
|
|
||||||
{
|
|
||||||
$table = $session['table'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if ($session === true || $session == 'files')
|
|
||||||
{
|
{
|
||||||
$this->handler = 'files';
|
$host = 'tcp://' . $datasource['hostname'] . ':' . $datasource['port'];
|
||||||
}
|
|
||||||
elseif ($session == 'memcache')
|
|
||||||
{
|
|
||||||
$this->handler = 'memcache';
|
|
||||||
$datasource = 'memcache';
|
|
||||||
}
|
|
||||||
elseif ($session == 'mysql')
|
|
||||||
{
|
|
||||||
$this->handler = 'mysql';
|
|
||||||
$datasource = 'mysql';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($this->handler)
|
switch ($this->handler)
|
||||||
{
|
{
|
||||||
case 'files':
|
|
||||||
ini_set('session.save_handler', 'files');
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'memcache':
|
case 'memcache':
|
||||||
$hostname = 'localhost';
|
|
||||||
$port = 11211;
|
|
||||||
|
|
||||||
if ($datasource !== false && isset($datasources[$datasource]))
|
|
||||||
{
|
|
||||||
$hostname = $datasources[$datasource]['hostname'];
|
|
||||||
$port = $datasources[$datasource]['port'];
|
|
||||||
}
|
|
||||||
|
|
||||||
ini_set('session.save_handler', 'memcache');
|
ini_set('session.save_handler', 'memcache');
|
||||||
ini_set('session.save_path', 'tcp://' . $hostname . ':' . $port . '?persistent=1&weight=1&timeout=1&retry_interval=15');
|
ini_set('session.save_path', $host . '?persistent=1&weight=1&timeout=1&retry_interval=15');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// @todo memcached
|
||||||
|
|
||||||
case 'mysql':
|
case 'mysql':
|
||||||
if ($datasource !== false && isset($datasources[$datasource]))
|
// Sets our access time and time to live
|
||||||
{
|
$this->accessed_at = time();
|
||||||
// Sets our access time and time to live
|
$this->time_to_live = ini_get('session.gc_maxlifetime');
|
||||||
$this->accessed_at = time();
|
|
||||||
$this->time_to_live = ini_get('session.gc_maxlifetime');
|
|
||||||
|
|
||||||
$this->datasource = $datasource;
|
$this->datasource = $datasource;
|
||||||
$this->table = $table;
|
$this->table = $table;
|
||||||
|
|
||||||
// Gets a database instance
|
// Gets a database instance
|
||||||
$this->db = Database::getInstance($this->datasource);
|
$this->db = Database::getInstance($this->datasource);
|
||||||
|
|
||||||
// Initializes the session
|
// Initializes the session
|
||||||
$this->initialize();
|
$this->initialize();
|
||||||
}
|
break;
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new Exception('Unable to determine which datasource to use');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
case 'redis':
|
||||||
|
ini_set('session.save_handler', 'redis');
|
||||||
|
ini_set('session.save_path', $host . '?weight=1' . (isset($datasource['database']) ? '&database=' . $datasource['database'] : ''));
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
case 'files':
|
||||||
|
ini_set('session.save_handler', 'files');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
pickles.php
11
pickles.php
|
@ -138,7 +138,7 @@ if (is_array($config->php) && count($config->php) > 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Starts session handling
|
// Starts session handling (old)
|
||||||
if (isset($config->pickles['session']))
|
if (isset($config->pickles['session']))
|
||||||
{
|
{
|
||||||
if (session_id() == '' && $config->pickles['session'] !== false)
|
if (session_id() == '' && $config->pickles['session'] !== false)
|
||||||
|
@ -147,6 +147,15 @@ if (isset($config->pickles['session']))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Starts session handling (new)
|
||||||
|
if (isset($config->pickles['sessions']))
|
||||||
|
{
|
||||||
|
if (session_id() == '' && $config->pickles['sessions'] !== false)
|
||||||
|
{
|
||||||
|
new Session();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
// {{{ Defaults some internals for ease of use
|
// {{{ Defaults some internals for ease of use
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue