PHP Class yii\redis\Session

Redis Session requires redis version 2.6.12 or higher to work properly. It needs to be configured with a 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 Connection as an application component, the following is sufficient: ~~~ [ 'components' => [ 'session' => [ 'class' => 'yii\redis\Session', 'redis' => 'redis' // id of the connection application component ], ], ] ~~~
Since: 2.0
Author: Carsten Brandt ([email protected])
Inheritance: extends yii\web\Session
Show file Open project: yiisoft/yii2-redis

Public Properties

Property Type Description
$keyPrefix 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.
$redis the Redis Connection object or the application component ID of the Redis Connection. This can also be an array that is used to create a 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 Connection object.

Public Methods

Method Description
destroySession ( string $id ) : boolean Session destroy handler.
getUseCustomStorage ( ) : boolean Returns a value indicating whether to use custom session storage.
init ( ) Initializes the redis Session component.
readSession ( string $id ) : string Session read handler.
writeSession ( string $id, string $data ) : boolean Session write handler.

Protected Methods

Method Description
calculateKey ( string $id ) : string Generates a unique key used for storing session data in cache.

Method Details

calculateKey() protected method

Generates a unique key used for storing session data in cache.
protected calculateKey ( string $id ) : string
$id string session variable name
return string a safe cache key associated with the session variable name

destroySession() public method

Do not call this method directly.
public destroySession ( string $id ) : boolean
$id string session ID
return boolean whether session is destroyed successfully

getUseCustomStorage() public method

This method overrides the parent implementation and always returns true.
public getUseCustomStorage ( ) : boolean
return boolean whether to use custom storage.

init() public method

This method will initialize the [[redis]] property to make sure it refers to a valid redis connection.
public init ( )

readSession() public method

Do not call this method directly.
public readSession ( string $id ) : string
$id string session ID
return string the session data

writeSession() public method

Do not call this method directly.
public writeSession ( string $id, string $data ) : boolean
$id string session ID
$data string session data
return boolean whether session write is successful

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 [[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 $keyPrefix

$redis public property

the Redis Connection object or the application component ID of the Redis Connection. This can also be an array that is used to create a 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 Connection object.
public $redis