Class yii\redis\Session
Inheritance | yii\redis\Session » yii\web\Session |
---|---|
Available since version | 2.0 |
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 |
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 \yii\redis\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.
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 |
---|
Session destroy handler.
Do not call this method directly.
public boolean destroySession ( $id ) | ||
$id | string | Session ID |
return | boolean | Whether session is destroyed successfully |
---|
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. |
---|
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. |
---|
Session read handler.
Do not call this method directly.
public string readSession ( $id ) | ||
$id | string | Session ID |
return | string | The session 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 |
---|