Class yii\redis\Session

Inheritanceyii\redis\Session » yii\web\Session
Available since version2.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

Hide inherited properties

PropertyTypeDescriptionDefined 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

Hide inherited methods

MethodDescriptionDefined 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

Hide inherited methods

MethodDescriptionDefined By
calculateKey() Generates a unique key used for storing session data in cache. yii\redis\Session

Property Details

$keyPrefix public property

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.

public string $keyPrefix null
$redis public property

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

calculateKey() protected method

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

destroySession() public method

Session destroy handler.

Do not call this method directly.

public boolean destroySession ( $id )
$id string

Session ID

return boolean

Whether session is destroyed successfully

getUseCustomStorage() public method

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.

init() public method

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.

readSession() public method

Session read handler.

Do not call this method directly.

public string readSession ( $id )
$id string

Session ID

return string

The session data

writeSession() public method

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