Class yii\redis\Session
Inheritance | yii\redis\Session » yii\web\Session |
---|---|
Available since extension's version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-redis/blob/master/src/Session.php |
Redis Session implements a session component using redis as the storage medium.
Redis Session requires redis version 2.6.12 or higher to work properly.
It needs to be configured with a redis yii\redis\Connection that is also configured as an application component.
By default it will use the redis
application component.
To use redis Session as the session application component, configure the application as follows,
[
'components' => [
'session' => [
'class' => 'yii\redis\Session',
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
]
],
],
]
Or if you have configured the redis yii\redis\Connection as an application component, the following is sufficient:
[
'components' => [
'session' => [
'class' => 'yii\redis\Session',
// 'redis' => 'redis' // id of the connection application component
],
],
]
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$keyPrefix | string | A string prefixed to every cache key so that it is unique. | yii\redis\Session |
$redis | yii\redis\Connection|string|array | The Redis yii\redis\Connection object or the application component ID of the Redis yii\redis\Connection. | yii\redis\Session |
$useCustomStorage | boolean | Whether to use custom storage. | yii\redis\Session |
Public Methods
Method | Description | Defined By |
---|---|---|
destroySession() | Session destroy handler. | yii\redis\Session |
getUseCustomStorage() | Returns a value indicating whether to use custom session storage. | yii\redis\Session |
init() | Initializes the redis Session component. | yii\redis\Session |
readSession() | Session read handler. | yii\redis\Session |
writeSession() | Session write handler. | yii\redis\Session |
Protected Methods
Method | Description | Defined By |
---|---|---|
calculateKey() | Generates a unique key used for storing session data in cache. | yii\redis\Session |
Property Details
A string prefixed to every cache key so that it is unique. If not set, it will use a prefix generated from Application::id. You may set this property to be an empty string if you don't want to use key prefix. It is recommended that you explicitly set this property to some static value if the cached data needs to be shared among multiple applications.
The Redis yii\redis\Connection object or the application component ID of the Redis yii\redis\Connection. This can also be an array that is used to create a redis yii\redis\Connection instance in case you do not want do configure redis connection as an application component. After the Session object is created, if you want to change this property, you should only assign it with a Redis yii\redis\Connection object.
Whether to use custom storage.
Method Details
Generates a unique key used for storing session data in cache.
protected string calculateKey ( $id ) | ||
$id | string |
Session variable name |
return | string |
A safe cache key associated with the session variable name |
---|
protected function calculateKey($id)
{
return $this->keyPrefix . md5(json_encode([__CLASS__, $id]));
}
Session destroy handler.
Do not call this method directly.
public boolean destroySession ( $id ) | ||
$id | string |
Session ID |
return | boolean |
Whether session is destroyed successfully |
---|
public function destroySession($id)
{
$this->redis->executeCommand('DEL', [$this->calculateKey($id)]);
// @see https://github.com/yiisoft/yii2-redis/issues/82
return true;
}
Returns a value indicating whether to use custom session storage.
This method overrides the parent implementation and always returns true.
public boolean getUseCustomStorage ( ) | ||
return | boolean |
Whether to use custom storage. |
---|
public function getUseCustomStorage()
{
return true;
}
Initializes the redis Session component.
This method will initialize the $redis property to make sure it refers to a valid redis connection.
public void init ( ) | ||
throws | \yii\base\InvalidConfigException |
if $redis is invalid. |
---|
public function init()
{
$this->redis = Instance::ensure($this->redis, Connection::className());
if ($this->keyPrefix === null) {
$this->keyPrefix = substr(md5(Yii::$app->id), 0, 5);
}
parent::init();
}
Session read handler.
Do not call this method directly.
public string readSession ( $id ) | ||
$id | string |
Session ID |
return | string |
The session data |
---|
public function readSession($id)
{
$data = $this->redis->executeCommand('GET', [$this->calculateKey($id)]);
return $data === false || $data === null ? '' : $data;
}
Session write handler.
Do not call this method directly.
public boolean writeSession ( $id, $data ) | ||
$id | string |
Session ID |
$data | string |
Session data |
return | boolean |
Whether session write is successful |
---|
public function writeSession($id, $data)
{
if ($this->getUseStrictMode() && $id === $this->_forceRegenerateId) {
//Ignore write when forceRegenerate is active for this id
return true;
}
return (bool) $this->redis->executeCommand('SET', [$this->calculateKey($id), $data, 'EX', $this->getTimeout()]);
}