Class yii\redis\Session
| Inheritance | yii\ |
|---|---|
| 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 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\
[
'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 | yii\ |
The Redis yii\ |
yii\ |
| $useCustomStorage | boolean | Whether to use custom storage. | yii\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| destroySession() | Session destroy handler. | yii\ |
| getUseCustomStorage() | Returns a value indicating whether to use custom session storage. | yii\ |
| init() | Initializes the redis Session component. | yii\ |
| readSession() | Session read handler. | yii\ |
| writeSession() | Session write handler. | yii\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| calculateKey() | Generates a unique key used for storing session data in cache. | yii\ |
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\
Whether to use custom storage.
Method Details
Generates a unique key used for storing session data in cache.
| protected string calculateKey ( string $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 ( string $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 mixed init ( ) | ||
| throws | \ |
if $redis is invalid. |
|---|---|---|
public function init()
{
$this->redis = Instance::ensure($this->redis, ConnectionInterface::class);
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 ( string $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 ( string $id, string $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()]);
}