-
Notifications
You must be signed in to change notification settings - Fork 48
Additional Session Prefix #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,6 +253,13 @@ class Handler implements \SessionHandlerInterface | |
* @var boolean | ||
*/ | ||
private $_readOnly; | ||
|
||
/** | ||
* Additional Configurable Prefix for Session | ||
* | ||
* @var string | ||
*/ | ||
private $_prefixAdditional; | ||
|
||
/** | ||
* @param ConfigInterface $config | ||
|
@@ -284,6 +291,7 @@ public function __construct(ConfigInterface $config, LoggerInterface $logger, $r | |
$this->_failAfter = $this->config->getFailAfter() ?: self::DEFAULT_FAIL_AFTER; | ||
$this->_maxLifetime = $this->config->getMaxLifetime() ?: self::DEFAULT_MAX_LIFETIME; | ||
$this->_minLifetime = $this->config->getMinLifetime() ?: self::DEFAULT_MIN_LIFETIME; | ||
$this->_prefixAdditional = $this->config->getPrefix() ?: ''; | ||
$this->_useLocking = ! $this->config->getDisableLocking(); | ||
|
||
// Use sleep time multiplier so fail after time is in seconds | ||
|
@@ -411,7 +419,7 @@ protected function hasConnection() | |
public function read($sessionId) | ||
{ | ||
// Get lock on session. Increment the "lock" field and if the new value is 1, we have the lock. | ||
$sessionId = self::SESSION_PREFIX.$sessionId; | ||
$sessionId = 'sess_'.$this->_prefixAdditional.$sessionId; | ||
colinmollenhour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$tries = $waiting = $lock = 0; | ||
$lockPid = $oldLockPid = null; // Restart waiting for lock when current lock holder changes | ||
$detectZombies = false; | ||
|
@@ -616,6 +624,8 @@ public function read($sessionId) | |
*/ | ||
public function write($sessionId, $sessionData) | ||
{ | ||
$sessionId = 'sess_'.$this->_prefixAdditional.$sessionId; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about adding prefix as a configurable parameter this->config->getPrefix() with a default value 'sess_' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good as long as it is fully BC. |
||
|
||
if ($this->_sessionWritten || $this->_readOnly) { | ||
$this->_log(sprintf(($this->_sessionWritten ? "Repeated" : "Read-only") . " session write detected; skipping for ID %s", $sessionId)); | ||
return true; | ||
|
@@ -628,7 +638,7 @@ public function write($sessionId, $sessionData) | |
if($this->_dbNum) $this->_redis->select($this->_dbNum); // Prevent conflicts with other connections? | ||
|
||
if ( ! $this->_useLocking | ||
|| ( ! ($pid = $this->_redis->hGet('sess_'.$sessionId, 'pid')) || $pid == $this->_getPid()) | ||
|| ( ! ($pid = $this->_redis->hGet($sessionId, 'pid')) || $pid == $this->_getPid()) | ||
) { | ||
$this->_writeRawSession($sessionId, $sessionData, $this->getLifeTime()); | ||
Genaker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->_log(sprintf("Data written to ID %s in %.5f seconds", $sessionId, (microtime(true) - $timeStart))); | ||
|
@@ -661,10 +671,12 @@ public function write($sessionId, $sessionData) | |
*/ | ||
public function destroy($sessionId) | ||
{ | ||
|
||
$sessionId = 'sess_'.$this->_prefixAdditional.$sessionId; | ||
$this->_log(sprintf("Destroying ID %s", $sessionId)); | ||
$this->_redis->pipeline(); | ||
if($this->_dbNum) $this->_redis->select($this->_dbNum); | ||
$this->_redis->del(self::SESSION_PREFIX.$sessionId); | ||
$this->_redis->del($sessionId); | ||
$this->_redis->exec(); | ||
return true; | ||
} | ||
|
@@ -828,15 +840,14 @@ protected function _decodeData($data) | |
*/ | ||
protected function _writeRawSession($id, $data, $lifetime) | ||
{ | ||
$sessionId = 'sess_' . $id; | ||
$this->_redis->pipeline() | ||
->select($this->_dbNum) | ||
->hMSet($sessionId, array( | ||
->hMSet($id, array( | ||
'data' => $this->_encodeData($data), | ||
'lock' => 0, // 0 so that next lock attempt will get 1 | ||
)) | ||
->hIncrBy($sessionId, 'writes', 1) | ||
->expire($sessionId, min((int)$lifetime, (int)$this->_maxLifetime)) | ||
->hIncrBy($id, 'writes', 1) | ||
->expire($id, min((int)$lifetime, (int)$this->_maxLifetime)) | ||
->exec(); | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.