Skip to content
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

Additional Session Prefix #29

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/Cm/RedisSession/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ class Handler implements \SessionHandlerInterface
* @var boolean
*/
private $_readOnly;

/**
* Additional Configurable Prefix for Session
*
* @var string
*/
private $_prefixAdditional;

/**
* @param ConfigInterface $config
Expand Down Expand Up @@ -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() ?: '';
colinmollenhour marked this conversation as resolved.
Show resolved Hide resolved
$this->_useLocking = ! $this->config->getDisableLocking();

// Use sleep time multiplier so fail after time is in seconds
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -616,6 +624,8 @@ public function read($sessionId)
*/
public function write($sessionId, $sessionData)
{
$sessionId = 'sess_'.$this->_prefixAdditional.$sessionId;
Copy link
Author

Choose a reason for hiding this comment

The 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_'
without getAdditionalPrefix()? Then if somebody wants override can override the default parameter.

Copy link
Owner

Choose a reason for hiding this comment

The 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;
Expand All @@ -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)));
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}

Expand Down
10 changes: 10 additions & 0 deletions src/Cm/RedisSession/Handler/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,14 @@ public function getSentinelVerifyMaster();
* @return string
*/
public function getSentinelConnectRetries();


/**
* Get configurable prefix for session ID
*
* @return string
*/
public function getPrefix();


}